Showing posts with label trigger. Show all posts
Showing posts with label trigger. Show all posts

Monday, March 19, 2012

Compare Triggers Values

Hi.
How to create a trigger before insert that if a new row is giong to be
inserted in a table the trigger compares a value of a column from the
table with the value of a column in the record to be inserted...
Regards
Muhammad Bilal
<bilal_4x@.hotmail.com>
*** Sent via Developersdex http://www.examnotes.net ***Use of inserted table and deleted table in the trigger can help you.
According to BOL
two special tables are used in trigger statements: the deleted table
and the inserted table. SQL Server 2000 automatically creates and
manages these tables. You can use these temporary, memory-resident
tables to test the effects of certain data modifications and to set
conditions for trigger actions;
Look at books online for more details and samples.
Or
Post your table with DDL and data and describe your problem.
Regards
Amish

Sunday, February 19, 2012

Commiting a trigger under certain conditions

I have a sitemapcache that caches nodes with a sqlcachedependency. Everything is working fine but one thing. Every time you visit a forum, the viewcount is changed, therefore raising the trigger and dropping my cache object. How do i make it so that the trigger is only fired if I update the Title or Description field?

ALTER TRIGGER [dbo].[sp_Forums_Topics_AspNet_SqlCacheNotification_Trigger]ON [dbo].[sp_Forums_Topics]
FOR INSERT,UPDATE,DELETE AS BEGIN
SET NOCOUNT ON
EXEC dbo.AspNet_SqlCacheUpdateChangeIdStoredProcedure N'sp_Forums_Topics'END

Hi,

You can use UPDATE() clause of transact sql to do that. I think it is only specific to 2005, but you could check it in 2000 too. Example is given below.

USE AdventureWorks;GOIFEXISTS (SELECT name FROM sys.objectsWHERE name ='reminder'AND type ='TR')DROP TRIGGER Person.reminder;GOCREATE TRIGGER reminderON Person.AddressAFTERUPDATE AS IF (UPDATE (StateProvinceID)ORUPDATE (PostalCode) )BEGINRAISERROR (50009, 16, 10)END;GO-- Test the trigger.UPDATE Person.AddressSET PostalCode = 99999WHERE PostalCode ='12345';GO