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)
 Cursor-based to set-base solution

Author  Topic 

dee-u
Starting Member

14 Posts

Posted - 2007-03-02 : 23:46:59
Anyone here who could help me out in transforming my sp to a set-based one?

IF EXISTS (SELECT name FROM sysobjects 
WHERE name = 'IncomeCodes' AND type = 'P')
DROP PROCEDURE IncomeCodes
GO

CREATE PROCEDURE IncomeCodes AS
SET NOCOUNT ON
DECLARE @income_code int, @totalcost money, @eyeglass money, @expenditure money
DECLARE income_cursor CURSOR FOR
SELECT tabEGPurchase.Code
FROM tabEGPurchase INNER JOIN
tabEGPayments ON tabEGPurchase.Code = tabEGPayments.Code INNER JOIN
tabEarningRecords ON tabEGPayments.IncomeCode = tabEarningRecords.Sequence LEFT OUTER JOIN
tabFrameList ON tabEGPurchase.EyeglassCode = tabFrameList.ControlCode
WHERE (DATEDIFF(Month, tabEarningRecords.DateEarned, GETDATE()) > 0)
GROUP BY tabEGPurchase.Code

UPDATE tabEarningRecords SET Expenditure = 0
WHERE (Sequence IN
(SELECT tabEarningRecords.Sequence
FROM tabEGPurchase INNER JOIN
tabEGPayments ON tabEGPurchase.Code = tabEGPayments.Code INNER JOIN
tabEarningRecords ON tabEGPayments.IncomeCode = tabEarningRecords.Sequence))

OPEN income_cursor

FETCH NEXT FROM income_cursor
INTO @income_code

WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @totalcost=ISNULL(tabFrameList.BuyingPrice, 0) + ISNULL(tabEGPurchase.MTC, 0) FROM tabEGPurchase LEFT OUTER JOIN
tabFrameList ON tabEGPurchase.EyeglassCode = tabFrameList.ControlCode
WHERE tabEGPurchase.Code = @income_code

DECLARE income2_cursor CURSOR FOR
SELECT tabEarningRecords.EyeGlass
FROM tabEGPurchase INNER JOIN
tabEGPayments ON tabEGPurchase.Code = tabEGPayments.Code INNER JOIN
tabEarningRecords ON tabEGPayments.IncomeCode = tabEarningRecords.Sequence LEFT OUTER JOIN
tabFrameList ON tabEGPurchase.EyeglassCode = tabFrameList.ControlCode
WHERE (DateDiff(Month, tabEarningRecords.DateEarned, GETDATE()) > 0) AND (tabEGPurchase.Code = @income_code)

OPEN income2_cursor
FETCH NEXT FROM income2_cursor INTO @eyeglass

WHILE @@FETCH_STATUS = 0
BEGIN
If (@totalcost >= @eyeglass)
BEGIN
SET @totalcost = @totalcost - @eyeglass
SET @expenditure = @eyeglass
END
Else
BEGIN
SET @expenditure = @totalcost
SET @totalcost = 0
END

--UPDATE tabEarningRecords SET Expenditure = @expenditure WHERE
UPDATE tabEarningRecords SET Expenditure = @expenditure
WHERE CURRENT OF income2_cursor

PRINT 'eyeglass : ' + Convert(NVarChar,@eyeglass)
PRINT 'expenditure : ' + Convert(NVarChar,@expenditure)
PRINT 'remaining : ' + Convert(NVarChar,@totalcost)
PRINT ''

IF (@totalcost = 0) BREAK

FETCH NEXT FROM income2_cursor INTO @eyeglass
END

CLOSE income2_cursor
DEALLOCATE income2_cursor

-- Get the next author.
FETCH NEXT FROM income_cursor
INTO @income_code
END

CLOSE income_cursor
DEALLOCATE income_cursor
GO

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2007-03-04 : 13:44:49
Sure... but would you put in a little effort? Like maybe YOU analyze the code and come up with a decent problem description?

--Jeff Moden
Go to Top of Page

dee-u
Starting Member

14 Posts

Posted - 2007-03-05 : 03:58:46
I am actually a novice when it comes to T-SQL, just heard that sql2000 likes a set-based solution better than a cursor based which is the one I am currently using so I really don't know where is the correct path to go so any advices will be appreciated. :-)
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-05 : 04:01:25
Maybe you can start by explaining what the stored procedure is doing.


KH

Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2007-03-05 : 09:32:42
supplying some sample input data + matching expected results may help as well.
as would be some DDL for the tables listed.
Go to Top of Page
   

- Advertisement -