Finding Data in Temp Tables



Here’s a simple little trick for Application Engines that have Temporary Tables assigned.

When you have temporary tables, you don’t know for sure which Temporary Table it is using.  You could write a quick select and change the number on the end until you find the one you are looking for, but here is a slightly quicker method.

Note: This is designed for Oracle, but you could easily change it up to work with SQL Server.

First, build a select statement that will list all of the tables.  We’ll assume our Temporary Table Record is MY_TEMP_TAO.  You can substitute with your specific record.

SELECT 'UNION SELECT ''' || TABLE_NAME || ''' TBL, A.* FROM SYSADM.' || TABLE_NAME || ' A'
FROM DBA_TABLES
WHERE table_name LIKE 'PS_MY_TEMP_TAO%';

Update (thanks to Nicolas): You can avoid having to remove the first UNION if you use something more like this:

SELECT 'SELECT ''' || TABLE_NAME || ''' TBL, A.* ' ||
 'FROM SYSADM.' || TABLE_NAME || ' A' ||
 case when count(*)over()=rownum
 then ';' else ' UNION ' end
FROM ALL_TABLES
WHERE table_name LIKE 'PS_MY_TEMP_TAO%';

The output should look something like this:

UNION SELECT 'PS_MY_TEMP_TAO' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO A
UNION SELECT 'PS_MY_TEMP_TAO1' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO1 A
UNION SELECT 'PS_MY_TEMP_TAO2' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO2 A
UNION SELECT 'PS_MY_TEMP_TAO3' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO3 A
UNION SELECT 'PS_MY_TEMP_TAO4' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO4 A
UNION SELECT 'PS_MY_TEMP_TAO5' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO5 A
UNION SELECT 'PS_MY_TEMP_TAO6' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO6 A;

You just simply need to delete the first Union to make something like this:

SELECT 'PS_MY_TEMP_TAO' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO A
UNION SELECT 'PS_MY_TEMP_TAO1' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO1 A
UNION SELECT 'PS_MY_TEMP_TAO2' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO2 A
UNION SELECT 'PS_MY_TEMP_TAO3' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO3 A
UNION SELECT 'PS_MY_TEMP_TAO4' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO4 A
UNION SELECT 'PS_MY_TEMP_TAO5' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO5 A
UNION SELECT 'PS_MY_TEMP_TAO6' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO6 A;

If you know your temp table has the process instance as the key and you know the one you are looking for, you could do something like this:

SELECT * FROM (
     SELECT 'PS_MY_TEMP_TAO' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO A
     UNION SELECT 'PS_MY_TEMP_TAO1' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO1 A
     UNION SELECT 'PS_MY_TEMP_TAO2' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO2 A
     UNION SELECT 'PS_MY_TEMP_TAO3' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO3 A
     UNION SELECT 'PS_MY_TEMP_TAO4' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO4 A
     UNION SELECT 'PS_MY_TEMP_TAO5' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO5 A
     UNION SELECT 'PS_MY_TEMP_TAO6' TBL, A.* FROM SYSADM.PS_MY_TEMP_TAO6 A;
) Z WHERE PROCESS_INSTANCE = 12345

Share

2 Comments

  1. Hi Stephen,

    What about :
    SELECT ‘SELECT ”’ || TABLE_NAME || ”’ TBL, A.* FROM SYSADM.’ || TABLE_NAME || ‘ A’
    ||case when count(*)over()=rownum then ‘;’ else ‘ UNION ‘ end
    FROM DBA_TABLES
    WHERE LIKE ‘PS_MY_TEMP_TAO%';

    Nicolas.

  2. digitaleagle says:

    Nicolas,

    Thanks so much for improving my SQL! I added it to the post above.

    Thanks,

    Stephen

Leave a comment