This is a fairly complex stored procedure. Though I have seen some that were a lot more complex. Essentially the procedure declares a temporary table (@tmpDevices) then populates it with the devices available on the specified date through a couple of INSERT INTO statements.
The next piece uses a cursor to select any updates and apply them to the available devices. Finally a SELECT statement is called on the temporary table to return the records. Here's the cursor portion:
Code:
DECLARE
curTmp CURSOR FOR
SELECT ri.ANALYZER_ID, ri.ITEM_TYPE, idt.ITEM_DATE_ID, idt.CHECKED
FROM (appToledoCEM.REPORT_ITEMS ri INNER JOIN appToledoCEM.STATUS_REPORT sr
ON ri.STATUS_REP_ID = sr.STATUS_REP_ID) INNER JOIN appToledoCEM.ITEM_DATES idt
ON ri.REPORT_ITEM_ID = idt.REPORT_ITEM_ID
WHERE sr.REPORT_DATE = @repdate
ORDER BY ri.ANALYZER_ID, ri.ITEM_TYPE, idt.ITEM_DATE_ID, idt.CHECKED;
OPEN curTmp;
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM curTmp INTO @analid, @ittype, @itemid, @checked;
UPDATE @tmpDevices SET CHECKED = @checked, ITEM_DATE_ID = @analid
WHERE ANALYZER_ID = @analid AND ITEM_TYPE = @ittype;
END;
CLOSE curTmp;
SELECT * FROM @tmpDevices ORDER BY ITEM_TYPE, ORDER_NUM;
With the test data I'm using there is only one device that needs to have CHECK set to 1 but it will only do it the first time I run the procedure after not running it for a long time (hours). Even if I edit the stored procedure and re-run it, it doesn't see the Checked item.
I can run the same SELECT statement I used to declare the cursor immediately before I open the cursor. This causes me to get 2 result sets back, the first set shows the one device I need checked. But the second set doesn't.
I added "SELECT @analid, @ittype, @itemid, @checked;" right after the FETCH statement. This give me 2 result sets (where I expected 3), the first is the one record from the Cursor SELECT statement and the second is the final result set. But I didn't get a set for the SELECT following the FETCH.
All in all I'm pretty confused and looking for another way to do this.
Any ideas?
Thanks
What you don't know can hurt you!