vendredi 27 novembre 2020

I have two tables

I have two tables person and relatives

  • person table contains person_id (primary key) and name dob address
  • relatives table contains rel_id (key) and name , relation_type , person_id (foreign key)

I am trying to insert data to both tables from a single form at same time.

I found SCOPE_IDENTITY() can get last identity value created in the current session.

But I need to insert more than one relative details to the relatives table with key of person table.

insert into person (name,dob,address) values ('ABC','1990-05-05','XYZ');


insert into relatives(name,relation_type,person_id) values('Qubert','Uncle',SCOPE_IDENTITY());
insert into relatives(name,relation_type,person_id) values('Bob','Cousin',SCOPE_IDENTITY());
insert into relatives(name,relation_type,person_id) values('Alice','Niece',SCOPE_IDENTITY());

I get the value of the person_id correctly for the first entry to relatives, how can i get the same into the other queries also?

Is there any better way than this? Thanks and regards :)




Aucun commentaire:

Enregistrer un commentaire