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)
 How to drop cursor from this query

Author  Topic 

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2005-07-05 : 00:37:10
Hi All,

Please tell me how to drop cursor from following code

DECLARE @StockCode AS VARCHAR(10)
DECLARE @BayNumber AS VARCHAR(10)
DECLARE @Quantity AS INTEGER
DECLARE @Now AS DateTime
DECLARE @TxnID AS Integer
DECLARE @MoveQuantity AS Integer

DECLARE stock_cursor CURSOR FOR
SELECT *
FROM TotalByBay
WHERE Quantity <> 0
ORDER BY StockCode

OPEN stock_cursor

FETCH NEXT FROM stock_cursor
INTO @StockCode, @BayNumber, @Quantity

WHILE @@FETCH_STATUS = 0
BEGIN

PRINT @StockCode + ', ' + @BayNumber + ', ' + CAST(@Quantity AS VARCHAR(20))

SET @Now = GetDate()

INSERT INTO MOVEMENT(StockCode,....) Values(@StockCode,...)------1
SELECT @TxnID = @@identity -- get id


SET @MoveQuantity = -1*@Quantity
INSERT INTO BayMovement(....) Values(@TxnID, @BayNumber, @MoveQuantity, @Quantity)-----2


INSERT INTO StkAdjustments(StockCode, InOut, Source, Destination, Initials, OrderID, InternalLocOut, DeleteItem, TxnID, BayNumber, MovementQuantity, RawQuantity)
VALUES(@StockCode, '2', '5', '4', 'SYS', 0, 2, 0, @TxnID, @BayNumber, @MoveQuantity, @Quantity)------3

FETCH NEXT FROM stock_cursor
INTO @StockCode, @BayNumber, @Quantity
END

CLOSE stock_cursor
DEALLOCATE stock_cursor
GO


As you can see ID generated in 1st Insert is used in 2nd & 3rd inserts.

Thanks

mk_garg

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-07-05 : 00:59:29
Did you run the code and did you get any error?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2005-07-05 : 01:02:36
No error!
i am thinking to write a solution to do samething without cursor.
i am not sure it is possible or not.

Thanks


mk_garg
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-07-05 : 01:16:40
Explain what you are trying to do in text so that everyone can understand easily

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2005-07-05 : 01:21:33
1. I read some records from "TotalByBay"
2. For each record i do some manipulation on data & insert record into Movement table. Which gives primary key value in variable @TxnID
3. Value in @TxnID is used in next two insert statements for each record read from "TotalByBay" Table

I hope now can understand what i am trying to do.

Thanks






mk_garg
Go to Top of Page

hgorijal
Constraint Violating Yak Guru

277 Posts

Posted - 2005-07-05 : 01:35:57
A standard alternative for cursors...


SELECT identity(int, 1,1) as ID_COL, <other required columns only; dont use *>
INTO #TEMP_TABLE
FROM TotalByBay
WHERE Quantity <> 0
ORDER BY StockCode

DECLARE @ID_COL int
:
<declare more variables for each column in the above tamp table>
:
SET @ID_COL = 0
WHILE (1=1)
BEGIN
SET ROWCOUNT 1

SELECT @ID_COL = ID_COL, <other columns into corresponding variables>
FROM #TEMP_TABLE
WHERE ID_COL > @ID_COL

If @@rowcount = 0 break

SET ROWCOUNT 0


:
:
< Do the rest of your process..>
:
:
END


HTH...

Hemanth Gorijala
I Came. I Saw. I Normalized.
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2005-07-05 : 01:41:16
Hemanth thanks for this.
Here we are loop instead of cursor.
Will it be faster than using cursor.

Thanks


mk_garg
Go to Top of Page

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2005-07-05 : 05:34:11
Is StockCode unique in table Movement?
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2005-07-05 : 18:29:19
Nope it is not unique

mk_garg
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2005-07-06 : 19:34:14
Can any one which approach should be used to get better performance
cursor
or
Temp table as suggested by hemanth.

Thanks

mk_garg
Go to Top of Page

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2005-07-07 : 03:48:58
Try this:
declare @tmp table(
TxnID int identity(1,1) primary key clustered,
StockCode ...,
BayNumber ...,
Quantity ...
)
insert into @tmp(StockCode, BayNumber, Quantity)
select StockCode, ...
from TotalByBay
where Quantity > 0 --better then <> 0 if there is an index having Quantity as a first column and qty can't be < 0
--1
set identity_insert movement on
insert into movement (TnxID, StockCode, ...)
select TnxID, StockCode, ...
from @tmp
set identity_insert movement off
--2
insert into BayMovement(TnxID, BayNumber, MoveQuantity, Quantity)
select TnxID, @BayNumber, -1 * Quantity, Quantity
from @tmp
--3
insert into StkAdjustments(StockCode, InOut, Source, Destination, Initials, OrderID, InternalLocOut, DeleteItem, TxnID, BayNumber, MovementQuantity, RawQuantity)
select StockCode, '2', '5', '4', 'SYS', 0, 2, 0, TxnID, BayNumber, -1 * Quantity, Quantity)
from @tmp
go
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2005-07-09 : 18:44:30
I always forget to mention, i am using SQL server 7.0.

Anyway i got what meant to say in the code.

Thanks for your help.

mk_garg
Go to Top of Page

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2005-07-10 : 06:00:02
You are wellcome. I made an error in the code. Instead of using identity value from temp table you will have to add the value of last identity before the insert. You have also to include retries after insert into movement table because of possible concurent executions. If it fails because of pk violation you would need to re-read last identity and try that insert again.
Go to Top of Page
   

- Advertisement -