Solved

How do I extract Stored procedures programatically

Posted on 2007-11-26
10
2,178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

726 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