SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 before update trigger
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

RB2K
Starting Member

4 Posts

Posted - 04/14/2008 :  04:52:31  Show Profile  Reply with Quote
Hi,

I'm trying to write an update trigger on sql 2005 but having a few issues. Basically I have a table with say 10 records in there. When an update occurs on a record and I want the trigger to recognise this and place a flag of X in a column to mark it.

The trigger then copies the record marked with X into another table. In the source table where the flag is marked X it will then update it with a GetDate function.

Can someone advise what I'm doing stupidly wrong. I want a before update function. But I'm getting an error on the Before Syntax. Any advise/help appreciated

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[Update_Flag]
ON [MYDB].[dbo].[SOURCETABLE]
BEFORE UPDATE
AS
Update [SOURCETABLE]
Set RecordUpdateFlag = 'X'

Update [SOURCETABLE]
Set RecordUpdateType='U' -- For Record Update

Insert into MYDB.dbo.[xSOURCETABLE]
Select * from [SOURCETABLE]
where RecordUpdateFlag = 'X'
and RecordUpdateType='U'

Update [SOURCETABLE]
Set RecordUpdateFlag= GetDate()

Update [xSOURCETABLE]
Set RecordUpdateFlag= GetDate()



harsh_athalye
Flowing Fount of Yak Knowledge

India
5509 Posts

Posted - 04/14/2008 :  05:17:19  Show Profile  Visit harsh_athalye's Homepage  Click to see harsh_athalye's MSN Messenger address  Send harsh_athalye a Yahoo! Message  Reply with Quote
I don't think BEFORE is a valid keyword when defining trigger in SQL Server 2005. However, you can use INSTEAD OF trigger.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29138 Posts

Posted - 04/14/2008 :  05:19:24  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Why a BEFORE update trigger?
CREATE TRIGGER dbo.trgUpdateFlag ON MyDB.dbo.SourceTable AFTER UPDATE
AS

UPDATE		t
SET		t.RecordUpdateFlag = GETDATE(),
		t.RecordUpdateType = 'U'
FROM		SourceTable AS t
INNER JOIN	inserted AS i ON i.PkCol = t.PkCol

INSERT		MyDB.dbo.xSourceTable
		(
			Col1,
			Col2,
			... ,
			RecordUpdateFlag,
			RecordUpdateType,
			...
		)
SELECT		Col1,
		Col2,
		... ,
		GETDATE() AS RecordUpdateFlag,
		'U' AS RecordUpdateType,
		...
FROM		inserted

E 12°55'05.25"
N 56°04'39.16"

Edited by - SwePeso on 04/14/2008 05:20:37
Go to Top of Page

RB2K
Starting Member

4 Posts

Posted - 04/14/2008 :  05:43:35  Show Profile  Reply with Quote
The reason for the before update command is unique. I basically when to identify and update a single record within a table. For example I have a source table of ten records. Once a record is being modified at run time I want to flag that. So when my trigger runs I can carry out my select query based upon that record being different to the rest. I don't want to update all records within a table and copy then to another. Only ones which have changed.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29138 Posts

Posted - 04/14/2008 :  05:52:14  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Yes, this is EXACTLY what I have done in my suggestion.
Just replace ".PkCol" with the name of the real primary key column.

Also, you have 5 DML statements, whereas I only use 2. This would suggestion my suggestion will run faster with less probability to create locks.


E 12°55'05.25"
N 56°04'39.16"

Edited by - SwePeso on 04/14/2008 05:53:40
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29138 Posts

Posted - 04/14/2008 :  05:58:04  Show Profile  Visit SwePeso's Homepage  Reply with Quote
quote:
Originally posted by RB2K

CREATE TRIGGER [dbo].[Update_Flag] 
ON [MYDB].[dbo].[SOURCETABLE]
BEFORE UPDATE 
AS 
Update [SOURCETABLE]
Set RecordUpdateFlag = 'X'

Update [SOURCETABLE]
Set RecordUpdateType='U' -- For Record Update

Insert into MYDB.dbo.[xSOURCETABLE]
Select * from [SOURCETABLE]
where RecordUpdateFlag = 'X' 
and RecordUpdateType='U'

Update [SOURCETABLE]
Set RecordUpdateFlag= GetDate()

Update [xSOURCETABLE]
Set RecordUpdateFlag= GetDate()


The statement

Update [xSOURCETABLE]
Set RecordUpdateFlag= GetDate()

is "stupidly" wrong, because that table has no informatio about what is being inserted in the current table.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000