DATA Step, Macro, Functions and more

Get record ID just after inserting

Reply
Occasional Contributor
Posts: 8

Get record ID just after inserting

Hi all

I want to insert a record to a table in oracle and read its ID just after inserting it.

I don't want to lose my record while so many records might be inserted at the time.

How can I do that?

Thanks a lot

Super User
Posts: 10,028

Re: Get record ID just after inserting

Make a counterpart of original dataset, then using EXCEPT operation to see what record you have inserted.

data one;set sashelp.class;run;
data two;set sashelp.class;run; 
proc sql;
insert into one values('Patrick','M',30,52.2,89.4);


 select * from one 
  except
 select * from two;
quit;

Ksharp

Occasional Contributor
Posts: 8

Get record ID just after inserting

Dear Ksharp

I read the values from table 'three' and insert it to 'one', how can I keep the  rsult of (select *... ) in a table? It seems that it doesn't understand the code.

my code is:

data three;

input name $ sex$ age@@ height@@ weight@@;

cards;

Patrick M 30 52.2 89.4

;

run;

proc sql;

  insert into one (name, sex, age, height, weight)

  select name, sex, age, height, weight

  from three;

select * from one

  except

select * from two;

quit;

run;

Super User
Posts: 10,028

Re: Get record ID just after inserting

OK. Easy.

data one; set sashelp.class; run;
data two; set sashelp.class;run;
data three;
input name $ sex$ age height weight;
cards;
Patrick M 30 52.2 89.4
Peter M 40 54.3 100.2
;
run;

proc sql;
insert into one select * from three;

create table want as
 select * from one 
  except
 select * from two;
quit;



Ksharp

Occasional Contributor
Posts: 8

Get record ID just after inserting

another question:

Does this code guarantee that we read the inserted record?

I mean if another data inserted from another procedure while our proc sql is running, does it guarantee that the result is our record?

this procedure lock the table while runing or not?

Super User
Posts: 10,028

Get record ID just after inserting

Just as Tom said, I think you need some help from Oracle Administrator .

If you are familiar with Oracle ,why not do it in Oracle?

Ksharp

Super User
Super User
Posts: 7,050

Get record ID just after inserting

Why do you need to know the ID number generated by the Oracle database?

If you are not competing against other proceses then just query the database to find the record you just inserted.  Otherwise if you really need to get the number that Oracle generated you might need to trigger a stored procedure in Oracle that would insert the record and return the new ID as the result.

Ask a Question
Discussion stats
  • 6 replies
  • 219 views
  • 3 likes
  • 3 in conversation