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


I have two tables there are parent and child table.and i want to insert data into those tables , firstly i want to insert in parent table and i want to get Identity col value from parent and using this, i want to insert details information into child table.

Here am using stored procedure for inserting data to tables

ex:

SQL
create table parent(id int primary key,name varchar(40),class varchar(40))

create table child(id int foreign key references parent(id), age int,village varchar(50))


Thanks and Regards,

Dhineshkumar V
Posted
Updated 16-May-17 1:34am
Comments
OriginalGriff 26-Jun-13 9:27am    
And?
What is the problem?
What have you tried? Where are you stuck?

1 solution

Example sql query:
SQL
DECLARE @pid INT --parent id, where id is identity for parent table

INSERT INTO parent ([name], [class])
VALUES('Dhinesh kumar.V', 'last')

SELECT @pid = @@IDENTITY

INSERT INTO parent (pid, age, village)
VALUES(@pid, 21, 'Best Village')

Have a look here:
INSERT (T-SQL)[^]
@@IDENTITY[^]
 
Share this answer
 
Comments
Dhinesh kumar.V 28-Jun-13 5:21am    
Thanks buddy...
Maciej Los 28-Jun-13 6:43am    
You're welcome ;)
If your problem is solved, then mark this answer as solved (green button).
Member 11332842 16-May-17 7:35am    
hello
how to insert multiple rows in children table with the same main table identity plz?
CHill60 16-May-17 7:46am    
If you have a question of your own use the  Ask a Question  link at the top of the page. Include details of the table(s), some sample data and your expected results. Also include the code you have tried so far

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