none
How to check whether SQL login exists?

    Question

  •  

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

     

    create login should be executed after check.

     

    thanks,

     

    Sreenath

    Monday, August 27, 2007 8:44 PM

All replies

  • 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
    Monday, August 27, 2007 9:51 PM