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

I need a small help from you all,

I have a table tab_1 with empid as primary key and I also have one more table tab_2 which also contains empid with no relation like primary or foreign key.

I need a procedure that if I insert empid details into tab_1 then i want to reflect the empid value to be inserted into tab_2 automatically.

Thank you all,
Posted
Comments
Anil Honey 206 21-May-12 6:57am    
How your inserting Empid in first table,Can u give me sample code
jaipal0908 21-May-12 7:03am    
I written one stored procedure to insert the values into tab_1 like..
Insert into CaseMaster(empId,empTitle,empDesc)
values(@emp_Id,@emp_Title,@emp_description)

Create a Trigger on tab_1 for inserted
and insert records into tab_2 in that trigger.
 
Share this answer
 
Comments
Unareshraju 21-May-12 7:15am    
is it possible ?
is there any associated keys either forenkey or primary key.
sunandandutt 21-May-12 7:22am    
no need
its possible. because in 2nd table you are just inserting records.
sunandandutt 21-May-12 7:23am    
do you have knowledge of trigger?
jaipal0908 21-May-12 7:48am    
Yes i have knowledge on trigger but i am using a stored procedure to insert the values into tab_1. Can inserting the empid value to the tab_2 be done using this sp.?If yes can anyone send me the code in sp?


Anil Honey 206 21-May-12 7:26am    
But its the bad way Through Trigger your inserting the values in Table 2 instead of Trigger we can insert through Code.He should maintain Relation ship between two tables.
Hi..use the scope_identity() key to insert in second table.
You can use SP.
 
Share this answer
 
Comments
jaipal0908 21-May-12 9:45am    
Hi madhusudhan,

Thank you very much,
Can you send me sample sp taking two tables and dummy columns.

Thank you,
Take a look at below example.

Ex : Table_1,Table_2
Table_1 with the Cols[empid(PK),empName]
Table_2 with the Cols[empid,empDecsr]

Create Procedure [name]
[Parameters]
As Begin

Declare @empid bigint
Insert into Table_1(empName)values('Madhu')

set @empid=(SELECT SCOPE_IDENTITY())

Insert into Table_2(empid,empDecsr)values(@empid,'xyz')

End

Hope this is helpful for you.

Regards
Madhu
 
Share this answer
 
Comments
jaipal0908 21-May-12 10:03am    
Thank you very much madhu

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