sqlserver关于对列的权限控制

时间:2021-12-21 23:25:58

-- 脚本新建登录数据库的用户

USE [master]
GO
CREATE LOGIN [sa1] WITH PASSWORD=N'123456', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
USE [yipond0229]
GO
CREATE USER [sa1] FOR LOGIN [sa1]
GO
--给新建的用户sa1 授权对 某库某表的 某些字段进行Select,注意该用户登录进之后,只能查看到被授权的表,但可以查看到这些表其他未被授权的字段
GRANT SELECT (UserSourceTypeId, Token) ON [Yisheng0229].[dbo].[AuthToken] TO sa1

----给新建的用户sa1 授权对 某库某表的 某些字段进行Update,注意该用户登录进之后,只能查看到被授权的表,但可以查看到这些表其他未被授权的字段
GRANT UPDATE([UserSourceTypeId], [Token]) ON [yipond0229].dbo.AuthToken TO sa1;
--下面脚本查看某用户所操作的权限
SELECT dp.grantee_principal_id ,
P.name AS UName ,
dp.permission_name ,
C.name ,
OBJECT_NAME(O.object_id) AS TabName
FROM sys.database_permissions dp
INNER JOIN sys.objects O ON dp.major_id = O.object_id
INNER JOIN sys.columns C ON C.object_id = O.object_id
AND C.column_id = dp.minor_id
INNER JOIN sys.database_principals P ON P.principal_id = dp.grantee_principal_id
WHERE P.name='sa1'

--关于Delete 和Insert则不能进行列级别的权限控制,因为它们操作的最小单位是一条记录