2012-05-29 72 views
0

我想要一個具有相同名稱的用戶和數據庫模式。執行下面的腳本用戶和數據庫模式衝突

CREATE DATABASE db1 
GO 
EXECUTE sp_addlogin @loginame = 'abc', 
        @passwd = '123456789', 
        @defdb = 'db1' 
GO 
USE db1 
GO 
EXECUTE sp_grantdbaccess @loginame = 'abc', 
         @name_in_db = 'abc' 
GO 
CREATE SCHEMA abc 
GO 

給我的錯誤:

Msg 2714: There is already an object named ‘abc’ in the database.

有什麼辦法來處理這不使用不同的名字?

回答

2

我建議不要使用sp_過程,並且在數據庫中將用戶添加爲數據庫之前,我不會將數據庫設置爲默認值。

CREATE DATABASE db1; 
GO 
CREATE LOGIN abc WITH PASSWORD = '123456789'; 
GO 
USE db1; 
GO 
CREATE USER abc FROM LOGIN abc; 
GO 
ALTER LOGIN abc WITH DEFAULT_DATABASE = db1; 
GO 
CREATE SCHEMA abc; 
GO 

-- if you want them to own the schema: 
ALTER AUTHORIZATION ON schema::abc TO abc; 

-- if you just want abc to be their default schema: 
ALTER USER abc WITH DEFAULT_SCHEMA = abc; 
GO