无法为用户编写脚本

时间:2018-12-05 17:27:36

标签: sql-server sql-server-2017

我在本地SQL Server 14.0.1000.169数据库上以sa身份登录,并尝试运行以下脚本:

 CREATE USER [myuser] FOR LOGIN [myuser] WITH DEFAULT_SCHEMA=[dbo]
 GO

我得到以下结果:

Msg 15007, Level 16, State 1, Line 1
myuser is not a valid login or you do not have permission

我仔细检查了一下,发现myuser不存在,而dbo确实存在,知道有什么问题吗?

1 个答案:

答案 0 :(得分:0)

Before Creating a User you must first create a global Login.

under Security -> Logins -> NewLogin

Or

First connect to the server and switch to the master database. In master create a login and then add a user for that login to the master database.

CREATE LOGIN [MyLogin] WITH password='xxxxxxxxx'
GO

CREATE USER [MyUser] FOR LOGIN [MyLogin] WITH DEFAULT_SCHEMA=[dbo] 
GO

Once the login exists you can add them as a User under a particular database.

相关问题