Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

What I am trying to do is to generate unique ids for queries created by users.

I am using oracle sequence to assign the query id, which is primary key in the table.

After creating the query, I want to display the query id created, which is basically the sequence no.

Thank you..!!

What I have tried:

Firstly, displaying the sequence.currval will not guarantee right outpput because within that time other user can insert query into database.

Secondly, if I select the id that is created by x user exactly at y time, that might work. But that requires another query to be fired.

Is there any alternative way?
Posted
Updated 1-Nov-17 22:37pm
Comments
GKP1992 2-Nov-17 2:31am    
As far as I understand, you insert the query into a table with query id as the primary key. Is that correct? Also, which version of Oracle are you using?
planetz 2-Nov-17 3:04am    
Yes GKP1992, that is correct. I am using oracle 10g.

1 solution

Hi,

You can use the returning clause to do that.

Example:
SQL
INSERT INTO {query_table} (query_id, query)
   VALUES (sequence.nextval, query_From_User)
RETURNING query_id into {a_defined_variable}


This will give you the id of the inserted record.
 
Share this answer
 
Comments
planetz 3-Nov-17 5:42am    
this query has to be in a procedure. right? else where can I declare 'a_defined_variable'?
GKP1992 3-Nov-17 7:19am    
I think it is called a block in Oracle. Goes like this

declare {var_name} {vartype} := {value};
begin
{write query that uses var here.}
end

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900