April 17, 2012 at 1:02 am
dear all,
i just need simple stored procedure to add a single value into multiple tables which has already assigned win foreign and primary keay concept.
tab1 - id ,name -- id primary key
tab2 - id,address -- id foreign key to tab1.
when i insert values it should affect both the tables..
otherwise please tell me the concept..
April 17, 2012 at 2:11 am
ashokkumar.ss (4/17/2012)
dear all,i just need simple stored procedure to add a single value into multiple tables which has already assigned win foreign and primary keay concept.
tab1 - id ,name -- id primary key
tab2 - id,address -- id foreign key to tab1.
when i insert values it should affect both the tables..
otherwise please tell me the concept..
Is ID an Identity column in tab 1?
Concept is that you do the insert into tab 1 first, then use Scope_Identity() to retrieve the Id of the inserted row, then insert to tab 2 using the Id you've just retrieved.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
April 17, 2012 at 2:59 am
If the id in first table is an Identity, then you have to use Scope_Identity to know the last inserted Identity and then insert value into the other table with that Identity as id.
If the id in first table is not an identity, then you can simply insert the values in both the tables using Insert Queries or if your Stored procedure is Parameterized then you can simply feed the parameters to both the tables using Insert Queries.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply