Checking if a SQL Server login already exists

前端 未结 10 2113
盖世英雄少女心
盖世英雄少女心 2020-12-12 12:27

I need to check if a specific login already exists on the SQL Server, and if it doesn\'t, then I need to add it.

I have found the following code to actually add the

10条回答
  •  一个人的身影
    2020-12-12 12:28

    From here

    If not Exists (select loginname from master.dbo.syslogins 
        where name = @loginName and dbname = 'PUBS')
    Begin
        Select @SqlStatement = 'CREATE LOGIN ' + QUOTENAME(@loginName) + ' 
        FROM WINDOWS WITH DEFAULT_DATABASE=[PUBS], DEFAULT_LANGUAGE=[us_english]')
    
        EXEC sp_executesql @SqlStatement
    End
    

提交回复
热议问题