次の作業を行う単一のストアドプロシージャ:列名を渡すと、その列を含むすべてのテーブルのリストと、すべてのデータベースからそれらのテーブルを使用しているすべてのストアドプロシージャのリストを取得する必要があります
質問する
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 に答える