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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 problem with dynamic sql statement

Author  Topic 

PeterG
Posting Yak Master

156 Posts

Posted - 2002-05-10 : 19:55:23
I have the following dynamic query:

Set @sql = 'Select CD.blnAllowAll from CodeDpndCol2 CD inner join CodePFMulti2 CP
on CD.lngCodeSourceID = CP.lngCodeID inner join Element' + @acctLevel + '_Codes EC
on EC.lngIndex = CD.lngCodeDependID where CP.lngProfID = ' + @profID + ' and EC.txtCostCenter = ' + @array_value


When I run this on query analyzer, I get an error of "comp1 is an invalid column" . But if I replace the variables and write this as a static query I get 1. I did a select @sql and I think the problem is that @array_value is a string and that when it is executed it must have the single quotes around it like this 'comp1', so it is recognized as string. How do I fix this? Pls help. Thanks.

[There are '+' characters before/after the variables.]

VyasKN
SQL Server MVP & SQLTeam MVY

313 Posts

Posted - 2002-05-11 : 05:20:38
Try this (Next time post the variable declarations as well, so we don't have to do it ourselves):

Set @sql = 'Select CD.blnAllowAll from CodeDpndCol2 CD inner join CodePFMulti2 CP
on CD.lngCodeSourceID = CP.lngCodeID inner join Element' + @acctLevel + '_Codes EC
on EC.lngIndex = CD.lngCodeDependID where CP.lngProfID = ' + @profID + ' and EC.txtCostCenter = ''' + @array_value + ''''




--
HTH,
Vyas
http://vyaskn.tripod.com
Go to Top of Page
   

- Advertisement -