且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查Azure SQL数据库中是否已存在数据库用户

更新时间:2023-02-08 17:21:09

查看包含的数据库用户模型( https://msdn.microsoft.com/en-us/library/ff929188.aspx ).

Look into the Contained Database User Model (https://msdn.microsoft.com/en-us/library/ff929188.aspx).

在包含的数据库用户模型中,不存在master数据库中的登录名.而是在用户数据库上进行身份验证过程,并且用户数据库中的数据库用户在主数据库中没有关联的登录名.

In the contained database user model, the login in the master database is not present. Instead, the authentication process occurs at the user database, and the database user in the user database does not have an associated login in the master database.

IF NOT EXISTS (
    SELECT  [name]
    FROM    sys.database_principals
    WHERE   [name] = 'myusername'
)
BEGIN
    CREATE USER [myusername] WITH PASSWORD = '********';
END