I'm trying to pass a parameter to a sql query (in c#) but the parameter is inside a text block:
SqlCommand cmd = new SqlCommand(@"EXEC sp_helptext N'dbo.@spname';");
cmd.Parameters.AddWithValue("@spname", "StoredProcedureName");
If I use the stored procedure name directly on query it works fine, but if I pass the parameter it does not work.
Does anyone know how to do this correcty ?
You could do
SqlCommand cmd = new SqlCommand("EXEC sp_helptext @spname");
cmd.Parameters.AddWithValue("@spname", "dbo.StoredProcedureName");
But using
SqlCommand cmd = new SqlCommand("sp_helptext");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@objname", "dbo.StoredProcedureName");
is a better choice