如何使用ALTER ROLE将用户添加到sysadmin角色?

时间:2013-07-14 15:15:20

标签: sql-server

'sa'用户在执行以下脚本时,会显示以下错误消息。

IF NOT EXISTS (SELECT LOGINNAME FROM MASTER.DBO.SYSLOGINS WHERE NAME = N'p_sys')
BEGIN
    CREATE LOGIN [p_sys] WITH PASSWORD=N'test', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF;
    ALTER ROLE sysadmin ADD MEMBER p_sys;
END

Msg 15151, Level 16, State 1, Line 4
Cannot alter the role 'sysadmin', because it does not exist or you do not have permission.

我不想使用sp_addsrvrolemember,因为MS说它是deprecated

请帮忙。

1 个答案:

答案 0 :(得分:6)

alter role用于数据库角色,但sysadmin是服务器角色。请改为alter server role

ALTER SERVER ROLE server_role_name ADD MEMBER p_sys