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
 General SQL Server Forums
 New to SQL Server Programming
 Change temp table to real table

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2012-08-13 : 11:15:42
SQL 2008
I'm trying to trouble shoot an issue with the code below. Is there a way to see what is in the temp table abc?

Or how can I change the code to use a real table abc? I've created a table in SQL called ABC with the fields declared below.

DECLARE @req_ship_dt varchar(20), @qty_to_ship decimal(13,4), @qty_bkord decimal(13,4),
@qty_ordered decimal(13,4), @qty_on_hand decimal(13,4), @qty_allocated decimal(13,4), @so_qty_bkord decimal(13,4)
DECLARE abc CURSOR FOR
SELECT req_ship_dt, qty_to_ship, qty_bkord, so_qty_bkord, qty_ordered,
qty_on_hand, qty_allocated FROM LarryAlkon
ORDER BY req_ship_dt
for update of qty_to_ship, so_qty_bkord, qty_bkord;
OPEN abc
FETCH NEXT FROM abc INTO @req_ship_dt, @qty_to_ship, @qty_bkord,
@so_qty_bkord, @qty_ordered, @qty_on_hand, @qty_allocated
WHILE (@@FETCH_STATUS = 0)
BEGIN--While
DECLARE @available decimal(13,4)
SET @available = @qty_on_hand + @qty_bkord - @qty_allocated
IF (@available > 0) BEGIN --1
IF (@available > @so_qty_bkord) BEGIN --2
UPDATE LarryAlkon
SET qty_to_ship = (SELECT qty_ordered FROM oeordlin_sql WHERE ord_type=LarryAlkon.ord_type
AND ord_no=LarryAlkon.ord_no AND line_seq_no=LarryAlkon.line_seq_no),
so_qty_bkord = 0
WHERE CURRENT OF abc
UPDATE LarryAlkon
SET qty_bkord = @qty_bkord-@so_qty_bkord
WHERE CURRENT OF abc
END --2
ELSE BEGIN --3
UPDATE LarryAlkon
SET qty_to_ship = @qty_to_ship + @available, so_qty_bkord = @so_qty_bkord - @available
WHERE CURRENT OF abc
UPDATE LarryAlkon
SET qty_bkord = @qty_bkord - @available
WHERE CURRENT OF abc
END --3
END --1
FETCH NEXT FROM abc INTO @req_ship_dt, @qty_to_ship,
@qty_bkord, @so_qty_bkord, @qty_ordered, @qty_on_hand, @qty_allocated
END --While
CLOSE abc
DEALLOCATE abc
GO
--ADDED BY PETE TO CLEAR BO QTY FLAG WHEN FILLED
UPDATE oeordlin_sql
SET qty_bkord = 0
WHERE (qty_to_ship - qty_ordered = 0) AND (qty_bkord <> 0)
GO
--ADDED BY PETE TO CALCULATE NEW BO QTY WHEN FILLED
UPDATE oeordlin_sql
SET qty_bkord = qty_ordered - qty_to_shiP
WHERE (ord_type = 'O') AND (qty_ordered - qty_to_ship <> qty_bkord) AND (bkord_fg = 'Y')
GO

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2012-08-13 : 11:24:53
What is is in "abc" is the recordset produced by the SELECT statements. You'll need to use Dynamic Cursors to see what is in the table

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-08-13 : 11:49:52
What's the issue you're having with the code? It looks like you're doing an ordered update of inventory based on shipping date.
Go to Top of Page

Vack
Aged Yak Warrior

530 Posts

Posted - 2012-08-13 : 13:48:30
Getting the following message after updating from SQL 2000 to SQL 2008. But I think the script is still running.
Problem is that its updating the qty_to_ship incorrectly. After stepping through the code it looks like there is a problem with the logic. Had someone else tell me it wasn't working correctly because of the error msg below.

(1 row(s) affected)
Msg 16947, Level 16, State 1, Line 23
No rows were updated or deleted.
The statement has been terminated.


Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-08-13 : 14:13:15
My only guess is that cursors are processing differently in 2008 than 2000, and that this cursor is declared as DYNAMIC when it should not be. Assuming you can't rewrite this to not use cursors, I suggest declaring the cursor with STATIC and FORWARD_ONLY to ensure each row is only processed once.

If you have to do the update in ordered fashion, changing the cursor to a table won't change the processing. If you don't have to do an ordered update, and you can rewrite this, it can be greatly simplified in SQL 2008.
Go to Top of Page
   

- Advertisement -