Trigger

I want to write a trigger that updates one table after a value is inserted,updated or deleted into another table..
for instance i have this table userinfo which contains the employee details once a new value is entered into this table i want a trigger to update another table viz syncinfo, can anyone tell me the logic through which i can do this procedure?

Regards,
Venki

[388 byte] By [Macho] at [2008-1-10]
# 1

Read about Trigger in BOL and also there are many KB available in net

refer : http://www.sql-server-performance.com/articles/dev/triggers_2000_p1.aspx

http://www.sqlteam.com/article/an-introduction-to-triggers-part-i

In your scenario, you should create a trigger for Inser,Update,Delete in USERINFO table.

Madhu

MadhuKNair at 2007-10-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 2

Hi,

You can create trigger on the table where in following way:

CREATE TRIGGER syncinfo ON [userinfo]
FOR INSERT
AS
UPDATE p SET p.columnname='Test'
FROM anothertable p

Also you can do this by the stored procedure as triggres should be used in foll senarios:

1) if the database is de-normalized and requires an automated way to update redundant data contained in multiple tables 2) if customized messages and complex error handling are required, or if a value in one table must be validated against a non-identical value in another table.

Regards

Jayant

jayant_magic at 2007-10-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 3

Hi Venki,

You can do the same functionality through procedures.

I believe that you have procedures for insert/update/delete data into the userinfo table. On the same procedure, you can implement the update information in syncinfo table.

Thanks

Naras.

Naras at 2007-10-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 4

Hi,

You can also use:

Code Block

select * from inserted

in order to gather the new data, and

Code Block

select * from deleted

to gather the old values. You could, for instance, put them into variables and the update the other tables.

regards.

pdjota at 2007-10-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...

SQL Server

Site Classified