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 |
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2007-09-25 : 06:41:28
|
Hi All,For various reason I am inserting the update statement in a variable, then I am also putting my WHERE criteria in another variable. Finally I want to run this two variables as if its a update statement with a WHERE clause. But I am getting erorr.Please run the following code to see.EXAMPLE1:Use Northwindselect * into #OrderDetails from [Order Details]DECLARE @WhereCriteria as nvarchar(1000),@sql as nvarchar(1000)set @WhereCriteria='price<6'set @sql='update #OrderDetails set quantity=0 where '+@WhereCriteriaprint @WhereCriteriaprint @sqlexec @sqldrop table #OrderDetailsThe erorr says Could not find stored procedure 'update #OrderDetails set quantity=0 where price<6' Any idea why I am getting this erorr?In a similar way I want to run the following script.EXAMPLE2:CREATE TABLE #PromoMeasure (ID int IDENTITY(1,1),PromotionType nvarchar(100), AdType nvarchar(100), DisplayType nvarchar(100),WhereCriteria nvarchar(1000))GOINSERT INTO #PromoMeasure(PromotionType, AdType, DisplayType,WhereCriteria)VALUES('BOGOF','OVERHEAD BANNER','AISLE END','CoulmnA>250 and CoulmnB<=-49 and CoulmnB>=-52')GOINSERT INTO #PromoMeasure(PromotionType, AdType, DisplayType,WhereCriteria)VALUES('Price Promotion','SHELF TALKER','PROMOTION BIN','CoulmnA>250 and CoumnB<=-30 and CoumnB>=-48')GODECLARE @i as INT, @Counter AS INT, @ID as INT, @PromotionType nvarchar(100), @AdType nvarchar(100), @DisplayType nvarchar(100), @WhereCriteria nvarchar(1000), @sql as nvarchar(1000)set @i=1set @Counter=2 While @i<=@Counterbegin print @i print @counter select @ID=ID,@PromotionType=PromotionType,@AdType=AdType,@DisplayType=DisplayType, @WhereCriteria=WhereCriteria from #PromoMeasure where ID=@i set @sql = 'update #Table2 Set PromotionType=@PromotionType,AdType=@AdType,DisplayType=@DisplayType where'+' '+ @WhereCriteria --where [NewUnitsContribution%]>250 and DiffWithRegularPrice<=-49 and DiffWithRegularPrice>=-52 exec @sqlendHere it gives the following erorr:The name 'update #Table2 Set PromotionType=@PromotionType,AdType=@AdType,DisplayType=@DisplayType where CoulmnA>250 and CoulmnB<=-49 and CoulmnB>=-52' is not a valid identifier.Can someone please help?Thanks a million in advance.Zee |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-09-25 : 06:47:12
|
quote: Originally posted by zeeshan13 Hi All,For various reason I am inserting the update statement in a variable, then I am also putting my WHERE criteria in another variable. Finally I want to run this two variables as if its a update statement with a WHERE clause. But I am getting erorr.Please run the following code to see.EXAMPLE1:Use Northwindselect * into #OrderDetails from [Order Details]DECLARE @WhereCriteria as nvarchar(1000),@sql as nvarchar(1000)set @WhereCriteria='price<6'set @sql='update #OrderDetails set quantity=0 where '+@WhereCriteriaprint @WhereCriteriaprint @sqlexec(@sql)drop table #OrderDetailsThe erorr says Could not find stored procedure 'update #OrderDetails set quantity=0 where price<6' Any idea why I am getting this erorr?In a similar way I want to run the following script.EXAMPLE2:CREATE TABLE #PromoMeasure (ID int IDENTITY(1,1),PromotionType nvarchar(100), AdType nvarchar(100), DisplayType nvarchar(100),WhereCriteria nvarchar(1000))GOINSERT INTO #PromoMeasure(PromotionType, AdType, DisplayType,WhereCriteria)VALUES('BOGOF','OVERHEAD BANNER','AISLE END','CoulmnA>250 and CoulmnB<=-49 and CoulmnB>=-52')GOINSERT INTO #PromoMeasure(PromotionType, AdType, DisplayType,WhereCriteria)VALUES('Price Promotion','SHELF TALKER','PROMOTION BIN','CoulmnA>250 and CoumnB<=-30 and CoumnB>=-48')GODECLARE @i as INT, @Counter AS INT, @ID as INT, @PromotionType nvarchar(100), @AdType nvarchar(100), @DisplayType nvarchar(100), @WhereCriteria nvarchar(1000), @sql as nvarchar(1000)set @i=1set @Counter=2 While @i<=@Counterbegin print @i print @counter select @ID=ID,@PromotionType=PromotionType,@AdType=AdType,@DisplayType=DisplayType, @WhereCriteria=WhereCriteria from #PromoMeasure where ID=@i set @sql = 'update #Table2 Set PromotionType=@PromotionType,AdType=@AdType,DisplayType=@DisplayType where'+' '+ @WhereCriteria --where [NewUnitsContribution%]>250 and DiffWithRegularPrice<=-49 and DiffWithRegularPrice>=-52 exec(@sql)endHere it gives the following erorr:The name 'update #Table2 Set PromotionType=@PromotionType,AdType=@AdType,DisplayType=@DisplayType where CoulmnA>250 and CoulmnB<=-49 and CoulmnB>=-52' is not a valid identifier.Can someone please help?Thanks a million in advance.Zee
Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
|
|
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2007-09-25 : 07:02:20
|
harsh_athalye,Thanks for the quick reply.I made the changes and it worked for the first example.But for example its giving the following erorr:Server: Msg 137, Level 15, State 2, Line 1Must declare the variable '@PromotionType'.Server: Msg 137, Level 15, State 2, Line 1Must declare the variable '@PromotionType'.Although I have declared this '@PromotionType' variable, anny idea why its giving this error?Thanks for all your help.Zee |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-25 : 07:12:34
|
http://www.sommarskog.se/dynamic_sql.htmlset @sql = 'update #Table2 Set PromotionType=' + quotename(@PromotionType, '''') + ',AdType=' + quotename(@AdType, '''') + ',DisplayType=' + quotename(@DisplayType, '''') + ' where ' + @WhereCriteriaexec (@sql) E 12°55'05.25"N 56°04'39.16" |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2007-09-25 : 08:55:17
|
@PromotionType is defined outside the scope of your executed SQL Statement, so you must concatenate the value as part of your statement. You cannot refer to variables inside a dynamic sql statement that were defined outside that statement.set @sql = 'update #Table2 Set PromotionType=' + @PromotionType + ',AdType=@AdType,DisplayType=@DisplayType where'+' '+ @WhereCriteria --where [NewUnitsContribution%]>250 and DiffWithRegularPrice<=-49 and DiffWithRegularPrice>=-52exec @sql e4 d5 xd5 Nf6 |
|
|
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2007-09-25 : 10:54:20
|
blindman / Peso,Yes thats what i already did and it worked :)Thanks for your help. I want to put a star, or lets say I want to accept your solution here...how do you do that?Also I will be posting another problem, please check.Thanks,Zee |
|
|
|
|
|
|
|