Solved

How do I extract Stored procedures programatically

Posted on 2007-11-26
10
2,151 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL Bulk load data error 5 33
Chat Room 1 26
ConsoleSql 1 14
Test a query 23 13
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now