如何让SQL Server Express创建用户?

时间:2010-06-04 18:47:16

标签: sql-server-2008-express

在我的机器上以管理员身份运行,我可以很好地创建数据库,但是当我尝试创建用户时,它会因此错误消息而失败。

1> create user mark from login mark
2> go
Msg 15007, Level 16, State 1, Server ENDER\SQLEXPRESS, Line 1
'mark' is not a valid login or you do not have permission.

我正在使用sqlcmd来执行此操作。

1 个答案:

答案 0 :(得分:5)

在创建数据库用户“mark”

之前,您需要创建服务器登录名“mark”

e.g。对于Windows身份验证

CREATE LOGIN [mark] FROM WINDOWS WITH DEFAULT_DATABASE=[master]

e.g。或者对于SQL Server身份验证

CREATE LOGIN [mark] WITH PASSWORD=N'YourPassword', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF

然后,一旦创建了登录,以下内容就可以正常工作

CREATE USER [mark] FOR LOGIN [mark]

顺便获取服务器登录列表,这是管理工作室使用的查询

SELECT
log.name AS [Name],
CASE WHEN N'U' = log.type THEN 0 WHEN N'G' = log.type THEN 1 WHEN N'S' = log.type THEN 2 WHEN N'C' = log.type THEN 3 WHEN N'K' = log.type THEN 4 END AS [LoginType],
log.is_disabled AS [IsDisabled],
log.create_date AS [CreateDate]
FROM
sys.server_principals AS log
WHERE
(log.type in ('U', 'G', 'S', 'C', 'K') AND log.principal_id not between 101 and 255 AND log.name <> N'##MS_AgentSigningCertificate##')
ORDER BY
[Name] ASC