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 |
ashishmishra81
Starting Member
5 Posts |
Posted - 2008-04-21 : 03:00:21
|
I am try to build a Stored procedure to insert into a table whose name is contain in variable @tname, other field are just variable to supply value to table fields,i have implement simple type of SP for "SELECT" AND "CREATE" ,but this procedure does not work, please help meCREATE proc insert_mess@tname nvarchar(50),@mass nvarchar(250),@to_id int,@form_id intasexec ('insert into ['+ @tname +'] ( [message],to_id,form_id)values ('+ @mass +',' +@to_id + ','+@form_id +') ')Edit/Delete Messagesonu |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-21 : 03:57:01
|
Shouldn't "form_id" be "FROM_ID"?Also @mass variable need to be enclosed in single quotes.CREATE proc insert_mess@tname nvarchar(50),@mass nvarchar(250),@to_id int,@form_id intasexec ('insert into ['+ @tname +'] ( [message],to_id,from_id)values ('+ quotename(@mass, '''') +',' +@to_id + ','+@form_id +') ') E 12°55'05.25"N 56°04'39.16" |
 |
|
ashishmishra81
Starting Member
5 Posts |
Posted - 2008-04-21 : 04:09:12
|
thanks for replay mebut it does not work,'form'is just a column field name in tablequote: Originally posted by Peso Shouldn't "form_id" be "FROM_ID"?Also @mass variable need to be enclosed in single quotes.CREATE proc insert_mess@tname nvarchar(50),@mass nvarchar(250),@to_id int,@form_id intasexec ('insert into ['+ @tname +'] ( [message],to_id,from_id)values ('+ quotename(@mass, '''') +',' +@to_id + ','+@form_id +') ') E 12°55'05.25"N 56°04'39.16"
sonu |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-21 : 04:33:25
|
Ok. So just ignore the first suggestion.What happens when you tried the second suggestion.Also explain why it "doesn't work". E 12°55'05.25"N 56°04'39.16" |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-04-21 : 05:39:44
|
You need to use multiple single quotes if the value is a stringAlso make sure to read thiswww.sommarskog.se/dynamic_sql.htmlMadhivananFailing to plan is Planning to fail |
 |
|
ashishmishra81
Starting Member
5 Posts |
Posted - 2008-04-22 : 01:48:22
|
thanks friend it now working if i used multiple single quotes in string field which @mass here, if i used three single qoutes nested each other ,then only it worksCREATE proc insert_mess@tname nvarchar(50),@mass nvarchar(250),@to_id int,@form_id intasexec ('insert into ['+ @tname +'] ( [message],to_id,form_id)values ('''+@mass+''',' +@to_id + ','+@form_id +') ')[quote]Originally posted by madhivanan You need to use multiple single quotes if the value is a stringAlso make sure to read thiswww.sommarskog.se/dynamic_sql.htmlMadhivanansonu |
 |
|
dass05555
Yak Posting Veteran
55 Posts |
Posted - 2008-04-22 : 02:56:02
|
hi friend,chk it out this ,i think it would satisfy u...drop proc insert_messgoCREATE proc insert_mess@tname nvarchar(50),@mass nvarchar(250),@to_id int,@form_id intasexec ('insert into ['+ @tname +'] ( [message],to_id,form_id) values ('+@mass+','+@to_id+','+@form_id+')') |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-04-22 : 03:23:30
|
quote: Originally posted by dass05555 hi friend,chk it out this ,i think it would satisfy u...drop proc insert_messgoCREATE proc insert_mess@tname nvarchar(50),@mass nvarchar(250),@to_id int,@form_id intasexec ('insert into ['+ @tname +'] ( [message],to_id,form_id) values ('+@mass+','+@to_id+','+@form_id+')')
Read OP's previous replyMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|