Monday, November 24, 2008

Create new SQL Server Login on Log shipping destination server

Create new SQL Server Login on Log shipping destination server
As restored database on destination server will be in standby read only mode, it is not possible to create a new login on it and then associate this login with newly restored database. If you require to create a login with specific permissions onto restored database you need to create a similar login on source server first and then onto destination server. Follow the steps below to create a new SQL Server login on log shipping destination server.
a. Create new SQL Server login on source machine and grant required database access permissions which you want this user to have on destination machine.
b. Create a user on source database on source machine from newly created login in step a.
c. Take a transaction log backup on source machine and transfer to destination server.
d. Create new SQL Server login on destination machine with same login name as of source server. Don’t give any database access grants to this login yet.
e. Restore the transaction log on destination server which was backed in step c. After successful restoration, you’ll see the new user created in step a in the database security user tab. But at this step, the new user will not be able to login and access the database.
f. Log on to source machine again and get the SID of SQL login created in step a. You can use the following query to get the SID of the login:

SELECT name, sid
FROM master.dbo.syslogins
WHERE name in ('testlogin1')
Note: ‘Testlogin1’ is the login name.

g. Now on destination server, drop the login created in step d. Use the following query to drop the login
sp_droplogin 'testlogin1'
go
Note: ‘Testlogin1’ is the login name.

h. Recreate the same login again but with same SID as of source machine.
sp_addlogin 'testlogin1', @passwd='pwd123', sid=0xA20F6D5qwer0891asdfB448380E66F5D8C6155
go
Note: ‘Testlogin1’ is the login name. ‘pwd123’ is the password for this login and ‘0xA20F6D5qwer0891asdfB448380E66F5D8C6155’ is the SID from source machine

i. Run a test to ensure newly created SQL login can successfully access the restored database.

Get date of next Sunday based on a date

If you need to find the date on next sunday based on a date value supplied, use the following query

SELECT DATEADD(wk,DATEDIFF(wk,0,cast('20081121' as datetime)+7),0)-1

Source: http://www.sqlservercentral.com/Forums/Topic606669-338-3.aspx