Tuesday, February 2, 2010

How to Create Trigger in sql

//posted by anil jain

CREATE TRIGGER trig1 ON trig_example FOR INSERT
as
IF (SELECT COUNT(*) FROM INSERTED
WHERE salary > 100000) > 0
BEGIN
print "TRIG1 Error: you attempted to insert a salary > $100,000"
ROLLBACK TRANSACTION
END


CREATE trigger Tg_Update_directory_date on directory
for insert
as

insert into directory_update (directory_id,update_date)
select max(directory_id) as directory_id, getdate() from directory)


CREATE TRIGGER tr_Orders_INSERT
ON Orders
FOR INSERT
AS
IF (SELECT COUNT(*) FROM inserted WHERE Ord_Priority = 'High') = 1
BEGIN
PRINT 'Email Code Goes Here'
END

No comments:

Post a Comment