Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Executing an insert statement with variables

Status
Not open for further replies.

HARLEY12

Programmer
Jan 12, 2005
9
US
Within a stored procedure, I am trying to execute an insert statement which contains variables. The code for the insert is below:

select @insertstatement = * 'insert into KbText_' + @appid select ''Yes'', ' + @vtext1 + @vtext2 + @vtext3 + @vtext4 + @vtext5 + @vtext6 + ' TextData ', + @appid
exec(@insertstatement)

I keep getting the following error:

Error 141: A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.

How can I execute an insert statement which has variables?

Thanks!
 
you need to construct the statement something like :
Code:
declare @insertstatement nvarchar(8000)
 set @insertstatement = 'insert into KbText_'
 + @appid+'(select  ''Yes'', ' + @vtext1+',' + @vtext2+',' +
 @vtext3+',' + @vtext4+',' + @vtext5+',' + @vtext6 +
 '  TextData ', +  @appid
PRINT @insertstatement
exec(@insertstatement)

[bandito] [blue]DBomrrsm[/blue] [bandito]
 
Thanks dbomrrsm ...with a few modifications this seemed work!

set @insertstatement = 'insert into KbText_' + cast(@appid as varchar(25))+' select ''Yes'',' + @vtext1+ @vtext2+ @vtext3+ @vtext4+ @vtext5+ @vtext6 + ' TextData,' + cast(@appid as varchar(25))
exec(@insertstatement)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top