MSreenath


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

create login should be executed after check.

thanks,

Sreenath





Re: How to check whether SQL login exists?

thermanson


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