Sunday, September 4, 2011

Fixing Orphanes Users in SQL Server


What is an orphaned User?
A database user for which the corresponding SQL Server login is undefined or is incorrectly defined on a server instance cannot log in to the instance. Such a user is said to be an orphaned user of the database on that server instance. A database user can become orphaned if the corresponding SQL Server login is dropped. Also, a database user can become orphaned after a database is restored or attached to a different instance of SQL Server. Orphaning can happen if the database user is mapped to a SID that is not present in the new server instance. SQL Server manages the SIDs for SQL Server logins so there is no guarantee that the new login has the same SID as the original login did. Then when you restore your database, the users in that database are expecting SIDs that are not there and the next thing you know you have orphaned users. Just a note, this does not occur with Windows Logins because the SID is controlled by Windows or Active Directory. Unless you drop and re-create the user in Windows, the SID of an Active Directory user will be the same on all SQL Servers and hence your user accounts see the SID they are looking for.

How do you fix the problem without dropping and re-creating the user and messing up the permissions in the process?
By using a stored procedure called sp_change_users_login that you can use to fix orphaned users. This procedure can do several things; it can tell you which users are orphaned, it lets you fix an orphaned user manually, and it can attempt to automatically fix your issues.

Detecting orphaned users:
USE ;
GO;
sp_change_users_login @Action='Report';
GO;
Note: sp_change_users_login cannot be used with SQL Server logins that are created from Windows.

Fixing a particular Orphaned User:
Run sp_change_users_login with the UPDATE_ONE action and tell SQL Server the name of my orphaned user and the name of the appropriate login.
EXEC sp_change_users_login 'UPDATE_ONE','name_orphanedUser','name_appropriateLogin'

Fixing Orphaned Users Automatically:
This procedure should be created in the Master database. This procedure takes no parameters. It will remap orphaned users in the current database to EXISTING logins of the same name. This is usefull in the case a new database is created by restoring a backup to a new database, or by attaching the datafiles to a new server.

IF OBJECT_ID('dbo.sp_fixusers') IS NOT NULL
BEGIN
DROP PROCEDURE dbo.sp_fixusers
IF OBJECT_ID('dbo.sp_fixusers') IS NOT NULL
PRINT '<<< FAILED DROPPING PROCEDURE dbo.sp_fixusers >>>'
ELSE
PRINT '<<< DROPPED PROCEDURE dbo.sp_fixusers >>>'
END

GO

CREATE PROCEDURE dbo.sp_fixusers

AS

BEGIN

DECLARE @username varchar(25)

DECLARE fixusers CURSOR
FOR

SELECT UserName = name FROM sysusers
WHERE issqluser = 1 and (sid is not null and sid <> 0x0)
and suser_sname(sid) is null
ORDER BY name

OPEN fixusers

FETCH NEXT FROM fixusers
INTO @username

WHILE @@FETCH_STATUS = 0
BEGIN
EXEC sp_change_users_login 'update_one', @username, @username
FETCH NEXT FROM fixusers
INTO @username
END


CLOSE fixusers
DEALLOCATE fixusers
END
go
IF OBJECT_ID('dbo.sp_fixusers') IS NOT NULL
PRINT '<<< CREATED PROCEDURE dbo.sp_fixusers >>>'
ELSE
PRINT '<<< FAILED CREATING PROCEDURE dbo.sp_fixusers >>>'
go

Congratulations! Orphaned users are Fixed.

Friday, September 2, 2011

Configuring Database Mail in SQL Server 2008 R2

Database mail enables an instance of SQL server to send outbound e-mail messages.

For using Database mail feature in SQL server, one has to enable database mail feature by running the following query:

EXEC sp_configure "Database Mail XPs",1
GO
reconfigure with override


The next step would be to Configure Database Mail. For this,
    Connect to the instance of SQL server. Open Management node -> right click on Database Mail -> Configure. You will get welcome screen. Click Next. (There's nothing to do on the Welcome screen). In case you didnt enable the feature of Database Mail, it shows you a pop up asking you to enable it. Click "ok". If you did enable it earlier, it just takes you to the next page of the dialog.
    Click Set up(assuming you're installing it for the first time) -> Next.
    Specify Name for the profile and Click on ADD for configuring e-mail.
    You need an SMTP account for this. Fill in your account details. Also select your authentication mode and fill in the required details.
    Click ok and then click Next -> Finish.
    The final page shows all your configurations and gives message of completion. Click Close.
Now you are ready to test your Database Mail.
Right Click on Database Mail and click on send Test Mail. Enter the recepient's email address and hit send mail. Verify that you have recieved it.
Congratulations. You are all set to start using this wonderful service.