|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, February 27, 2013 1:38 PM
Points: 370,
Visits: 670
|
|
I run the following script to script all stored procs for all databases on a server, but I would also like to display the database names for these stored procs. How do I do that?
EXECUTE sp_msforeachdb 'select s.*, p.* from ?.sys.sql_modules s inner join ?.sys.procedures p on s.object_id = p.object_id'
Thnx.
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, February 27, 2013 1:38 PM
Points: 370,
Visits: 670
|
|
I found it myself and it is:
EXECUTE sp_msforeachdb 'select specific_catalog, specific_name, routine_definition from information_schema.routines'
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, February 27, 2013 1:38 PM
Points: 370,
Visits: 670
|
|
Made a mistake. The last one did not work. What works is the following, but I also need database name with it. ANY IDEAS???
EXECUTE sp_msforeachdb 'select s.*, p.* from ?.sys.sql_modules s inner join ?.sys.procedures p on s.object_id = p.object_id'
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 3:52 AM
Points: 11,613,
Visits: 27,670
|
|
the OBJECT_NAME function has a second , optional parameter for the db_id for exactly this reason; this works in 2005 and above;
sp_msforeachdb ' select object_name(s.object_id,db_id(''?'')), s.*, p.* from ?.sys.sql_modules s inner join ?.sys.procedures p on s.object_id = p.object_id'
the schema_name() function does not have the optional parameter in 2005, so if you need the schema (assuming you have otehrs than dbo) that might take some more work.
Lowell
--There is no spoon, and there's no default ORDER BY in sql server either. Actually, Common Sense is so rare, it should be considered a Superpower. --my son
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, February 27, 2013 1:38 PM
Points: 370,
Visits: 670
|
|
| but that does not give me database name.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 3:50 AM
Points: 1,618,
Visits: 20,898
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 5:44 PM
Points: 1,289,
Visits: 3,857
|
|
ramadesai108 (12/9/2010) I found it myself and it is:
EXECUTE sp_msforeachdb 'select specific_catalog, specific_name, routine_definition from information_schema.routines'
It does work, you just missed the "?"
EXECUTE sp_msforeachdb 'select specific_catalog, specific_name, routine_definition from ?.information_schema.routines'
MM
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 5:44 PM
Points: 1,289,
Visits: 3,857
|
|
And just to show how it can be done with the first query:
EXECUTE sp_msforeachdb 'use ?;select DB_NAME(),s.*, p.* from sys.sql_modules s inner join sys.procedures p on s.object_id = p.object_id'
MM
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 3:52 AM
Points: 11,613,
Visits: 27,670
|
|
ramadesai108 (12/9/2010) but that does not give me database name.
you forgot how the proc works! the question mark is substituted with the dbname! db_name() function will keep returning master or whatever db you started the cursor in.
sp_msforeachdb ' select ''?'' AS DBName, object_name(s.object_id,db_id(''?'')), s.*, p.* from ?.sys.sql_modules s inner join ?.sys.procedures p on s.object_id = p.object_id'
Lowell
--There is no spoon, and there's no default ORDER BY in sql server either. Actually, Common Sense is so rare, it should be considered a Superpower. --my son
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, February 27, 2013 1:38 PM
Points: 370,
Visits: 670
|
|
|
|
|