If i've understood correctly, set up a SqlCommand
object with the following
using (SqlConnection con = new SqlConnection ("Connection String Here"))
{
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "sp_helptext @procName";
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("procName", "Name Of Stored Proc Here");
con.Open();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
/*
You will get the CREATE PROC text here
Do what you need to with it. For example, write
to a .sql file
*/
}
}
}
}
This will return the text of a stored procedure as a CREATE PROC
statement, in a result set where each row of the resultset is a line of the stored procedure.
To create a DROP PROC
statement, simply write out
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'procName') AND type in (N'P', N'PC'))
DROP PROC procName
to a .sql
file.