Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Default value trigger
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00810601
Message ID:
00810890
Views:
14
Hi Gilles,

A trigger in SQL Server fires once per SQL command regardless of how many records are affected by that commnad. You've to take it into account when you code your triggers. Below you'll find sample Insert and Update trigger that does what you want. It assumes that table has Primiry Key field PK.
IF EXISTS (SELECT name 
	   FROM   sysobjects 
	   WHERE  name = N'trg_punch_iu' 
	   AND 	  type = 'TR')
    DROP TRIGGER trg_punch_iu
GO

CREATE TRIGGER trg_punch_iu
ON test.dbo.punch
FOR INSERT, UPDATE 
AS 
SET NOCOUNT ON
If UPDATE([date]) OR UPDATE(JOUR_HEURE)
BEGIN
  UPDATE dbo.punch 
	SET DATE_PAYE = CASE WHEN CONVERT(char(8), inserted.JOUR_HEURE, 108) < '05:00:00'
			THEN inserted.[date]-1 ELSE inserted.[date] END
    FROM dbo.punch JOIN inserted ON inserted.pk = punch.pk
END
GO
>I'm new with triggers in SQL Server....
>
>In table PUNCH, I have 3 columns: DATE (datetime), JOUR_HEURE (datetime) and DATE_PAYE (datetime). Obviously the table has more than 3 columns ;-)
>
>I want to have the value of DATE_PAYE derived from DATE and JOUR_HEURE when an INSERT or an UPDATE is fired agains that table.
>
>The rule is: IF JOUR_HEURE is before 5AM (the date portion is irrelevant), the DATE_PAYE should be DATE - 1 DAY, ELSE DATE_PAYE = DATE.
>
>I'm thingking of using a trigger (other ways ???) and I want the trigger to fire (on updates) only when IF DATE or JOUR_HEURE changes (USING COLUMNS_UPDATED, since DATE and )
>
< snip >
--sb--
Previous
Reply
Map
View

Click here to load this message in the networking platform