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 |
|
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 codeDECLARE @StockCode AS VARCHAR(10)DECLARE @BayNumber AS VARCHAR(10)DECLARE @Quantity AS INTEGERDECLARE @Now AS DateTimeDECLARE @TxnID AS IntegerDECLARE @MoveQuantity AS IntegerDECLARE stock_cursor CURSOR FORSELECT *FROM TotalByBayWHERE Quantity <> 0ORDER BY StockCodeOPEN stock_cursorFETCH NEXT FROM stock_cursorINTO @StockCode, @BayNumber, @QuantityWHILE @@FETCH_STATUS = 0BEGIN 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, @QuantityENDCLOSE stock_cursorDEALLOCATE stock_cursorGO As you can see ID generated in 1st Insert is used in 2nd & 3rd inserts.Thanksmk_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?MadhivananFailing to plan is Planning to fail |
 |
|
|
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.Thanksmk_garg |
 |
|
|
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 easilyMadhivananFailing to plan is Planning to fail |
 |
|
|
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 @TxnID3. Value in @TxnID is used in next two insert statements for each record read from "TotalByBay" TableI hope now can understand what i am trying to do.Thanksmk_garg |
 |
|
|
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 TotalByBayWHERE Quantity <> 0ORDER BY StockCodeDECLARE @ID_COL int: <declare more variables for each column in the above tamp table>:SET @ID_COL = 0WHILE (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 GorijalaI Came. I Saw. I Normalized. |
 |
|
|
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.Thanksmk_garg |
 |
|
|
mmarovic
Aged Yak Warrior
518 Posts |
Posted - 2005-07-05 : 05:34:11
|
| Is StockCode unique in table Movement? |
 |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2005-07-05 : 18:29:19
|
| Nope it is not uniquemk_garg |
 |
|
|
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 performancecursororTemp table as suggested by hemanth.Thanksmk_garg |
 |
|
|
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--1set identity_insert movement oninsert into movement (TnxID, StockCode, ...) select TnxID, StockCode, ... from @tmpset identity_insert movement off--2insert into BayMovement(TnxID, BayNumber, MoveQuantity, Quantity) select TnxID, @BayNumber, -1 * Quantity, Quantity from @tmp--3insert 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 @tmpgo |
 |
|
|
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 |
 |
|
|
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. |
 |
|
|
|
|
|
|
|