Skip to Main Content
  • Questions
  • How to create temp Sequence for multi sessions

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Trento.

Asked: September 15, 2010 - 5:57 pm UTC

Last updated: September 16, 2010 - 7:52 am UTC

Version: 9

Viewed 1000+ times

You Asked

Hi,
I'm using Global Temp table and I need to insert Identity (or Line_Number) column into it, but I also have multiple sessions for my sp and each case should have its own Line-Number, starting from 1.
Is there any kind of "temp sequence" in Oracle ? that works in the same fashion like GTT , so multiple sessions each will get fresh zero anytime they will refer it. Or probably even I created it with static name it will treated as a new ? I could not find this info yet.. If I create sequence then use it during the execution my sp (let say for 10sec) then delete it, can somebody else collide with me on this seq ??

I'd defenetely go with static sequence name if pssble, as our site has restriction for dynamic sql.

I also found out that ROWNUM will not work in my case.



Tx
Trent

and Tom said...

... and each case should have its own Line-Number, starting from 1. ..

rethink your requirement - they don't work like that (sequences), they never have - never will. They are not gap free, they are not necessarily sequential even.


I don't know why you would need it to start at one, you can get a sequential number when retrieving if you want:

(select gtt.*, row_number() over (order by something) rn from gtt)

or

(select gtt.*, rownum from (select * from gtt order by something) gtt)


you can use that anywhere you would normally reference just GTT in your query. If the something you order by is unique, this will return the same rownum values given the same data. Be prepared to be "slow" however.

The other alternative would be to use rownum or row_number on the query that populates the table.

But sequences - not a chance.



as our site has restriction for dynamic sql.

funny that, all sql is really dynamic sql under the covers. dynamic sql is not evil, developers can be evil and do things wrong with it (as they can with static sql). Rules like that really annoy me - they are very short sighted. It is true that dynamic sql in plsql should be the last resort - but it should be permitted when necessary.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A reader, September 17, 2010 - 3:54 pm UTC

all right in the eyeball;

More to Explore

Analytics

Analytic SQL got you confused? Check out Connor McDonald's complete video course.