Alternate method to global temp tables for Oracle Stored Procedure

青春壹個敷衍的年華 提交于 2019-12-04 06:16:06

问题


I have read and understand that Oracle uses only global temp tables unlike MS SQL which allows #temp tables. The situation that I have would call for me to create hundreds of Global temp tables in order to complete the DB conversion I am working on from MS SQL to Oracle. I want to know if there is another method out there, within a Oracle Stored Procedure, other than creating all of these tables which will have to be maintained in the DB.

Thank You


回答1:


" Most of the time the only thing the temp tables are used within a stored proc and then truncated at the end. We do constant upgrades to our applications and having them somewhat comparable ensures that when a change is made in one version that it can be easily merged to the other."

T-SQL Temp tables are essentially memory structures. They provide benefits in MSSQL which are less obvious in Oracle, because of differences in the two RDBMS architectures. So if you were looking to migrate then you would be well advised to take an approach more fitted to Oracle.

However, you have a different situation, and obviously keeping the two code bases in sync will make your life easier.

The closest thing to temporary tables as you want to use them are PL/SQL collections; specifically, nested tables.

There are a couple of ways of declaring these. The first is to use a SQL template - a cursor - and define a nested table type based on it. The second is to declare a record type and then define a nested table on that. In either case, populate the collection variable with a bulk operation.

declare
    -- approach #1 - use a cursor
    cursor c1 is 
          select *
          from t23;
    type nt1 is table of c1%rowtype;
    recs1 nt1;

    -- approach #1a - use a cursor with an explicit projection
    cursor c1a is 
          select id, col_d, col_2 
          from t23;
    type nt1a is table of c1a%rowtype;
    recs1 nt1a;


    -- approach #2 - use a PL/SQL record
    type r2 is record (
        my_id number
        , some_date date
        , a_string varchar2(30)
    );
    type nt2 is table of r2;
    recs2 nt2;
begin
    select *
    bulk collect into recs1
    from t23;

    select id, col_d, col_2
    bulk collect into recs2
    from t23;
end;
/

Using a cursor offers the advantage of automatically reflecting changes in the underlying table(s). Although the RECORD provides the advantage of stability in the face of changes in the underlying table(s). It just depends what you want :)

There's a whole chapter in the PL/SQL reference manual. Read it to find out more.



来源:https://stackoverflow.com/questions/25454311/alternate-method-to-global-temp-tables-for-oracle-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!