Results 1 to 6 of 6

Thread: A Better Way??

  1. #1
    Join Date
    Feb 2002
    Location
    Minneapolis, MN
    Posts
    253

    Unanswered: 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..."

  2. #2
    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

  3. #3
    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..."

  4. #4
    Join Date
    Aug 2001
    Location
    UK
    Posts
    4,650
    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.

  5. #5
    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

  6. #6
    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..."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •