一个完成以下工作的存储过程:当我传递一个列名时,我应该得到一个包含该列的所有表的列表,以及一个使用所有数据库中这些表的所有存储过程的列表
问问题
555 次
1 回答
0
DECLARE @rolename AS VARCHAR(200) = 'testx_table'
DECLARE @string1 VARCHAR(2000) = '',
@string2 VARCHAR(2000) = '',
@string3 VARCHAR(2000) = '',
@string4 VARCHAR(2000) = ''
IF @rolename = 'testx_table'
GOTO table_op
IF @rolename = 'testx_proc'
GOTO proc_op
TABLE_OP:
BEGIN
DECLARE @catalog VARCHAR(500),
@schema VARCHAR(50),
@tablename VARCHAR(500),
@tabletype VARCHAR(50)
DECLARE crs_tables CURSOR FOR
SELECT table_catalog,
table_schema,
table_name,
table_type
FROM [INFORMATION_SCHEMA].tables
WHERE table_schema = 'DBO'
OPEN crs_tables
FETCH next FROM crs_tables INTO @catalog, @schema, @tablename,
@tabletype
SELECT @catalog = table_catalog,
@schema = table_schema,
@tablename = table_name,
@tabletype = table_type
FROM [INFORMATION_SCHEMA].tables
WHERE table_schema = 'DBO'
AND @catalog = table_catalog
AND @schema = table_schema
AND @tablename = table_name
AND @tabletype = table_type
SET @string1 = 'GRANT INSERT ON ' + '[dbo]' + '.' + @tablename
+ ' TO ' + @rolename
SET @string2 = 'GRANT DELETE ON ' + '[dbo]' + '.' + @tablename
+ ' TO ' + @rolename
SET @string3 = 'GRANT UPDATE ON ' + '[dbo]' + '.' + @tablename
+ ' TO ' + @rolename
SET @string4 = 'GRANT SELECT ON ' + '[dbo]' + '.' + @tablename
+ ' TO ' + @rolename
EXEC(@string1)
EXEC(@string2)
EXEC(@string3)
EXEC(@string4)
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @catalog = table_catalog,
@schema = table_schema,
@tablename = table_name,
@tabletype = table_type
FROM [INFORMATION_SCHEMA].tables
WHERE table_schema = 'DBO'
AND @catalog = table_catalog
AND @schema = table_schema
AND @tablename = table_name
AND @tabletype = table_type
FETCH next FROM crs_tables INTO @catalog, @schema, @tablename,
@tabletype
SET @string1 = 'GRANT INSERT ON ' + '[dbo]' + '.' + @tablename
+ ' TO ' + @rolename
SET @string2 = 'GRANT DELETE ON ' + '[dbo]' + '.' + @tablename
+ ' TO ' + @rolename
SET @string3 = 'GRANT UPDATE ON ' + '[dbo]' + '.' + @tablename
+ ' TO ' + @rolename
SET @string4 = 'GRANT SELECT ON ' + '[dbo]' + '.' + @tablename
+ ' TO ' + @rolename
EXEC(@string1)
EXEC(@string2)
EXEC(@string3)
EXEC(@string4)
END
CLOSE crs_tables
DEALLOCATE crs_tables
GOTO EXIT
END
/* */
PROC_OP:
BEGIN
DECLARE @p_catalog VARCHAR(500),
@p_schema VARCHAR(50),
@p_name VARCHAR(500),
@p_type VARCHAR(50),
@xtype VARCHAR(50)
DECLARE crs_routines CURSOR FOR
SELECT r.specific_catalog,
r.specific_schema,
r.specific_name,
r.routine_type,
s.xtype
FROM [INFORMATION_SCHEMA].routines r
INNER JOIN sysobjects s
ON s.NAME = r.specific_name
--and s.xtype = 'FN'
WHERE r.specific_schema = 'DBO'
OPEN crs_routines
FETCH next FROM crs_routines INTO @p_catalog, @p_schema, @p_name,
@p_type,
@xtype
SELECT @p_catalog = r.specific_catalog,
@p_schema = r.specific_schema,
@p_name = r.specific_name,
@p_type = r.routine_type,
@xtype = s.xtype
FROM [INFORMATION_SCHEMA].routines r
INNER JOIN sysobjects s
ON s.NAME = r.specific_name
--and s.xtype = 'FN'
WHERE r.specific_schema = 'DBO'
AND @p_catalog = r.specific_catalog
AND @p_schema = r.specific_schema
AND @p_name = r.routine_name
AND @p_type = r.routine_type
SET @string1 = 'GRANT ' + CASE WHEN @p_type = 'PROCEDURE' OR @xtype =
'FN'
THEN ' EXECUTE '
ELSE ' SELECT' END + ' ON ' + '[dbo]' + '.' + '[' +
@p_name +
']'
+ ' TO ' + @rolename
SET @string2 = 'GRANT ' + ' VIEW DEFINITION ' + ' ON ' + '[dbo]'
+ '.' + '[' + @p_name + ']' + ' TO ' + @rolename
PRINT @string1 + ' / ' + @p_type
EXEC(@string1)
EXEC(@string2)
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @p_catalog = r.specific_catalog,
@p_schema = r.specific_schema,
@p_name = r.specific_name,
@p_type = r.routine_type,
@xtype = s.xtype
FROM [INFORMATION_SCHEMA].routines r
INNER JOIN sysobjects s
ON s.NAME = r.specific_name
--and s.xtype = 'FN'
WHERE r.specific_schema = 'DBO'
AND @p_catalog = r.specific_catalog
AND @p_schema = r.specific_schema
AND @p_name = r.routine_name
AND @p_type = r.routine_type
FETCH next FROM crs_routines INTO @p_catalog, @p_schema, @p_name
,
@p_type,
@xtype
SET @string1 = 'GRANT ' + CASE WHEN @p_type = 'PROCEDURE' OR
@xtype =
'FN'
THEN ' EXECUTE '
ELSE ' SELECT' END + ' ON ' + '[dbo]' + '.' + '['
+
@p_name
+
']'
+ ' TO ' + @rolename
SET @string2 = 'GRANT ' + ' VIEW DEFINITION ' + ' ON ' + '[dbo]'
+ '.' + '[' + @p_name + ']' + ' TO ' + @rolename
PRINT @string1 + ' / ' + @p_type
PRINT @string2 + ' / ' + @p_type
EXEC(@string1)
EXEC(@string2)
END
CLOSE crs_routines
DEALLOCATE crs_routines
GOTO EXIT
END
于 2018-08-17T05:29:30.337 回答