Tuesday, March 27, 2012

Changing the LoginName of a user

I'm trying to automate the creation of a databse and a User/Login but i'm running into problem with a conflict between a new user to which i'm trying to assign a WindowsLogin which is already linked to the dbo. (SQL Server 2005)

Is there a way to reassign the dbo's LoginName to some othe user

Here the code

IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = N'ABC')
BEGIN
declare @.cmd nvarchar(max)
set @.cmd = 'CREATE USER [ABC] FOR LOGIN ' + @.WindowsLogin + ' WITH DEFAULT_SCHEMA=[Shared]'
exec(@.cmd)
END

Which give me the error

Msg 15063, Level 16, State 1, Server SR-DEV-GOI1, Line 1
The login already has an account under a different user name.

The script is running under the @.WindowsLogin in question !

Thanks All

Gilbert


** Reply to myself **

I have found the way to change the loginName of the dbo user with sp_changedbowner but then it restrict the access to the database. We're gonna use an other Login to create the process (which makes more sense)
|||

The system SP sp_changedbowner uses the ALTER AUTHORIZATION DDL among other things. You can use the DDL instead to perform more granular operations including changing database ownership. See BOL for more details.

No comments:

Post a Comment