Solved

How do I extract Stored procedures programatically

Posted on 2007-11-26
10
2,167 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:Trancos
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 35

Expert Comment

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

select name from sysobjects where type='P'
0
 
LVL 35

Expert Comment

by:YZlat
ID: 20353053
to select only user-defined stored procedures, use

select name from sysobjects where type='P' and category=0
0
 
LVL 32

Expert Comment

by:bhess1
ID: 20353113
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)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 35

Expert Comment

by:YZlat
ID: 20353160
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
0
 
LVL 35

Accepted Solution

by:
YZlat earned 350 total points
ID: 20353265
copy those 2 functions to your code:

public static SqlConnection GetConnection(string strConn)
            {
                  SqlConnection conn=null;
                  try
                  {
                        conn = new SqlConnection(strConn);
                        conn.Open();
                  }
                  catch (SqlException ex)
                  {
                        Console.Write("SQL ERROR: " + ex.Message);
                  }
                  catch (Exception ex)
                  {
                        Console.Write("ERROR: " + ex.Message);
                  }
                  return conn;
            }

            public static DataSet GetDataSet(string strConn, string query)
            {
                  SqlConnection conn=null;

                  DataSet dset = new DataSet();
                  try
                  {
                        conn = GetConnection(strConn);
                        SqlDataAdapter da = new SqlDataAdapter(query, conn);
                        da.SelectCommand.CommandTimeout = 0;
                        da.Fill(dset);
                  }
                  catch (SqlException ex)
                  {
                        Console.Write("SQL ERROR: " + ex.Message);
                  }
                  catch (Exception ex)
                  {
                        Console.Write("ERROR: " + ex.Message);
                  }
                  finally
                  {
                        if (conn.State == ConnectionState.Open)
                        {
                              conn.Close();
                        }
                  }
                  return dset;
            }


then call GetDataSet:

DataSet ds=new DataSet();
string query="select name from sysobjects where type='P' and category=0";
string strConn="YOUR DATABASE CONNECTION STRING HERE";
ds=GetDataSet(strConn,query);

if (ds.Tables.Count > 0)
{
      if(ds.Tables[0].Rows.Count > 0)
      {
            for(int i=0; i<=ds.Tables[0].Rows.Count -1;i++)
            {
                  string proc_name=ds.Tables[0].Rows[i].ItemArray[0].ToString();
                  //here get stored proc text using procedure name
                  string sql="SELECT text FROM syscomments WHERE id=(SELECT id FROM sysobjects WHERE name = '" + proc_name +"')";
                  DataSet ds2=new DataSet();
                  ds2=GetDataSet(strConn, sql);

                  //your code here
            }//end for
      }//end if
}//end if
0
 
LVL 12

Expert Comment

by:kselvia
ID: 20353964
You can also use SQLDMO.

Here is an example in .ASP

http://www.developerfusion.co.uk/show/1820/4/
0
 
LVL 1

Author Comment

by:Trancos
ID: 20354199
YZLat.
I'll try your solution and get back later with the results.
Thanks all for your help.
0
 
LVL 1

Author Comment

by:Trancos
ID: 20354766

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

0
 
LVL 32

Assisted Solution

by:bhess1
bhess1 earned 150 total points
ID: 20354837
Try adding this instead - it has changed in .Net, and it took me a bit of tracking to find the new syntax

SqlCommandBuilder.DeriveParameters(adoCmd);

This should populate the .Parameters collection of the SqlCommand object
0
 
LVL 1

Author Closing Comment

by:Trancos
ID: 31411074
Ignorance is expensive. Thanks guys for enlightening the path.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question