5

任何人都可以建议在所有数据库中检索所有存储过程的列表及其模式名称的方法吗?感谢您的输入!

4

4 回答 4

12
CREATE TABLE #x(db SYSNAME, s SYSNAME, p SYSNAME);

DECLARE @sql NVARCHAR(MAX) = N'';

SELECT @sql += N'INSERT #x SELECT ''' + name + ''',s.name, p.name
  FROM ' + QUOTENAME(name) + '.sys.schemas AS s
  INNER JOIN ' + QUOTENAME(name) + '.sys.procedures AS p
  ON p.schema_id = s.schema_id;
' FROM sys.databases WHERE database_id > 4

EXEC sp_executesql @sql;

SELECT db,s,p FROM #x ORDER BY db,s,p;

DROP TABLE #x;
于 2013-03-31T02:45:33.940 回答
4

对于所有数据库,搜索用户定义的存储过程、视图、函数和表值函数,以下可能会有所帮助(基于上述 Habo):

declare @Sprocs as Table (DBName VarChar(64), SchemaName varchar (128), 
    StoredProcName VarChar(128), 
    type varchar(10), type_desc varchar(128), 
    create_date datetime, modify_date datetime, is_MS_Shipped int)
insert into @Sprocs
    execute sp_MSforeachdb N'use ?; select ''?'', 
        s.name as SchemaName, o.name as StoredProcName, 
        type, type_desc, create_date, modify_date, is_MS_Shipped
        from sys.all_objects as o inner join sys.schemas as s 
            on s.schema_id = o.schema_id 
        where o.type in (''P'', ''V'', ''PC'', ''FN'', ''TF'')
            and is_ms_shipped = 0'

select * from @Sprocs
where DBName not in ('master', 'tempdb', 'model', 'msdb', 'dba') 
order by DBName, SchemaName, type, StoredProcName

添加“U”以获取用户生成的表。

于 2014-06-05T16:45:30.190 回答
2

对于单个数据库:

select s.name + '.' + p.name as sproc_name_with_schema
  from sys.procedures as p inner join
    sys.schemas as s on s.schema_id = p.schema_id

所有数据库都需要它吗?你的问题有点不清楚。

declare @Sprocs as Table ( DBName VarChar(64), SprocName VarChar(128) )
insert into @Sprocs
  execute sp_MSforeachdb N'use ?; select ''?'', s.name + ''.'' + o.name from sys.all_objects as o inner join sys.schemas as s on s.schema_id = o.schema_id where o.type = ''P'''
select * from @Sprocs
于 2013-03-31T00:51:36.317 回答
1

没有临时表和插入的第一个答案的替代方法:

declare @sql varchar(max) = '';
select 
    @sql += ' ' +
    'union all ' +
    'select ''' +
        dat.name + ''' as bas, b.name as sch, a.name as pro ' +
    'from ' +
        '[' + dat.name + '].[sys].procedures as a ' +
        'inner join [' + dat.name + '].[sys].schemas as b on a.schema_id = b.schema_id'
from sys.databases as dat;
set @sql = RIGHT(@sql, LEN(@sql) - 11);
exec(@sql);
print @sql;
于 2014-08-02T19:32:33.400 回答