Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
can u say how to store the same values to different tables at a register time...
Posted

for example u inserting empid to two tables then write store procedure like

create procedure emp_id(@id int)as
begin
insert into table_1(empid) values(@id)
go
insert into table_2(empid),values(@id)
end
 
Share this answer
 
SQL
declare @Amount decimal(18,2)
set @Amount

insert into Table1(Amount)
select @Amount
insert into Table2(Amount)
select @Amount
 
Share this answer
 
If you need to store the same values in two tables at the same time then you have two options:

1) Write a stored procedure to do it (recommended)
2) Use a transaction to ensure that both writes occur together or neither do.

By preference, use a transaction inside a stored procedure.

But why do that? All you are doing is duplicating information, which is both inefficient, and can be dangerous if one set is not kept in synch with the other. Why not keep the duplicates in a separate table with links to the two tables you are referring it to?
For example: a company may have a separate billing and delivery address: Have an Addresses table and the Company table refers to two entries in the addresses table. Or the same address if the billing and delivery addresses are the same physical location.
 
Share this answer
 
Comments
Raghupathiraja 21-Oct-11 5:48am    
thank you...
[no name] 7-Nov-11 5:12am    
a question, why we can't prefer TRIGGER ?
OriginalGriff 7-Nov-11 5:40am    
A trigger doesn't guarantee that both table updates complete ok. If the triggered update fails, the triggering update remains, possibly with orphaned references.

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