Skip to the content

Vincenzo Nardone 's Blog

DBA SQL Server Microsoft System Administrator



Create new login with the same SID

Author: Vincenzo Nardone
Date: 27/05/2013
Tags: Login, SID, SQL Server
Categories: SQL Server

Come creare una login SQL Server con lo stesso SID

select 'if not exists (select * from sys.server_principals where name = ''' + p.name + ''') '
			+ char(13) + char(10) + char(9)
				+'create login [' + p.name + '] ' +
	 case when p.type in('U','G') then 'from windows ' else '' end +
	 'with ' +
	 case when p.type = 'S' then 'password = ' + master.sys.fn_varbintohexstr(l.password_hash)
		+ ' hashed, ' +
	  'sid = ' + master.sys.fn_varbintohexstr(l.sid) + ', check_expiration = ' +
	 case when l.is_policy_checked > 0 then 'ON, ' else 'OFF, ' end +
	 'check_policy = ' + case when l.is_expiration_checked > 0 then 'ON, ' else 'OFF, ' end +
	 case when l.credential_id > 0 then 'credential = ' + c.name + ', ' else '' end
		else '' end +
	 'default_database = ' + p.default_database_name +
		case when len(p.default_language_name) > 0 then ', default_language = '
		+ p.default_language_name else '' end
from sys.server_principals p
left join sys.sql_logins l
on  p.principal_id = l.principal_id
left join sys.credentials c
on  l.credential_id = c.credential_id
where p.type in('S','U','G')
and  p.name = '<SQLServer_Login>'
Top expensive query in SQL Server »
  • S.A. says:
    24/01/2014 at 18:23

    Non male, ma perchè non usare la stored di MS che, oltre al SID, si porta via anche la password di eventuali login SQL?

    Reply
  • football players says:
    14/04/2017 at 09:51

    Fantastic post however , I was wondering if you could write a litte more on this
    topic? I’d be very grateful if you could elaborate a little bit
    further. Kudos!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Categories

  • Oracle (1)
  • PowerShell (15)
  • SharePoint (15)
  • SQL Server (19)
  • Windows Server (5)

Posts

  • .net Framework 3.5 installation failed with error code 0x800f081f
  • How to Change the Caching Service Account (AppFabric 1.1)
  • How to change the Distributed Cache Service Managed Account
  • How to rename SharePoint_AdminContent Database
  • How to find the size of Database File Data&Log in SQL Server

Tags

AlwaysOn Backup Cache Central Administration Configure Copy Count Details File Filter Folder Grant IIS LinkedServer Location MultiSubnetFailover OPENROWSET Oracle Parallelism Permissions PowerShell Processes Query RAC Restore Result Resume Script Search Service Application Service Account Set SharePoint Shutdown Site Collection sp_who2 SQL Server Startup State Service Status Tables TempDB TOP Web Application Windows Server WSS_UsageApplication Proxy

Archives

  • March 2016 (1)
  • February 2016 (3)
  • January 2016 (1)
  • May 2015 (3)
  • July 2014 (1)
  • June 2014 (1)
  • May 2014 (2)
  • April 2014 (1)
  • March 2014 (2)
  • February 2014 (1)
  • January 2014 (1)
  • November 2013 (4)
  • June 2013 (8)
  • May 2013 (9)

RSS Vincenzo Nardone 's Blog

  • .net Framework 3.5 installation failed with error code 0x800f081f
  • How to Change the Caching Service Account (AppFabric 1.1)
  • How to change the Distributed Cache Service Managed Account
© 2026 Vincenzo Nardone 's Blog. All rights reserved. Powered by WordPress.