1

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 ?

bummi
  • 27,123
  • 14
  • 62
  • 101
mmarques
  • 625
  • 3
  • 9
  • 27

1 Answers1

1

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

podiluska
  • 50,950
  • 7
  • 98
  • 104
  • I already try it and doesnt work. I need to pass a text paremether to sp_helptext – mmarques Oct 18 '13 at 09:09
  • That solution will works fine but, i have a long query and the execution of sp_helptext is somewhere in the midle of the query, i cant execute it separatly – mmarques Oct 18 '13 at 09:13
  • All right, i made a few corrections to my query and your first answer works fine with it. Thanks podiluska – mmarques Oct 18 '13 at 09:25