If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > A Better Way??

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-11-04, 16:23
ansonee ansonee is offline
Registered User
 
Join Date: Feb 2002
Location: Minneapolis, MN
Posts: 253
A Better Way??

Looking for a better way to implement this stored procedure. The issue is that at the end of the procedure, when you try to drop the temp table after you open the cursor, you get an error stating can't perform that action because there's a pending operation on the temp table (the cursor reading it I assume)

And if you close the cursor in order to be able to drop the temp table, you destroy the result table.

If anyone can think of a better way to implement this procedure without the need of a temp table, I'd appreciate any ideas.

Thanks in advance!

CREATE PROCEDURE AIM.GetRetrievalImageDetail (IN decAIMRetrievalID DECIMAL(13,0))
------------------------------------------------------------------------
-- SQL Stored Procedure
-- Original Author Date: April 7, 2004
-- Author: Anthony
-- Returns Image detail information for an individual AIMRetrieval.
--
-- April 14, 2004:
-- Removed STATUS as a field being returned from the AIMRetrievedItem Table
-- Changed AvailableFlag from 'Y' and 'N' to 1 and 2 per Stephanie's code.
--
------------------------------------------------------------------------
LANGUAGE SQL
RESULT SETS 1
P1: BEGIN


DECLARE GLOBAL TEMPORARY TABLE RetrievedItem
(AIMRETRIEVEDITEMID DECIMAL(13,0),
ARCHIVERETRIEVALID DECIMAL(13, 0),
AIMDOCUMENTID VARCHAR(256),
ARCHIVEDOCUMENTID VARCHAR(124),
CAPTUREDATE DATE,
ROUTINGTRANSITNUMBER VARCHAR(9),
MICRACCOUNTNUMBER VARCHAR(24),
POSTINGACCOUNTNUMBER VARCHAR(24),
LOCATION VARCHAR(24),
SERIALNUMBER VARCHAR(20),
AMOUNT DECIMAL(13, 2),
ITEMTYPE VARCHAR(20),
SEQUENCENUMBER VARCHAR(24),
SORRYDOCINDICATOR VARCHAR(20),
APPLICATIONID VARCHAR(20),
CREDITDEBITFLAG VARCHAR(5),
IMAGELENGTH INTEGER,
AVAILABLEFLAG INTEGER
) WITH REPLACE;

BEGIN
DECLARE SELECT_HIGHLEVEL CURSOR WITH RETURN FOR
SELECT
AIM.AIMRETRIEVAL.DESCRIPTION, AIM.AIMRETRIEVAL.AIMRETRIEVALRECEIPT, AIM.STATUS.STATUS
FROM
AIM.AIMRETRIEVAL,
AIM.STATUS
WHERE
AIM.AIMRETRIEVAL.STATUSID = AIM.STATUS.STATUSID
AND decAIMRetrievalID = AIM.AIMRetrieval.AIMRetrievalID;

DECLARE SELECT_CURSOR CURSOR WITH RETURN FOR
SELECT * FROM SESSION.RetrievedItem;

INSERT INTO SESSION.RetrievedItem
(
AIMRETRIEVEDITEMID ,
ARCHIVERETRIEVALID ,
AIMDOCUMENTID ,
ARCHIVEDOCUMENTID ,
CAPTUREDATE ,
ROUTINGTRANSITNUMBER ,
MICRACCOUNTNUMBER ,
POSTINGACCOUNTNUMBER ,
LOCATION ,
SERIALNUMBER ,
AMOUNT ,
ITEMTYPE ,
SEQUENCENUMBER ,
SORRYDOCINDICATOR ,
APPLICATIONID ,
CREDITDEBITFLAG ,
IMAGELENGTH
)
SELECT

