Checking if a SQL Server login already exists

前端 未结 10 2099
盖世英雄少女心
盖世英雄少女心 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
    
    0 讨论(0)
  • 2020-12-12 12:30

    You can use the built-in function:

    SUSER_ID ( [ 'myUsername' ] )
    

    via

    IF [value] IS NULL [statement]
    

    like:

    IF SUSER_ID (N'myUsername') IS NULL
    CREATE LOGIN [myUsername] WITH PASSWORD=N'myPassword', 
    DEFAULT_LANGUAGE=[us_english], 
    CHECK_EXPIRATION=OFF, 
    CHECK_POLICY=OFF 
    GO
    

    https://technet.microsoft.com/en-us/library/ms176042(v=sql.110).aspx

    0 讨论(0)
  • 2020-12-12 12:36

    As a minor addition to this thread, in general you want to avoid using the views that begin with sys.sys* as Microsoft is only including them for backwards compatibility. For your code, you should probably use sys.server_principals. This is assuming you are using SQL 2005 or greater.

    0 讨论(0)
  • 2020-12-12 12:36

    In order to hande naming conflict between logins, roles, users etc. you should check the type column according to Microsoft sys.database_principals documentation

    In order to handle special chacters in usernames etc, use N'<name>' and [<name>] accordingly.

    Create login

    USE MASTER
    IF NOT EXISTS (SELECT 1 FROM master.sys.server_principals WHERE 
    [name] = N'<loginname>' and [type] IN ('C','E', 'G', 'K', 'S', 'U'))
        CREATE LOGIN [<loginname>] <further parameters>
    

    Create database user

    USE [<databasename>]
    IF NOT EXISTS (SELECT 1 FROM sys.database_principals WHERE 
    [name] = N'<username>' and [type] IN ('C','E', 'G', 'K', 'S', 'U'))
        CREATE USER [<username>] FOR LOGIN [<loginname>]
    

    Create database role

    USE [<databasename>]
    IF NOT EXISTS (SELECT 1 FROM sys.database_principals WHERE 
    [name] = N'<rolename>' and Type = 'R')
        CREATE ROLE [<rolename>]
    

    Add user to role

    USE [<databasename>]
    EXEC sp_addrolemember N'<rolename>', N'<username>'
    

    Grant rights to role

    USE [<databasename>]
    GRANT SELECT ON [<tablename>] TO [<rolename>]
    GRANT UPDATE ON [<tablename>] ([<columnname>]) TO [<rolename>]
    GRANT EXECUTE ON [<procedurename>] TO [<rolename>]
    

    The SQL is tested on SQL Server 2005, 2008, 2008 R2, 2014, 2016, 2017, 2019

    0 讨论(0)
  • 2020-12-12 12:38

    First you have to check login existence using syslogins view:

    IF NOT EXISTS 
        (SELECT name  
         FROM master.sys.server_principals
         WHERE name = 'YourLoginName')
    BEGIN
        CREATE LOGIN [YourLoginName] WITH PASSWORD = N'password'
    END
    

    Then you have to check your database existence:

    USE your_dbname
    
    IF NOT EXISTS
        (SELECT name
         FROM sys.database_principals
         WHERE name = 'your_dbname')
    BEGIN
        CREATE USER [your_dbname] FOR LOGIN [YourLoginName] 
    END
    
    0 讨论(0)
  • 2020-12-12 12:40

    Try this (replace 'user' with the actual login name):

    IF NOT EXISTS(
    SELECT name 
    FROM [master].[sys].[syslogins]
    WHERE NAME = 'user')
    
    BEGIN 
        --create login here
    END
    
    0 讨论(0)
提交回复
热议问题