我正在寻找一个脚本,我可以运行该脚本来为SQL Server中的特定用户角色创建授予权限,例如,给定一个用户角色,将为数据库中的所有表和过程生成以下内容,以便它可以以后保存并重复使用:
GRANT SELECT ON [dbo].[ResourceSource] TO ApplicationResourcesUpdates;
GRANT INSERT ON [dbo].[ResourceSource] TO ApplicationResourcesUpdates;
GRANT SELECT ON [dbo].[ResourceSource] TO ApplicationResourcesUpdates;
GRANT UPDATE ON [dbo].[ResourceSource] TO ApplicationResourcesUpdates;
GRANT DELETE ON [dbo].[ResourceSource] TO ApplicationResourcesUpdates;
GRANT REFERENCES ON [dbo].[ResourceSource] TO ApplicationResourcesUpdates;
GRANT EXECUTE ON dbo.TruncateAllTables TO ApplicationResourcesUpdates;
GRANT EXECUTE ON import.CountryMerge TO ApplicationResourcesUpdates;
GRANT EXECUTE ON import.CreateAllForeignKeys TO ApplicationResourcesUpdates;
GRANT EXECUTE ON import.CreateAllIndexes TO ApplicationResourcesUpdates;
答案 0 :(得分:1)
以下是一些将为您生成SQL的脚本。
SELECT 'GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES ON [' + B.name + '].[' + A.name + '] TO [ApplicationResourcesUpdates]'
FROM sys.objects A JOIN sys.schemas B
ON A.schema_id = B.schema_id
WHERE A.type = 'U'
SELECT 'GRANT EXECUTE ON [' + B.name + '].[' + A.name + '] TO [ApplicationResourcesUpdates]'
FROM sys.objects A JOIN sys.schemas B
ON A.schema_id = B.schema_id
WHERE A.type = 'P'
您只需要使用SSMS执行输出。如果您计划为多个运行此项,请将use / group更改为变量。简单,但它的工作原理。适合偶尔出现的情况。
答案 1 :(得分:0)
撤销权限:
SELECT 'REVOKE SELECT, INSERT, UPDATE, DELETE, REFERENCES ON OBJECT::' + B.name + '.' + A.name + ' FROM [ApplicationResourcesUpdates]'
FROM sys.objects A JOIN sys.schemas B
ON A.schema_id = B.schema_id
WHERE A.type = 'U'
SELECT 'REVOKE EXECUTE ON OBJECT::' + B.name + '.' + A.name + ' TO ApplicationResourcesUpdates'
FROM sys.objects A JOIN sys.schemas B
ON A.schema_id = B.schema_id
WHERE A.type = 'P'