| Author |
Topic |
|
ujjaval
Posting Yak Master
108 Posts |
Posted - 2006-01-12 : 00:39:57
|
| I am trying to run following query in SQL Server 7.0 but getting errors. Any suggestion to make it work.Thanks,Ujjaval.----------declare @tempCodeNo intdeclare @tempString varchar(20)declare @rowcounter intdeclare @counter intset @rowcounter = 0set @counter = (select count(*) from [dbo].[codeTable])while(@counter>0)beginset @query = '(select @tempCodeNo = bitmapID, @tempString = description from [dbo].[codeTable] where bitmapID not in (select top ' + cast(@rowcounter as nvarchar)+ ' bitmapID from [dbo].[codeTable] order by bitmapID) order by bitmapID)'exec @queryset @counter = @counter - 1set @rowcounter = @rowcounter + 1print @tempCodeNoprint @tempStringendgo |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-12 : 00:54:23
|
| 1. variable @query not declared2. use exec(@query)-----------------'KH'Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. |
 |
|
|
ujjaval
Posting Yak Master
108 Posts |
Posted - 2006-01-12 : 01:04:09
|
| It changed the error message now.actually that was for a stored procedure that I created. Here is the complete code. -------------use cascadeTestgoif exists (select * from sysobjects where id = object_id(N'[dbo].[sp_dynamicDecode]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[sp_dynamicDecode]GOcreate procedure sp_dynamicDecode( @codeNo int)ASset nocount ondeclare @outputString varchar(50)declare @counter intdeclare @rowcounter intdeclare @tempCodeNo intdeclare @tempString varchar(20)declare @tempOutput varchar(50)declare @query varchar(1000)set @counter = (select count(*) from [dbo].[codeTable])set @rowcounter = 0set @outputString = ''while(@counter > 0)begin set @query = '(select @tempCodeNo = bitmapID, @tempString = description from [dbo].[codeTable] where bitmapID not in (select top ' + cast(@rowcounter as nvarchar)+ ' bitmapID from [dbo].[codeTable] order by bitmapID) order by bitmapID)' exec (@query) set @tempOutput = ', ' + @tempString print @tempOutput if (@codeNo & @tempCodeNo) = @tempCodeNo set @outputString = @outputString + @tempOutput set @counter = @counter - 1 set @rowcounter = @rowcounter + 1endset @outputString = right(@outputString, len(@outputString)-2)select @outputString---------------to execute this stored procedure I am doing this:-------------use cascadeTestgosp_dynamicDecode 12---------------And I'm getting the following error messages:------------------Server: Msg 137, Level 15, State 1, Line 1949556518Must declare the variable '@tempCodeNo'.Server: Msg 156, Level 15, State 1, Line 1949556518Incorrect syntax near the keyword 'order'.Server: Msg 137, Level 15, State 1, Line 1949556518Must declare the variable '@tempCodeNo'.Server: Msg 156, Level 15, State 1, Line 1949556518Incorrect syntax near the keyword 'order'.Server: Msg 137, Level 15, State 1, Line 1949556518Must declare the variable '@tempCodeNo'.Server: Msg 156, Level 15, State 1, Line 1949556518Incorrect syntax near the keyword 'order'.Server: Msg 137, Level 15, State 1, Line 1949556518Must declare the variable '@tempCodeNo'.Server: Msg 156, Level 15, State 1, Line 1949556518Incorrect syntax near the keyword 'order'.Server: Msg 137, Level 15, State 1, Line 1949556518Must declare the variable '@tempCodeNo'.Server: Msg 156, Level 15, State 1, Line 1949556518Incorrect syntax near the keyword 'order'.Server: Msg 137, Level 15, State 1, Line 1949556518Must declare the variable '@tempCodeNo'.Server: Msg 156, Level 15, State 1, Line 1949556518Incorrect syntax near the keyword 'order'.Server: Msg 536, Level 16, State 1, Line 1949556518Invalid length parameter passed to the substring function. -------------------------------------------------- Any ideas now??Thanks in advance.Ujjaval |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-12 : 01:28:31
|
| the variable @tempCodeNo is out of scope. Change you code to use sp_executesql. Unlike exec() it support parameters.-----------------'KH'Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-01-12 : 02:28:35
|
| Why do you need dynamic SQL?Post your exact requirementMadhivananFailing to plan is Planning to fail |
 |
|
|
ujjaval
Posting Yak Master
108 Posts |
Posted - 2006-01-12 : 20:18:13
|
| I tried using sp_executesql. But it can't take more than one variable in argument i think as it gave me error. Also, I want the query output in @tempCodeNo and @tempString variable. So, that I can use it later.I tried to break my sql into two parts, one for @tempCodeNo and the other for @tempString.But I can't get the values in both variable. it gets printed on screen though. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
Mubarak110
Starting Member
2 Posts |
Posted - 2009-06-29 : 09:31:36
|
| Hi Try to include all the variable declaration within the begin and end statement.good luck |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-06-29 : 10:20:48
|
quote: Originally posted by Mubarak110 Hi Try to include all the variable declaration within the begin and end statement.good luck
This thread is three years old. I doubt OP must have solved the issue much earlier MadhivananFailing to plan is Planning to fail |
 |
|
|
Mubarak110
Starting Member
2 Posts |
Posted - 2009-06-30 : 15:03:46
|
quote: Originally posted by madhivanan
quote: Originally posted by Mubarak110 Hi Try to include all the variable declaration within the begin and end statement.good luck
This thread is three years old. I doubt OP must have solved the issue much earlier MadhivananFailing to plan is Planning to fail
lol, did not even look at the date, yah the OP must have solve it by now thanks for reply |
 |
|
|
|