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 |
ram_01
Starting Member
1 Post |
Posted - 2006-11-03 : 01:59:05
|
hello all,i have a problem appending a variable declared as varcharto a query the below code works declare @str varchar(100),@str2 varchar(1000),@orgid intset @str = 'sum(a.CY_Sales_CropProtection) >100000 'set @orgid=1set @str2 =(select sum(a.CY_Sales_CropProtection) from usa_data a , tmpstep3 b where (a.orgid<>''+@orgid+'') and (a.CY_RC_Chain=b.RetailCategory_chain) having (sum(a.CY_Sales_CropProtection) >100000))print @str2but when i try to append i get an incorrect syntax error,declare @str varchar(100),varchar(100),@str2 varchar(100),@orgid intset @str = 'sum(a.CY_Sales_CropProtection) >100000 'set @orgid=1set @str2 =(select sum(a.CY_Sales_CropProtection) from usa_data a , tmpstep3 b where (a.orgid<>''+@orgid+'') and (CY_RC_Chain=b.RetailCategory_chain) having (+@str)) print @str2kindly let me know how to solve this |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-03 : 02:46:09
|
declare @str varchar(100),varchar(100),@str2 varchar(100),@orgid intset @str = 'sum(a.CY_Sales_CropProtection) >100000 'set @orgid=1set @str2 ='(select sum(a.CY_Sales_CropProtection) from usa_data a , tmpstep3 b where (a.orgid<>' + @orgid + ') and (CY_RC_Chain=b.RetailCategory_chain)having (' + @str + '))' print @str2Peter LarssonHelsingborg, SwedenEDIT: PS. If possible, stay away from dynamic SQL. |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-11-03 : 02:50:53
|
The first code will not work as SQL code, is it from an application? It looks like perhaps what you're trying to do is something like thisdeclare @str varchar(100), @str2 varchar(1000), @orgid intset @str = 'sum(a.CY_Sales_CropProtection) > 100000'set @orgid=1set @str2 = '(select sum(a.CY_Sales_CropProtection) from usa_data a , tmpstep3 bwhere (a.orgid <> ' + cast(@orgid AS varchar(10)) + ') and (CY_RC_Chain=b.RetailCategory_chain)having (' + @str + '))' print @str2 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-11-03 : 03:41:55
|
quote: Originally posted by Peso declare @str varchar(100),varchar(100),@str2 varchar(100),@orgid intset @str = 'sum(a.CY_Sales_CropProtection) >100000 'set @orgid=1set @str2 ='(select sum(a.CY_Sales_CropProtection) from usa_data a , tmpstep3 b where (a.orgid<>' + @orgid + ') and (CY_RC_Chain=b.RetailCategory_chain)having (' + @str + '))' print @str2
Peter, you missed a couple of errors in the original post - extra varchar in the declare that is probably what was causing rams syntax error, @str2 is too short to hold the result, and @orgid is an int, so needs to be cast to a varchar. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-11-04 : 06:32:22
|
As said, avoid Dynamic SQL as much as possibleor read this www.Sommarskog.se/dynamic_sql.htmlMadhivananFailing to plan is Planning to fail |
|
|
|
|
|
|
|