January 17, 2014 at 2:45 pm
I need to copy some databases from one server to another. What is the best way to copy logins with their passwords? Each login has access to multiple databases and I want that permissions to be preserved in the new database.
January 17, 2014 at 3:04 pm
SQL_Surfer (1/17/2014)
I need to copy some databases from one server to another. What is the best way to copy logins with their passwords? Each login has access to multiple databases and I want that permissions to be preserved in the new database.
Use "Windows Authentication" instead of "SQL Server Authentication" in the future. I suppose one could write a script to grab the hash codes for the user passwords but, IIRC, even that won't work because the PW's are server senstive (and, yeah... I could be wrong but that's my recollection).
--Jeff Moden
Change is inevitable... Change for the better is not.
January 17, 2014 at 3:09 pm
microsoft supplies a proc named sp_help_revlogin here that scripts out sql users with hashed passwords, for importation onto other servers.
you'll need to grab the right version off of the MS site, as there's a version for 2005 vs 2008 (i think)
Lowell
January 17, 2014 at 4:04 pm
the version for 2005 and 2008 is the same.(but different from the 2000 version)
---------------------------------------------------------------------
January 18, 2014 at 8:54 am
Lowell (1/17/2014)
microsoft supplies a proc named sp_help_revlogin here that scripts out sql users with hashed passwords, for importation onto other servers.you'll need to grab the right version off of the MS site, as there's a version for 2005 vs 2008 (i think)
Ah, dang. I forgot all about that. Thanks, Lowell.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 20, 2014 at 5:23 am
SQL_Surfer (1/17/2014)
I need to copy some databases from one server to another. What is the best way to copy logins with their passwords? Each login has access to multiple databases and I want that permissions to be preserved in the new database.
Are these logins to be moved from a legacy version of SQL Server or between instances that are SQL Server 2005 onwards?
To get a login and retain its password and SID use the following to script the SQL login out.
note: this is not necessary with Windows logins as the SID is pulled from the domain controller
SELECT'CREATE LOGIN [' + name + '] WITH PASSWORD = ' +
sys.fn_varbintohexstr(password_hash) +
' HASHED, SID = ' + sys.fn_varbintohexstr(sid) +
', DEFAULT_DATABASE = ' + QUOTENAME(default_database_name) +
', DEFAULT_LANGUAGE = ' + default_language_name +
', CHECK_EXPIRATION = ' +
CASE
WHEN is_expiration_checked = 0 THEN 'OFF'
ELSE 'ON'
END +
', CHECK_POLICY = ' +
CASE
WHEN is_policy_checked = 0 THEN 'OFF'
ELSE 'ON'
END +
CASE is_disabled
WHEN 0 THEN ''
ELSE '; ALTER LOGIN [' + name + '] DISABLE;'
END
FROM master.sys.sql_logins
-----------------------------------------------------------------------------------------------------------
"Ya can't make an omelette without breaking just a few eggs" 😉
January 20, 2014 at 5:25 am
Also consider the SSIS Transfer Logins task.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
January 20, 2014 at 6:07 am
GilaMonster (1/20/2014)
Also consider the SSIS Transfer Logins task.
Oh jesus, not that festering steamy pile of doggy doo doo 😀
My script is a lot cleaner and easier 😉
-----------------------------------------------------------------------------------------------------------
"Ya can't make an omelette without breaking just a few eggs" 😉
January 20, 2014 at 6:37 am
Perry Whittle (1/20/2014)
SQL_Surfer (1/17/2014)
I need to copy some databases from one server to another. What is the best way to copy logins with their passwords? Each login has access to multiple databases and I want that permissions to be preserved in the new database.Are these logins to be moved from a legacy version of SQL Server or between instances that are SQL Server 2005 onwards?
To get a login and retain its password and SID use the following to script the SQL login out.
note: this is not necessary with Windows logins as the SID is pulled from the domain controller
SELECT'CREATE LOGIN [' + name + '] WITH PASSWORD = ' +
sys.fn_varbintohexstr(password_hash) +
' HASHED, SID = ' + sys.fn_varbintohexstr(sid) +
', DEFAULT_DATABASE = ' + QUOTENAME(default_database_name) +
', DEFAULT_LANGUAGE = ' + default_language_name +
', CHECK_EXPIRATION = ' +
CASE
WHEN is_expiration_checked = 0 THEN 'OFF'
ELSE 'ON'
END +
, CHECK_POLICY = ' +
CASE
WHEN is_policy_checked = 0 THEN 'OFF'
ELSE 'ON'
END +
CASE is_disabled
WHEN 0 THEN ''
ELSE '; ALTER LOGIN [' + name + '] DISABLE;'
END
FROM master.sys.sql_logins
Perry, syntax error on line , CHECK_POLICY = ' +
comma should be a quote.
this also captures 'sa' login which you would want to omit when transferring to a new server.
---------------------------------------------------------------------
January 20, 2014 at 6:39 am
Perry Whittle (1/20/2014)
GilaMonster (1/20/2014)
Also consider the SSIS Transfer Logins task.Oh jesus, not that festering steamy pile of doggy doo doo 😀
as perry says - doggy doo doo. For sql authenticated accounts it disables the id AND changes its password to a random value on the destination server, so worse than useless for most purposes.
---------------------------------------------------------------------
January 20, 2014 at 7:20 am
george sibbald (1/20/2014)
Perry Whittle (1/20/2014)
SQL_Surfer (1/17/2014)
I need to copy some databases from one server to another. What is the best way to copy logins with their passwords? Each login has access to multiple databases and I want that permissions to be preserved in the new database.Are these logins to be moved from a legacy version of SQL Server or between instances that are SQL Server 2005 onwards?
To get a login and retain its password and SID use the following to script the SQL login out.
note: this is not necessary with Windows logins as the SID is pulled from the domain controller
SELECT'CREATE LOGIN [' + name + '] WITH PASSWORD = ' +
sys.fn_varbintohexstr(password_hash) +
' HASHED, SID = ' + sys.fn_varbintohexstr(sid) +
', DEFAULT_DATABASE = ' + QUOTENAME(default_database_name) +
', DEFAULT_LANGUAGE = ' + default_language_name +
', CHECK_EXPIRATION = ' +
CASE
WHEN is_expiration_checked = 0 THEN 'OFF'
ELSE 'ON'
END +
, CHECK_POLICY = ' +
CASE
WHEN is_policy_checked = 0 THEN 'OFF'
ELSE 'ON'
END +
CASE is_disabled
WHEN 0 THEN ''
ELSE '; ALTER LOGIN [' + name + '] DISABLE;'
END
FROM master.sys.sql_logins
Perry, syntax error on line , CHECK_POLICY = ' +
comma should be a quote.
this also captures 'sa' login which you would want to omit when transferring to a new server.
yes, it also scripts the default cert logins too, but you just need to apply the ones you require.
I've corrected my original script above
-----------------------------------------------------------------------------------------------------------
"Ya can't make an omelette without breaking just a few eggs" 😉
January 20, 2014 at 8:35 am
Nice Script Perry. When transferring logins, I will always go for the script approach similar to what Perry has shared.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
January 20, 2014 at 10:41 am
Does this also carry over the appropriate permissions on the dbs?
January 20, 2014 at 11:06 am
database permissions are held within the database, so will be taken over with the database restore.
Scripts above only deal with the logins, but as they take the sids with them, the logins will automatically marry up to the users within the database.
---------------------------------------------------------------------
January 20, 2014 at 11:26 am
The script doesn't carry server permissions, but with a little imagination and querying the correct catalogs its easy to accomplish.
-----------------------------------------------------------------------------------------------------------
"Ya can't make an omelette without breaking just a few eggs" 😉
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply