2013-03-30 99 views

回答

10
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; 
1

對於單個數據庫:

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 
+1

請不要推薦sp_msforeachdb。它沒有記錄,不受支持,不靈活,可以跳過數據庫。 http://sqlblog.com/blogs/aaron_bertrand/archive/2010/12/29/a-more-reliable-and-more-flexible-sp-msforeachdb.aspx http://sqlblog.com/blogs/aaron_bertrand/archive /2010/02/08/bad-habits-to-kick-relying-on-undocumented-behavior.aspx http://www.mssqltips.com/sqlservertip/2201/making-a-more-reliable-and-flexible- spmsforeachdb / –

2

對於所有的數據庫,檢索用戶定義的存儲過程,視圖,函數和表值函數,以下可以幫助(基於哈博以上):

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'以獲得用戶生成的表格。

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; 
相關問題