Thursday, February 16, 2012

auto trigger

I don't know how to use an auto trigger. That's mean when we insert, delete
or update infomation on a table, the information will insert, delete or upda
te on another table ( we don't have to create any trigger to do that ).
Please tell me the way to do. Thanks !TuanAnh,
I'm interested in where you have heard of "Auto Triggers" to populate a
table? Indexed views have a similar functionality but are obviously not a
table. In SQL Server if you wanted an audit trail, you would have to use
after triggers and there is no other automatic "auto trigger" way of doing
it that I have ever heard of. Perhaps you are thinking of transactional
replication? This doesn't use triggers (providing the subscribers don't
change the data)?
Regards,
Paul Ibison|||I think that you want to create a trigger that will allow you to log =
data to a table. Is that correct? This example will get you started:
USE tempdb
GO
CREATE TABLE main (SomeColumn int, AnotherColumn varchar(10))
CREATE TABLE mainAudit (SomeColumn int, AnotherColumn varchar(10))
GO
CREATE TRIGGER mainInsert ON main=20
FOR INSERT
AS
INSERT INTO mainAudit (SomeColumn, AnotherColumn)
SELECT SomeColumn, AnotherColumn FROM inserted
GO
CREATE TRIGGER mainUpdate ON main=20
FOR UPDATE
AS
UPDATE mainAudit SET AnotherColumn =3D B.AnotherColumn
FROM mainAudit A JOIN inserted B ON A.SomeColumn =3D B.SomeColumn
GO
INSERT INTO main (SomeColumn, AnotherColumn) VALUES (1, 'test')
SELECT * FROM main
SELECT * FROM mainAudit
GO
INSERT INTO main (SomeColumn, AnotherColumn) VALUES (2, 'testing')
SELECT * FROM main
SELECT * FROM mainAudit
GO
INSERT INTO main (SomeColumn, AnotherColumn ) SELECT SomeColumn, =
AnotherColumn + 'new' FROM main
SELECT * FROM main
SELECT * FROM mainAudit
GO
UPDATE main SET AnotherColumn =3D 'foo'=20
SELECT * FROM main
SELECT * FROM mainAudit
GO
UPDATE main SET AnotherColumn =3D 'hello' WHERE SomeColumn =3D 1
SELECT * FROM main
SELECT * FROM mainAudit
GO
UPDATE main SET AnotherColumn =3D 'world' WHERE SomeColumn =3D 2
SELECT * FROM main
SELECT * FROM mainAudit
GO
DROP TABLE main
DROP TABLE mainAudit
--=20
Keith
"TuanAnh" <anonymous@.discussions.microsoft.com> wrote in message =
news:8E4A7419-372A-4EC1-8701-C3EF347D217E@.microsoft.com...
> I don't know how to use an auto trigger. That's mean when we =
insert, delete or update infomation on a table, the information will =
insert, delete or update on another table ( we don't have to create any =
trigger to do that ).
> Please tell me the way to do. Thanks !

No comments:

Post a Comment