Checking if a SQL Server login already exists

前端 未结 10 2110
盖世英雄少女心
盖世英雄少女心 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: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

提交回复
热议问题