AIM.AIMRETRIEVEDITEM.AIMRETRIEVEDITEMID, AIM.ARCHIVERETRIEVAL.ARCHIVERETRIEVALID, AIM.AIMRETRIEVEDITEM.AIMDOCUMENTID, AIM.AIMRETRIEVEDITEM.ARCHIVEDOCUMENTID, AIM.AIMRETRIEVEDITEM.CAPTUREDATE,
AIM.AIMRETRIEVEDITEM.ROUTINGTRANSITNUMBER, AIM.AIMRETRIEVEDITEM.MICRACCOUNTNUMBER, AIM.AIMRETRIEVEDITEM.POSTINGACCOUNTNUMBER,
AIM.AIMRETRIEVEDITEM.LOCATION, AIM.AIMRETRIEVEDITEM.SERIALNUMBER, AIM.AIMRETRIEVEDITEM.AMOUNT, AIM.AIMRETRIEVEDITEM.ITEMTYPE,
AIM.AIMRETRIEVEDITEM.SEQUENCENUMBER, AIM.AIMRETRIEVEDITEM.SORRYDOCINDICATOR, AIM.AIMRETRIEVEDITEM.APPLICATIONID,
AIM.AIMRETRIEVEDITEM.CREDITDEBITFLAG, Length(AIM.AIMRETRIEVEDITEM.ImageFront)
FROM
AIM.AIMRETRIEVAL,
AIM.STATUS,
AIM.AIMRETRIEVEDITEM,
AIM.ARCHIVERETRIEVAL,
AIM.ARCHIVETYPE
WHERE
AIM.AIMRETRIEVAL.STATUSID = AIM.STATUS.STATUSID
AND AIM.ARCHIVERETRIEVAL.ARCHIVERETRIEVALID = AIM.AIMRETRIEVEDITEM.ARCHIVERETRIEVALID
AND AIM.AIMRETRIEVAL.AIMRETRIEVALID = AIM.ARCHIVERETRIEVAL.AIMRETRIEVALID
AND AIM.ARCHIVERETRIEVAL.ARCHIVEID = AIM.ARCHIVETYPE.ARCHIVEID
AND AIM.ARCHIVETYPE.ARCHIVETYPE = 'D'
AND AIM.AIMRETRIEVAL.AIMRETRIEVALID = decAIMRetrievalID;

UPDATE SESSION.RetrievedItem SET AVAILABLEFLAG = 2 WHERE SESSION.RetrievedItem.ImageLength IS NULL OR SESSION.RetrievedItem.ImageLength = 0;
UPDATE SESSION.RetrievedItem SET AVAILABLEFLAG = 1 WHERE SESSION.RetrievedItem.ImageLength IS NOT NULL OR SESSION.RetrievedItem.ImageLength > 0;

OPEN SELECT_HIGHLEVEL;
OPEN SELECT_CURSOR;
--CLOSE SELECT_CURSOR;
--DROP TABLE SESSION.RetrievedItem;
--CLOSE SELECT_HIGHLEVEL;
--CLOSE SELECT_CURSOR;
--BEGIN
--DROP TABLE SESSION.RetrievedItem;
--END;
END;
END P1
__________________
Anthony Robinson

"If I'm curt with you, it's because time is a factor here. I think fast, I talk fast, and I need you guys to act fast if you want to get out of this. So, pretty please - with sugar on top..."
Reply With Quote
  #2 (permalink)  
Old 05-12-04, 03:25
RKrick RKrick is offline
Registered User
 
Join Date: Feb 2002
Location: Germany
Posts: 141
Is there a reason for dropping the GTT explicitly?

<snip>
If a DECLARE GLOBAL TEMPORARY TABLE statement is specified within the SQL procedure compound statement (defined by BEGIN and END), the scope of the declared global temporary table is the connection, not just the compound statement, and the table is known outside of the compound statement. The table is not implicitly dropped at the END of the compound statement.
(...)
When the application process that declared T terminates or disconnects from the database, T is dropped and its instantiated rows are destroyed.
</snip>
__________________
Rodney Krick
Reply With Quote
  #3 (permalink)  
Old 05-12-04, 09:24
ansonee ansonee is offline
Registered User
 
Join Date: Feb 2002
Location: Minneapolis, MN
Posts: 253
he temporary tables generated are not getting dropped and thece doesn't get "recycled". We keep running of space in the temporry tablespace.
__________________
Anthony Robinson

"If I'm curt with you, it's because time is a factor here. I think fast, I talk fast, and I need you guys to act fast if you want to get out of this. So, pretty please - with sugar on top..."
Reply With Quote
  #4 (permalink)  
Old 05-12-04, 09:31
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Do you mean User temporary tablespace?


<QUOTE>
We keep running of space in the temporry tablespace.
</QUOTE>
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #5 (permalink)  
Old 05-12-04, 10:02
RKrick RKrick is offline
Registered User
 
Join Date: Feb 2002
Location: Germany
Posts: 141
As I see from your logic, you really don't need a GTT (neither a stored procedure). Just define a view for both cases. For the second one, you can use a CASE expression for setting the flags.

HTH
__________________
Rodney Krick
Reply With Quote
  #6 (permalink)  
Old 05-12-04, 13:17
ansonee ansonee is offline
Registered User
 
Join Date: Feb 2002
Location: Minneapolis, MN
Posts: 253
Rodney:

Thanks - very good idea...!!!

Thanks to everyone who posted...
__________________
Anthony Robinson

"If I'm curt with you, it's because time is a factor here. I think fast, I talk fast, and I need you guys to act fast if you want to get out of this. So, pretty please - with sugar on top..."
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On