views:

1688

answers:

2

I have two Tables.

I want to insert the same record on both tables at the same time.
I.e., while I insert a record for the first table, this same record also is inserted in the second table using a trigger.

Do you have any experience/advice in this process ?

+3  A: 

if you're using stored procedures you can easily manage this

CREATE PROCEDURE sp_Insert
@Value varchar(10)
AS
insert into table1 (...,...) values (@value,...)
insert into table2 (...,...) values (@value,...)
Erik404
That is the easy way to handle it, in one SP, much more readable and easy to debug. Triggers can be trickey as once setup you can forget they are there and not understand why data is changing.
schooner
I don;t know what your environment is but if you're using C# in VS take a look at this tutorial. It's about using ADO.Net and stored procedures in C#.http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson07.aspx
Erik404
+1  A: 

I would suggest using Erik's method over a trigger. Triggers tend to cause performance issues, and a lot of times, you forget that the trigger exists, and get unexpected behavior. If you do want to use a trigger however, it will work. here is an example:

CREATE TRIGGER trgTest ON Test
FOR INSERT
AS
INSERT Test2
     (Id, value)
SELECT Id, Value 
FROM Inserted
Paul McCann