Showing posts with label sreenath. Show all posts
Showing posts with label sreenath. Show all posts

Sunday, February 19, 2012

How to check whether SQL login exists?

Is there any way to check whether the login exists before creating login ?

create login should be executed after check.

thanks,

Sreenath

Try something like this:

DECLARE @.SqlStatement nvarchar(4000)
Declare @.loginName varchar (100)

Select @.loginName = 'test\thermanson'

If not Exists (select loginname from master.dbo.syslogins where name = @.loginName and dbname = 'PUBS')
Begin

Set @.SqlStatement = 'CREATE LOGIN [' + @.loginName + '] FROM WINDOWS WITH DEFAULT_DATABASE=[PUBS], DEFAULT_LANGUAGE=[us_english]'

EXEC sp_executesql @.SqlStatement
End