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
 Transact-SQL (2000)
 Dynamic Query Result to a Cursor

Author  Topic 

dragon_ballz96
Starting Member

3 Posts

Posted - 2004-10-09 : 10:06:45
Hi,

I am trying to pass the result of a executed dynamic query to a curosr. I have trying something like this but didn't work:

declare @sql nvarchar(300)

set @sql = 'select * from tblLocation'

declare location_cursor cursor for
exec @sql

Anyone got an idea on how to do it correctly? Let me know. Thks.

Rgds
Ryan

robvolk
Most Valuable Yak

15732 Posts

Posted - 2004-10-09 : 10:36:40
Why are you using a cursor?
Go to Top of Page

dragon_ballz96
Starting Member

3 Posts

Posted - 2004-10-09 : 13:03:45
Actually, I need to get the Location_Id base on certain criteria (such as From and To location) which is generated dynamically. From the cursor, I will pass the info to another stored procedure to calculate the sales for each location.

Can't think of a way apart from using an cursor. Let me know if u have any better suggestion. Thks.

Ryan
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-10-09 : 13:15:33
give us some 'create tables' and sample data to work with.. and the desired result of course... and we can show you a better way than a cursor...

Corey
Go to Top of Page

dragon_ballz96
Starting Member

3 Posts

Posted - 2004-10-09 : 13:29:15
Probably let's start with following scenrio:

I got 2 tables, namely tblSalesDoc (header, Sales_Doc_Id as key) and tblSalesDocItems (line items, Sales_Doc_Id and Line_No as key), which contains sales items. Within the Sales Items, it can be either standard items or promotion items (grouping of a few standard items).

Right now, I need to display a report with criteria such as Sales Period, Location, Item_Type and so on and to show Promo Item as a single item (to combine a Promo Header, Serial No, etc from the respective standard line items within the promo items) and Standard items.

Any suggestion on how to tackle it?
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-10-09 : 14:17:25
Is this along the right lines...??


--Set ESP On

declare @tblSalesDoc table (Sales_Doc_Id int, OtherSalesInfo varchar(100))
Insert Into @tblSalesDoc
Select 1, 'This is sale #1, 1 non-promo item'
Union Select 2, 'This is sale #2, multiple non-promo items'
Union Select 3, 'This is sale #3, 1 promo item'
Union Select 4, 'This is sale #4, multiple promo items'
Union Select 5, 'This is sale #5, multiple non-promo items and multiple promo items'

declare @tblSalesDocItems table (Sales_Doc_Id int, LineItem int, PromoSerial varchar(100), itemSerial varchar(100))
Insert Into @tblSalesDocItems
Select 1, 1, null, 'abcd1234'
Union Select 2, 1, null, 'abcd1234'
Union Select 2, 2, null, 'zyxw9876'
Union Select 3, 1, 'promo1', 'abcd1234'
Union Select 3, 2, 'promo1', 'zyxw9876'
Union Select 4, 1, 'promo1', 'abcd1234'
Union Select 4, 2, 'promo1', 'zyxw9876'
Union Select 4, 3, 'promo2', 'abcd1234'
Union Select 4, 4, 'promo2', 'wjkf5542'
Union Select 4, 5, 'promo2', 'fkek5871'

Union Select 5, 1, null, 'abcd1234'
Union Select 5, 2, null, 'zyxw9876'
Union Select 5, 3, 'promo1', 'abcd1234'
Union Select 5, 4, 'promo1', 'zyxw9876'
Union Select 5, 5, 'promo2', 'abcd1234'
Union Select 5, 6, 'promo2', 'wjkf5542'
Union Select 5, 7, 'promo2', 'fkek5871'


--Select * From @tblSalesDoc

--Select * From @tblSalesDocItems


Select
Sales_Doc_Id,
OtherSalesInfo,
LineItem = min(LineItem),
isPromo,
ItemSerial
From
(
Select
A.Sales_Doc_Id,
A.OtherSalesInfo,
B.LineItem,
isPromo = case when PromoSerial is not null then 1 else 0 end,
ItemSerial = isnull(PromoSerial,itemSerial)
From @tblSalesDoc A
Inner Join @tblSalesDocItems B
On A.Sales_Doc_Id = B.Sales_Doc_Id
) Z
Group By Sales_Doc_Id, OtherSalesInfo, isPromo, ItemSerial

--Set ESP Off



Corey
Go to Top of Page

osbertv
Starting Member

6 Posts

Posted - 2005-06-17 : 21:02:54
quote:
Originally posted by dragon_ballz96

Hi,

I am trying to pass the result of a executed dynamic query to a curosr. I have trying something like this but didn't work:

declare @sql nvarchar(300)

set @sql = 'select * from tblLocation'

declare location_cursor cursor for
exec @sql

Anyone got an idea on how to do it correctly? Let me know. Thks.

Rgds
Ryan




you can't declare an 'exec' in a cursor,
but you can 'exec' a 'declare cursor'

declare @sql nvarchar(300)

set @sql = 'declare location_cursor cursor for select * from tblLocation'

exec sp_executesql @sql

Go to Top of Page
   

- Advertisement -