Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
imaraz
Starting Member
2 Posts |
Posted - 2008-07-12 : 23:38:03
|
| Hi everyone, I have created a general stored procedure that takes two parameters one as Table name and the other as Field name to be able to use it for several tables with same data structure.-----------------------------------------------------USE [Users]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE PROCEDURE [dbo].[spInsertIntoModList] @TableName nvarchar(64), @Name nvarchar(64)ASBEGIN SET NOCOUNT ON; DECLARE @cmd AS NVARCHAR(1000) SET @cmd = N'INSERT INTO ' + @TableName + ' (Name) VALUES (''' + @Name + ''')' EXEC @cmdEND-----------------------------------------------------I call this stored procedure using a C# code but it throws an exception of 'Could not find stored procedure 'INSERT INTO Applications (Name) VALUES ('App 11')''(The parameters sent from C# code was 'Applications' for @TableName and 'App 11' for @Name)C# code: public long ExecuteSP(string pSPName, ref SqlParameterCollection pParams) { try { OpenConnection(); } catch { return -1; } SqlParameter[] lParamArray = new SqlParameter[pParams.Count]; pParams.CopyTo(lParamArray, 0); SqlCommand lCommand = new SqlCommand(pSPName, mConnection); lCommand.CommandType = CommandType.StoredProcedure; for (int i = 0; i < pParams.Count; i++) lCommand.Parameters.AddWithValue(lParamArray[i].ParameterName.ToString(),lParamArray[i].Value.ToString()); try { lCommand.ExecuteNonQuery(); CloseConnection(); } catch { CloseConnection(); return 0; } return 1; }Any ideas what I am doing wrong?Thanks :) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-13 : 02:30:04
|
quote: Originally posted by imaraz Hi everyone, I have created a general stored procedure that takes two parameters one as Table name and the other as Field name to be able to use it for several tables with same data structure.-----------------------------------------------------USE [Users]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE PROCEDURE [dbo].[spInsertIntoModList] @TableName nvarchar(64), @Name nvarchar(64)ASBEGIN SET NOCOUNT ON; DECLARE @cmd AS NVARCHAR(1000) SET @cmd = N'INSERT INTO ' + @TableName + ' (Name) VALUES (''' + @Name + ''')' EXEC (@cmd)END-----------------------------------------------------I call this stored procedure using a C# code but it throws an exception of 'Could not find stored procedure 'INSERT INTO Applications (Name) VALUES ('App 11')''(The parameters sent from C# code was 'Applications' for @TableName and 'App 11' for @Name)C# code: public long ExecuteSP(string pSPName, ref SqlParameterCollection pParams) { try { OpenConnection(); } catch { return -1; } SqlParameter[] lParamArray = new SqlParameter[pParams.Count]; pParams.CopyTo(lParamArray, 0); SqlCommand lCommand = new SqlCommand(pSPName, mConnection); lCommand.CommandType = CommandType.StoredProcedure; for (int i = 0; i < pParams.Count; i++) lCommand.Parameters.AddWithValue(lParamArray[i].ParameterName.ToString(),lParamArray[i].Value.ToString()); try { lCommand.ExecuteNonQuery(); CloseConnection(); } catch { CloseConnection(); return 0; } return 1; }Any ideas what I am doing wrong?Thanks :)
put a braces around the variable in EXEC |
 |
|
|
imaraz
Starting Member
2 Posts |
Posted - 2008-07-13 : 13:47:33
|
| Wow it worked! Thanks a lot man.. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-14 : 01:08:36
|
quote: Originally posted by imaraz Wow it worked! Thanks a lot man..
You're welcome |
 |
|
|
|
|
|
|
|