Skip to content

Insert Data in two table but the second table with FK

Beavon Oigazy

Asked Jan 21, 2016, 11:46 PM EST1 answer
Share
LinkedInX
Hello C# Corner,
 
I want to insert Data in two table same time, but the second table consist Foreigner Key ID of the first table(Primary Key). 
 
sample tables are below;

User Table:

Id 
Name
Email Address 

Subscription Table:

· IIId

· User Id

· Activation Code

· Subscription Type

· Start Date

· End Date

· Subscription Scope

 
Please, Possible someone to help me with T-SQL code or Explanation. Thanks,
Beavon 
 


·

 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
     use 
     
    SCOPE_IDENTITY() of second table column  User Id for insert first column primary key
    ex
    insert into  User Table values(@name,@EmailAdd)
     
    insert into Subscription Table values(SCOPE_IDENTITY() ,@EmailAdd) 
    0