Link to home
Start Free TrialLog in
Avatar of Trancos
TrancosFlag for United States of America

asked on

How do I extract Stored procedures programatically

Experts,

As a background, I'm fairly new (or obsolete) on DB development. I'm using C# to build an application that would extract the list stored procedures found in a particular DB (SQL Sever 2K5). The question is, how do I do it programatically? and by doing so, will I be able to see the whole stored procedure, names and paramenters, or only names?
Thanks
Avatar of YZlat
YZlat
Flag of United States of America image

to select the names of stored procedures in a particular database, use the following sql:

select name from sysobjects where type='P'
to select only user-defined stored procedures, use

select name from sysobjects where type='P' and category=0
There are a few ways that may work to perform this task, and, depending on which way you use, you may be able to get parameter information.  One of the easiest method to get both the stored procedures and the parameters would be to use the query provided by YZlat above.  Then, using the information in that query, you can use an ADO command object, set it up to access each individual stored procedure, then use a Parameters.Refresh action to load the parameters into the .Parameters collection.  Spin through the collection, and you have all of the parameters (and their in/out status)
oh, and to retrieve the text of the stored procedure, use

SELECT text    
FROM syscomments    
WHERE id = (SELECT id FROM sysobjects WHERE name = 'YOUR STORED PROC NAME HERE')    
ORDER BY colid
ASKER CERTIFIED SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can also use SQLDMO.

Here is an example in .ASP

http://www.developerfusion.co.uk/show/1820/4/
Avatar of Trancos

ASKER

YZLat.
I'll try your solution and get back later with the results.
Thanks all for your help.
Avatar of Trancos

ASKER


OK,

code works perfectly, however I'm unable to follow bhess1 recommendation to obtain the list of parameters. I have the code shown below, but I'm unable to call a refresh or update or whatever method would allow me to obtaint the parameters for the stored procedure:

SqlConnection tempConnection=GetConnection(strConn);
SqlCommand adoCmd = new SqlCommand(proc_name, tempConnection);
adoCmd.CommandType = CommandType.StoredProcedure;

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Trancos

ASKER

Ignorance is expensive. Thanks guys for enlightening the path.