Author |
Topic |
BigDelboy
Starting Member
3 Posts |
Posted - 2010-11-22 : 12:14:17
|
Hi allI new here and sorry if I am posting into the wrong section..I wish to create a trigger on a table which can identify when a row is updated and then update a date field within that row displaying the date it was last updated.I would be happy if someone could point out somewhere in you vast knowledge base here where I can get my answer..Kind regards,D. |
|
X002548
Not Just a Number
15586 Posts |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-22 : 13:53:12
|
Brett - are you just kidding?Create Trigger for Update --> no need to check inserted and deleted.And your update statement is updating every row in the table without any relation to inserted/delted No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-22 : 14:05:31
|
quote: Originally posted by BigDelboy Hi allI new here and sorry if I am posting into the wrong section..I wish to create a trigger on a table which can identify when a row is updated and then update a date field within that row displaying the date it was last updated.I would be happy if someone could point out somewhere in you vast knowledge base here where I can get my answer..Kind regards,D.
We need the table structure to give a solution. No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
BigDelboy
Starting Member
3 Posts |
Posted - 2010-11-23 : 04:26:52
|
Hi Guys.. thanks for the prompt attention..In my table I have a GUID field and have to update a DateTime field when the record gets updateded externally. example a record is added to the table at some point..GUID - Address - Postcode - CreateDate - UpdatedDate1234ETC - 12 Dane Rd - SW30 8ET - 01/01/2010ETC - NULLOnce the record is updated, I want the trigger to update the UpdatedDate field..GUID - Address - Postcode - CreateDate - UpdatedDate1234ETC - 13 Dane Rd - SW30 8ET - 01/01/2010ETC - 23/11/2010ETCI hope this gives you the info you require.. Kind regards and many thanks,Derek. |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-23 : 04:35:23
|
create trigger tr_tbl on tbl for updateasupdate tblset UpdatedDate = getdate()from tbl tjoin inserted ion t.guid = i.guidgo==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
BigDelboy
Starting Member
3 Posts |
Posted - 2010-11-23 : 05:45:34
|
Perfect Nigel.. Many thanks for your reply today, it helped and solved my problem.. I can go on and cause more problems with this knowledge now :)Kind regardsDerek. |
|
|
s3245863
Starting Member
2 Posts |
Posted - 2010-12-13 : 09:39:58
|
Hi! I'm pretty new to Trigger and I have a problem hereI want to update a table and use a trigger to check a condition for the unique constraint. For example, the username. Here is my code but it seems not to work correctly:USE [TMSDB]GO/****** Object: Trigger [dbo].[TR_UPDATE_USER] Script Date: 12/13/2010 20:56:04 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER TRIGGER [dbo].[TR_UPDATE_USER] ON [dbo].[User]INSTEAD OF UPDATEASBEGINSET NOCOUNT ON-- Check for duplicate User. If there is no duplicate, do an update.IF (NOT EXISTS (SELECT U.Userid FROM [User] U, inserted I WHERE U.Username = I.Username)) UPDATE [User] SET [Username] = I.Username, [Password] = I.Password, [Firstname] = I.Firstname, [Middlename] = I.Middlename, [Lastname] = I.Lastname, [Email] = I.Email, [ManagerID] = I.ManagerID FROM [User] U, inserted I WHERE U.Userid = I.Userid ENDCan anyone tell me what wrong with this ?Tuan |
|
|
X002548
Not Just a Number
15586 Posts |
|
s3245863
Starting Member
2 Posts |
Posted - 2010-12-13 : 20:33:00
|
Thanks X002548!It works now but I still don't get it. I know that The 'inserted' is the table that we want it to be the result but what about the 'deleted'. What does it do to my problem. I should learn more about this, perhaps.Once again, thanks to X002548!Tuan |
|
|
|