Click here to Skip to main content
16,021,417 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to get the stored procedure script in the C#

SQL
SELECT text
FROM syscomments
WHERE id = (SELECT id FROM sysobjects WHERE name = 'Tablename')
ORDER BY colid

we can get the whole script but i need how can we take only the parameters script... we need to call the script in .net then we need to add some parameters to that existing parameters.

how to get script of parameters and need to add new parameter to ths script in c#

eg:

//starting parametees
@name varchar(20),
.
.
.
@result varchar(20)
//ending parameters
Posted
Updated 31-Jul-13 1:20am
v6
Comments
Sergey Alexandrovich Kryukov 31-Jul-13 1:13am    
What do you mean by "get"? How it can be C#?
—SA
Maciej Los 31-Jul-13 1:58am    
Please, elaborate... What do you want to achieve? Do you want to get the code of SP or data returned by SP?

 
Share this answer
 
Run the following two queries from C# you will get the desired result
run the following script you will get the definition of st pro
SQL
SELECT definition FROM sys.sql_modules 
WHERE [object_id] = OBJECT_ID('dbo.procedurename');

and you can get the params by running this query
SQL
SELECT name, system_type_id, max_length, precision, scale
FROM sys.parameters
WHERE [object_id] = OBJECT_ID('dbo.procedurename');
 
Share this answer
 
private string AddColumnInStoredProcedure(String newColumn)
{
try
{
connStringForDB = ConfigurationManager.ConnectionStrings["Conv3DBConnectionString"].ConnectionString;
sqlconn = new SqlConnection(connStringForDB);
if (sqlconn.State == 0)
sqlconn.Open();
string strSQL = "SELECT text FROM syscomments WHERE id = (SELECT id FROM sysobjects WHERE name = 'InsertRequestForexcel_temp_sp') ORDER BY colid";
DataSet ds = new DataSet();
sqlconn.Open();
SqlDataAdapter da = new SqlDataAdapter(strSQL, sqlconn);
//sqlconn.Close();
da.Fill(ds);
StringBuilder sb = new StringBuilder();
foreach (DataRow item in ds.Tables[0].Rows)
{
string Str = item.ItemArray[0].ToString();
string strNew = string.Empty;
string newColumn = "newCOl";
if (Str.Contains("--(EOP)"))
{
int startIndex = Str.IndexOf("--(EOP)");
strNew = Str.Substring(0, startIndex) + "@" + newColumn + " varchar(10), " + Str.Substring(startIndex);
}
if (Str.Contains("--(ICN)") && strNew == string.Empty)
{
int startIndex = Str.IndexOf("--(ICN)");
strNew = Str.Substring(0, startIndex) + "," + newColumn + " " + Str.Substring(startIndex);//INSERT COLUMN NAME
}
else if (strNew.Contains("--(ICN)") && strNew != string.Empty)
{
int startIndex = strNew.IndexOf("--(ICN)");
strNew = strNew.Substring(0, startIndex) + "," + newColumn + " " + strNew.Substring(startIndex);//INSERT COLUMN NAME
}
if (Str.Contains("--(ICV)") && strNew == string.Empty)
{
int startIndex = Str.IndexOf("--(ICV)");
strNew = Str.Substring(0, startIndex) + ",@" + newColumn + " " + Str.Substring(startIndex);//INSERT COLUMN VALUE(PARAMETER NAME)
}
else if (strNew.Contains("--(ICV)") && strNew != string.Empty)
{
int startIndex = strNew.IndexOf("--(ICV)");
strNew = strNew.Substring(0, startIndex) + ",@" + newColumn + " " + strNew.Substring(startIndex);//INSERT COLUMN NAME
}
if (strNew != "")
{
sb.Append(strNew);
//strList.Add(strNew);
}
else
{
sb.Append(Str);
//strList.Add(Str);
}


}

//SqlCommand cmd = new SqlCommand(sb.ToString(),sqlconn);
SqlCommand cmd = new SqlCommand("Alter " + sb.ToString().Substring(6), sqlconn);
cmd.ExecuteNonQuery();



}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900