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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql server 2000 a parametet inside dynamic sql 1

Status
Not open for further replies.

tempo1

Programmer
Feb 20, 2007
118
Hi everyone
i write a dynamic sql query in which a parameter is involoved:
Code:
/*drop table table1*/
DECLARE @param1 VARCHAR(10)
SET @param1='aa'
DECLARE @dynamic VARCHAR(1000)
SET @dynamic=
'
SELECT '
+ '@param1' + ' "param"
INTO
table1
'
print(@dynamic)
exec(@dynamic)
go
and i dont know how to get rid of the following error message i get
Server: Msg 137, Level 15, State 2, Line 2
Must declare the variable '@param1'.
Could anyone tell me what is wrong with that querry ?Thanks.
 
The quotes surrounding the parameter are part of the fixed pieces of the string to be executed. They should be inside the quotes surrounding the fixed pieces. Use two quotes to represent a literal quote mark in a string.
Code:
'
SELECT [COLOR=red]''[/color]'
+ @param1 + '[COLOR=red]''[/color]
INTO
table1
'

I am not familiar with the syntax of a SELECT INTO statement, but I would take a look at whether an alias "param" is needed or even allowed. It might be OK, but I would check Books Online.

Also you are putting newline characters in the string. They will probably be ignored, but I wouldnt do it.


 
Thanks a lot rac2, your consult was usefull.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top