SQL 服务器克隆存储过程,包括授权
SQL Server clone stored procedure including grants
我可以克隆一个包含新名称授权的存储过程吗?
create procedure y as clone x
也许你可以试试这个SQL服务器脚本:
Declare @sql_create NVarchar(MAX)
Declare @sql_grants NVarchar(MAX)
Declare @sp_name Varchar(MAX)
Declare @sp_new_name Varchar(MAX)
declare @object_id int
set @sp_name = '<YourSPName>'
set @sp_new_name = '<YourSPNewName>'
-- Get SP...
Select @object_id = o.object_id,
@sql_create = Replace(definition, @sp_name, @sp_new_name)
From sys.sql_modules m
inner join sys.objects o on m.object_id = o.object_id
where m.definition like '%' + @sp_name +'%'
and o.type = 'P'
-- Get all permissions
SELECT @sql_grants = COALESCE(@sql_grants + '; ', '') +
(
dp.state_desc + ' ' +
dp.permission_name collate latin1_general_cs_as +
' ON ' + '[' + s.name + ']' + '.' + '[' + Replace(o.name , @sp_name, @sp_new_name) + ']' +
' TO ' + '[' + dpr.name + ']'
)
FROM sys.database_permissions AS dp
INNER JOIN sys.objects AS o ON dp.major_id=o.object_id
INNER JOIN sys.schemas AS s ON o.schema_id = s.schema_id
INNER JOIN sys.database_principals AS dpr ON dp.grantee_principal_id=dpr.principal_id
WHERE dpr.name NOT IN ('public','guest')
and o.object_id = @object_id
-- Optional
Print (@sql_create) --Note: will cut off at TextWidth!
Print (@sql_grants)
-- Execute queries...
EXEC (@sql_create)
EXEC (@sql_grants)
希望对你有帮助!
我可以克隆一个包含新名称授权的存储过程吗?
create procedure y as clone x
也许你可以试试这个SQL服务器脚本:
Declare @sql_create NVarchar(MAX)
Declare @sql_grants NVarchar(MAX)
Declare @sp_name Varchar(MAX)
Declare @sp_new_name Varchar(MAX)
declare @object_id int
set @sp_name = '<YourSPName>'
set @sp_new_name = '<YourSPNewName>'
-- Get SP...
Select @object_id = o.object_id,
@sql_create = Replace(definition, @sp_name, @sp_new_name)
From sys.sql_modules m
inner join sys.objects o on m.object_id = o.object_id
where m.definition like '%' + @sp_name +'%'
and o.type = 'P'
-- Get all permissions
SELECT @sql_grants = COALESCE(@sql_grants + '; ', '') +
(
dp.state_desc + ' ' +
dp.permission_name collate latin1_general_cs_as +
' ON ' + '[' + s.name + ']' + '.' + '[' + Replace(o.name , @sp_name, @sp_new_name) + ']' +
' TO ' + '[' + dpr.name + ']'
)
FROM sys.database_permissions AS dp
INNER JOIN sys.objects AS o ON dp.major_id=o.object_id
INNER JOIN sys.schemas AS s ON o.schema_id = s.schema_id
INNER JOIN sys.database_principals AS dpr ON dp.grantee_principal_id=dpr.principal_id
WHERE dpr.name NOT IN ('public','guest')
and o.object_id = @object_id
-- Optional
Print (@sql_create) --Note: will cut off at TextWidth!
Print (@sql_grants)
-- Execute queries...
EXEC (@sql_create)
EXEC (@sql_grants)
希望对你有帮助!