Home » SQL & PL/SQL » SQL & PL/SQL » Oracle GTT vs SQL Temporary Table
icon7.gif  Oracle GTT vs SQL Temporary Table [message #626357] Mon, 27 October 2014 04:58 Go to next message
SunilJdh
Messages: 17
Registered: October 2014
Location: India
Junior Member

I just want to know that whether is it correct to create GTT and Drop in dynamic sql in Oracle. as we do in mssql for example select * into #t from table_name; and then after using #t drop temporary table.
Re: Oracle GTT vs SQL Temporary Table [message #626359 is a reply to message #626357] Mon, 27 October 2014 05:00 Go to previous messageGo to next message
John Watson
Messages: 8929
Registered: January 2010
Location: Global Village
Senior Member
Welcome to the forum. Please read our OraFAQ Forum Guide and please read How to use [code] tags and make your code easier to read

You never create and drop GTTs dynamically in an Oracle database. You create them once, and the definition is then visible to all sessions.
Re: Oracle GTT vs SQL Temporary Table [message #626360 is a reply to message #626357] Mon, 27 October 2014 05:01 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
GTT should be made only once & used as necessary.

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/

Re: Oracle GTT vs SQL Temporary Table [message #626361 is a reply to message #626359] Mon, 27 October 2014 05:03 Go to previous messageGo to next message
SunilJdh
Messages: 17
Registered: October 2014
Location: India
Junior Member

thanks i think i got answer.
Re: Oracle GTT vs SQL Temporary Table [message #626364 is a reply to message #626361] Mon, 27 October 2014 05:26 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

In addition, most of the time you don't need any GTT.
Oracle is able to handle very complex queries without using any temporary table.

Re: Oracle GTT vs SQL Temporary Table [message #626440 is a reply to message #626364] Mon, 27 October 2014 22:49 Go to previous messageGo to next message
Kevin Meade
Messages: 2103
Registered: December 1999
Location: Connecticut USA
Senior Member
It is true that most uses for temp tables are such that

1. the GTT is created ahead of time once and everyone can get their own copy so to speak.
2. Oracle is very smart about query rewrites and thus GTT are not normally needed.

I suspect though that the OP may be porting from SQL Server to Oracle? If so, one consideration may be to port without lots of changes and thus they may be looking for techniques that mimic what SQL Server does. To that end, just so the OP is aware, it is certainly possible to create GTT on the fly and drop them when no longer needed. Of course if you are doing this, there is no need for them to be an Oracle GTT. They would just as easily be normal tables unless you have a very unique requirement.

I am certainly not in favor of such an approach in Oracle. This certainly requires more privileges than would normally be given to production jobs. But we all work in a world that demands some amount of practicality. Budgets for ports often falls into such a category. Just say'in.

Kevin
Re: Oracle GTT vs SQL Temporary Table [message #626443 is a reply to message #626364] Tue, 28 October 2014 00:14 Go to previous messageGo to next message
SunilJdh
Messages: 17
Registered: October 2014
Location: India
Junior Member

My Requirement is that My .Net Form Application Accepts Excel Files converts them to xml and calls the oracle procedure.
In Oracle Procedure I Get Heavy XML. I have to Join that xml to severel oracle tables and extracting matched and unmatched sets. The Unmatched (New data From XML) set is inserted into relevant tables and Matched Rows are returned back to client (.net application) for further correction.

pls. tell me how do i do this without using GTT this would be a guide for me to migrate from MSSQL to ORACLE.

Pls. visit this link where i have put my dummy query and xml file.

https://community.oracle.com/message/12689970 Smile

if you require i will paste query here.

[Updated on: Tue, 28 October 2014 02:35]

Report message to a moderator

Re: Oracle GTT vs SQL Temporary Table [message #626479 is a reply to message #626443] Tue, 28 October 2014 06:46 Go to previous messageGo to next message
Kevin Meade
Messages: 2103
Registered: December 1999
Location: Connecticut USA
Senior Member
I guess the first question to ask is why you want to use temporary tables? Do you have existing code you are trying to migrate and are hoping to do as little modification as possible? Or are you willing to rewrite some pieces of the code to be Oracle centered instead?

Oracle has several features for dealing with XML. You can even load it directly to tables if you want. But none of this requires temporary tables.

You should take a moment to understand what you mean by a temporary table. What is your expectation? What does SQL Server mean by the term? What does Oracle mean by the term?

After reading the posts at the link it seems you might want to look ad DYNAMIC SQL and the PL/SQL command EXECUTE IMMEDIATE. With this you can dynamically create a table as needed and drop it when done. However to successfully use the dynamically created table, you will be required to changea all your other code into dynamic sql of some sort as well since compile time binding will not be possible.

For example:

07:43:33 SQL> declare
07:43:36   2     sqlv varchar2(32000);
07:43:36   3  begin
07:43:36   4     sqlv := 'CREATE TABLE KEVTEMP1 (A INTEGER NOT NULL,B INTEGER NOT NULL,C INTEGER NOT NULL)';
07:43:36   5     EXECUTE IMMEDIATE SQLV;
07:43:36   6     sqlv := 'INSERT INTO KEVTEMP1 VALUES (1,2,3)';
07:43:36   7     EXECUTE IMMEDIATE SQLV;
07:43:36   8     commit;
07:43:36   9     sqlv := 'DROP TABLE KEVTEMP1';
07:43:36  10     EXECUTE IMMEDIATE SQLV;
07:43:36  11  end;
07:43:36  12  /

PL/SQL procedure successfully completed.


This create a table on the fly, loads a row, commits that insert, then drops the table.

But notice also that all code related to the dynamically created table is also executed dynamically. Makes it harder to code and test.

As to the idea of using 100 new tables, one to support each of your 100 procedures... why not. Put these tables in their own schema and have at it. They won't clutter anything else up and they will be part of a specific application function you can isolate that way. If your goal is to port without a lot of code changes then do what you did before and don't worry about the proliferation of tables in a special purpose applcation schema.

Good luck. Your going to need it. Kevin

[Updated on: Tue, 28 October 2014 06:59]

Report message to a moderator

Re: Oracle GTT vs SQL Temporary Table [message #626536 is a reply to message #626479] Tue, 28 October 2014 12:14 Go to previous messageGo to next message
SunilJdh
Messages: 17
Registered: October 2014
Location: India
Junior Member

I learned that creating GTT on fly & droping after using it is bad coding practice in Oracle because it's DDL & generally should be avoided. So please tell me how do I do this without using GTT or collection As I have done in provided link ?

https://community.oracle.com/message/12689970

[Updated on: Tue, 28 October 2014 12:20]

Report message to a moderator

Re: Oracle GTT vs SQL Temporary Table [message #626537 is a reply to message #626536] Tue, 28 October 2014 12:58 Go to previous messageGo to next message
Kevin Meade
Messages: 2103
Registered: December 1999
Location: Connecticut USA
Senior Member
Yes in general that is a bad practice.

But like anything you have to use your own head and judge what you need for yourself. If you can save several hundred hours of new developement and testing by doing a BAD PRACTICE then maybe it deserves a second look.

However I think the issue here is you are not understanding what a GTT in Oracle is and thus why it is not normally necessary to create these on the fly. The table I created in the example provided above is not a GTT.

But, I would suggest that if you know the details of the tables you will need, just create them once and then use them. That is the technique. Otherwise, if you really do not know what the tables look like till execution time then you must create the dynamically as I showed, or not use tables at all and instead work out dynamically constructed result sets. Week defined ref cursors will do that.

Kevin
Re: Oracle GTT vs SQL Temporary Table [message #626539 is a reply to message #626537] Tue, 28 October 2014 14:47 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
What makes a GTT table nice is that you could have 50000 people using it at the same time and each one would only see their own data, even though they are all referencing the same table name.
Re: Oracle GTT vs SQL Temporary Table [message #626546 is a reply to message #626537] Tue, 28 October 2014 18:53 Go to previous messageGo to next message
SunilJdh
Messages: 17
Registered: October 2014
Location: India
Junior Member

Indeed whats in your example Is not GTT But its a DDL and it issues an implicit commit so it would or could commit the transaction which is undesirable. And collections/arrays are incapable handling large result sets that's why i think I have to use the GTT and there is no other option a available here.
Re: Oracle GTT vs SQL Temporary Table [message #626547 is a reply to message #626546] Tue, 28 October 2014 19:00 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>pls. tell me how do i do this without using GTT this would be a guide for me to migrate from MSSQL to ORACLE.

We don't have your data.
We don't have your tables.
We don't have your specific requirements.

All of the above make it impossible to provide you any coded solution.

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/
Re: Oracle GTT vs SQL Temporary Table [message #626551 is a reply to message #626547] Tue, 28 October 2014 19:07 Go to previous messageGo to next message
SunilJdh
Messages: 17
Registered: October 2014
Location: India
Junior Member

Below is Table, GTT and Procedure & XMLData. I just want to know if this can be done without the use of GTT ??

GTT :

CREATE GLOBAL TEMPORARY TABLE GTT_MACHINE
(
MACHINEID NUMBER(5, 0) NOT NULL
, MACHINENO NVARCHAR2(50) NOT NULL
, CARDNO NVARCHAR2(50) NOT NULL
, BATCHNO NVARCHAR2(50)
, VERIFYKEY NVARCHAR2(50)
, MACHINEDETAIL NVARCHAR2(250)
, CARDDETAIL NVARCHAR2(250)
, MACHINESTATUS NUMBER(1, 0)
, STOCKEFFECTED NUMBER(1, 0)
, ISACTIVE NUMBER(1, 0)
)
ON COMMIT PRESERVE ROWS
NOPARALLEL;

TABLE:

CREATE TABLE MACHINE
(
MACHINEID NUMBER(5, 0) NOT NULL
, MACHINENO NVARCHAR2(50) NOT NULL
, CARDNO NVARCHAR2(50) NOT NULL
, BATCHNO NVARCHAR2(50)
, VERIFYKEY NVARCHAR2(50)
, MACHINEDETAIL NVARCHAR2(250)
, CARDDETAIL NVARCHAR2(250)
, MACHINESTATUS NUMBER(1, 0)
, STOCKEFFECTED NUMBER(1, 0)
, ISACTIVE NUMBER(1, 0)
, CONSTRAINT MACHINE_PK PRIMARY KEY
(
MACHINEID
)
ENABLE
)
ORGANIZATION INDEX;

PROCEDURE:

create or replace PROCEDURE BULK_MACHINE_IMPORT(XMLDATA IN xmltype)
AS
RC SYS_REFCURSOR;
MACHINE_ID NUMBER(5,0);
ret_msg NVARCHAR2(500) := 'R0';
begin
--delete from GTT_machine purge;
insert into GTT_MACHINE
SELECT Row_number() over(order by 1) MachineID, XT.*
from XMLTABLE('//DataSet/MachineInwards'
PASSING XMLDATA
columns
"MachineNo" nvarchar2(50) PATH 'MachineNo'
, "CardNo" nvarchar2(50) PATH 'CardNo'
, "BatchNo" nvarchar2(50) PATH 'BatchNo'
, "VerifyKEY" nvarchar2(50) PATH 'VerifyKEY'
, "MachineDetail" nvarchar2(250) PATH 'MachineDetail'
, "CardDetail" nvarchar2(250) PATH 'CardDetail'
, "MachineStatus" number(1,0) PATH 'MachineStatus'
, "StockEffected" number(1,0) PATH 'StockEffected'
, "IsActive" NUMBER(1,0) PATH 'IsActive' ) XT;

lock table MACHINE in exclusive mode wait 5;

select NVL(max(MACHINEID),0)
into MACHINE_ID
FROM MACHINE;

/*Valid Records*/
INSERT INTO MACHINE
SELECT (T1.MACHINEID + machine_id) "MACHINEID",
T1.MACHINENO,
T1.CARDNO,
T1.BATCHNO,
T1.VERIFYKEY,
T1.MACHINEDETAIL,
T1.CARDDETAIL,
T1.MACHINESTATUS,
T1.STOCKEFFECTED,
T1.ISACTIVE
from GTT_MACHINE T1
LEFT JOIN (SELECT MACHINEID ,MACHINENO FROM MACHINE) T2 ON upper(T1.MACHINENO) = upper(T2.MACHINENO)
WHERE T2.MACHINEID IS NULL;

if (nvl(sql%ROWCOUNT,0) > 0) then
ret_msg := 'R101'; --Insert Successfull
end if;

/*Invalid (Duplicate) Records Returned to Client*/
OPEN rc FOR SELECT T1.MACHINEID,
T1.MACHINENO,
T1.CARDNO,
T1.BATCHNO,
T1.VERIFYKEY,
T1.MACHINEDETAIL,
T1.CARDDETAIL,
T1.MACHINESTATUS,
T1.STOCKEFFECTED,
T1.ISACTIVE from GTT_MACHINE T1
inner join ( select MACHINENO from MACHINE ) T2 on upper(T1.MACHINENO) = upper(T2.MACHINENO);

if (rc%isopen) then
DBMS_SQL.RETURN_RESULT(RC); --Implicit Results
end if;

if(RET_MSG = 'R101') then
commit;
else
rollback;
end if;
end BULK_MACHINE_IMPORT;

XML Data For Testing Procedure:
https://drive.google.com/open?id=0BwAVQqYmX0-zWlJzeHQwdVlGTjQ&authuser=0

[Updated on: Wed, 29 October 2014 07:13]

Report message to a moderator

Re: Oracle GTT vs SQL Temporary Table [message #626594 is a reply to message #626537] Wed, 29 October 2014 11:42 Go to previous messageGo to next message
SunilJdh
Messages: 17
Registered: October 2014
Location: India
Junior Member

Indeed whats in your example Is not GTT But its a DDL and it issues an implicit commit so it would or coil's commit the transaction which is undesirable. And collections/arrays are incapable handling large result sets that's why i think I have to use the GTT and there is no other option a available here.
Re: Oracle GTT vs SQL Temporary Table [message #626595 is a reply to message #626594] Wed, 29 October 2014 11:58 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Why do you think the above code needs a GTT?
Just replace the GTT in the insert into machine with the select statement used to populate the gtt in the first place.
I'm not sure why you think you need the lock table either.
Re: Oracle GTT vs SQL Temporary Table [message #626597 is a reply to message #626595] Wed, 29 October 2014 13:03 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
say your had a gtt call junk filled using the query select col1,col2,col3 from my_table.

and your select was

select var1,var2
from table2
where var1 in (select col1 from junk);

you can easly replace that with

select var1,var2
from table2
where var1 in (select col1 from my_table);

This is a simple example but everywhere you use a gtt simply replace it with the original select. you can even do something mlike

select col1,col2,col3
from
(select var1,var2
from my_table) a,
table2 b
where a.col1 = b.var1;
Re: Oracle GTT vs SQL Temporary Table [message #626607 is a reply to message #626595] Wed, 29 October 2014 21:54 Go to previous messageGo to next message
SunilJdh
Messages: 17
Registered: October 2014
Location: India
Junior Member

Suppose Some Element Name are Changed in XML Then How many Places I have to make Changes if I Remove GTT. and also every time i have to pull xmltable to use it in a join. will it not be heavy instead of using a gtt.??

Why I Lock Table:
In a multiuser environment is it not mandatory to lock a table where the primary key is not autogenerated and every time you insert a new records you pull max primary key (in my case its a numeric value) adds 1 and insert that record to table. Will locking a Table not prevent inconsistent data ??
Re: Oracle GTT vs SQL Temporary Table [message #626621 is a reply to message #626607] Thu, 30 October 2014 01:45 Go to previous messageGo to next message
Littlefoot
Messages: 21807
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Why don't you switch to a sequence (instead of MAX() + 1)?
Re: Oracle GTT vs SQL Temporary Table [message #626625 is a reply to message #626621] Thu, 30 October 2014 02:13 Go to previous messageGo to next message
SunilJdh
Messages: 17
Registered: October 2014
Location: India
Junior Member

I have around 500+ Tables in my Database, What I Understand from your statement is that i have to create 500+ sequences for each unique table in database. Pls. correct me if i am wrong. and nextval always return nextvalue in the sequence. and in case when a transaction is failed then what would be the new call to nextval function. ??

[Updated on: Thu, 30 October 2014 02:17]

Report message to a moderator

Re: Oracle GTT vs SQL Temporary Table [message #626626 is a reply to message #626625] Thu, 30 October 2014 02:23 Go to previous messageGo to next message
Littlefoot
Messages: 21807
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Nope, one sequence can be used for all your tables. A sequence doesn't guarantee gapless series of numbers, but they will certainly be unique.
Re: Oracle GTT vs SQL Temporary Table [message #626630 is a reply to message #626626] Thu, 30 October 2014 02:39 Go to previous messageGo to next message
SunilJdh
Messages: 17
Registered: October 2014
Location: India
Junior Member

then every table will hold different ids if one table has a record with id=50 then other tables will hold id <> 50; this will be very odd looking series also in my case if a single table holds around 2 million rows then i can't tell by just visualising the records that which are new records or which were inserted or updated earlier. this is not the correct solution for my design i think.

[Updated on: Thu, 30 October 2014 02:40]

Report message to a moderator

Re: Oracle GTT vs SQL Temporary Table [message #626633 is a reply to message #626630] Thu, 30 October 2014 02:44 Go to previous messageGo to next message
Littlefoot
Messages: 21807
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
I'm not forcing you to use a sequence. If you think that it is a worse solution than the one you currently have, abandon it.
Re: Oracle GTT vs SQL Temporary Table [message #626635 is a reply to message #626633] Thu, 30 October 2014 02:48 Go to previous messageGo to next message
SunilJdh
Messages: 17
Registered: October 2014
Location: India
Junior Member

No Seq Table has their own usability And I have already used them in my database as per requirement.
Thanks I learned something new from this discussion.
Re: Oracle GTT vs SQL Temporary Table [message #626638 is a reply to message #626630] Thu, 30 October 2014 03:34 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
SunilJdh wrote on Thu, 30 October 2014 02:54
Suppose Some Element Name are Changed in XML Then How many Places I have to make Changes if I Remove GTT.

The same number of places as you would have to change if you left the gtt in. You're going to have a select against the xml with or without a gtt.
If you change the xml structure, that select will have to be changed.

SunilJdh wrote on Thu, 30 October 2014 02:54

and also every time i have to pull xmltable to use it in a join. will it not be heavy instead of using a gtt.??

The join insert select will probably take more time to run than the simple insert select into the gtt. But it'll take less time to run than the insert/select into the gtt plus the insert/select into the proper table.
Test it.

SunilJdh wrote on Thu, 30 October 2014 02:54

Why I Lock Table:
In a multiuser environment is it not mandatory to lock a table where the primary key is not autogenerated and every time you insert a new records you pull max primary key (in my case its a numeric value) adds 1 and insert that record to table. Will locking a Table not prevent inconsistent data ??

That's an argument for using sequences.

SunilJdh wrote on Thu, 30 October 2014 07:39
then every table will hold different ids if one table has a record with id=50 then other tables will hold id <> 50; this will be very odd looking series also in my case if a single table holds around 2 million rows then i can't tell by just visualising the records that which are new records or which were inserted or updated earlier. this is not the correct solution for my design i think.

The best way to tell when a record was inserted is to have a date column that records when it was inserted. It'll be far clearer than what you are doing and avoid locking large chunks of the database.
Re: Oracle GTT vs SQL Temporary Table [message #626646 is a reply to message #626638] Thu, 30 October 2014 05:15 Go to previous message
SunilJdh
Messages: 17
Registered: October 2014
Location: India
Junior Member

thank u.
by your response i learned that GTT have Their place & Should be used where they should be. for example if i have to use xmltable in a single place then its correct to avoid gtt but if i have to use it more than 1 then gtt must be used (One Time Insert Into GTT and Using multiple Times).

thanks again.
Previous Topic: XMLTYPE procedure
Next Topic: Oracle Function to get ISO Country Code
Goto Forum:
  


Current Time: Fri Apr 19 03:02:55 CDT 2024