Solved

SQL trigger after update of a table

Posted on 2014-03-24
9
418 Views
Last Modified: 2014-03-31
Hi experts,

I am trying to write a SQL tigger however not having done one before all the examples I have found are a bit daunting and I don't quite fully understand them.

The scenario is that a table called workorders.workorders has a column that is updated called status.

What I am looking to dos when this status changes to complete or closed that the field closed date gets the system damage.

I would be very grateful if someone could give me an example and break it down for me please.

Thanks

Simon
0
Comment
Question by:SimonPrice33
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 3

Expert Comment

by:englanddg
Comment Utility
What do you mean by the term "system damage"?  Can you elaborate?
0
 

Author Comment

by:SimonPrice33
Comment Utility
Sorry, that was auto correct on the iPad.

System date it was meant to say
0
 
LVL 3

Expert Comment

by:englanddg
Comment Utility
Well, a few ways you could do it.

1)  You could have a SQL Agent job that updates the table.  Query would be something like this.

update <tablename> set statusdate = date() where statusdate is NULL and status = 'Closed'

Open in new window


2)  You could do it with an update through the UI itself...
  update <tablename> set statusdate = date() where ticketid = <%=ticketid%>

Open in new window


Or something like that.

3)  You want to use a trigger.  Which, is probably the best way.  So, here ya go.  I'm not going to steal this tutorial, cause it's perfect.  Gives you what you want, I think.

http://www.sqlteam.com/article/an-introduction-to-triggers-part-i

Let me know if you have any questions.
0
 
LVL 19

Accepted Solution

by:
Bhavesh Shah earned 500 total points
Comment Utility
hi,

you wanted to update date only when status update, if so check out the following code.

CREATE TRIGGER Trigegr1 ON Table1 FOR INSERT,UPDATE
AS
SET NOCOUNT ON
	IF(UPDATE(Status))
	BEGIN		
		
		UPDATE Table1
		SET UpdatedOn = getdate()
		WHERE STATUS = 'Closed'
	
	END

Open in new window

0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:SimonPrice33
Comment Utility
I am having trouble getting the link on my laptop, but will read through it on the iPad and try it shortly
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
CREATE TRIGGER workorders__TRG_UPDATE
ON dbo.workorders
AFTER UPDATE
AS
SET NOCOUNT ON
IF UPDATE(status)
BEGIN
    UPDATE wo
    SET [closed date] = GETDATE()
    FROM inserted i
    INNER JOIN deleted d ON
        d.key_col = i.key_col
    INNER JOIN workorders wo ON
        wo.key_col = i.key_col
    WHERE
        i.status IN ('closed', 'complete') AND
        ISNULL(o.status, '.') <> i.status
END --IF
0
 

Author Closing Comment

by:SimonPrice33
Comment Utility
perfect thank you
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
You do realize that trigger/UPDATE will always update every row, right?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Quite.  It sort of defeats the point of a TRIGGER.  I suspect they will notice something is amiss when they start seeing all the same dates for all the rows, regardless of when or even whether they were updated or not!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Select2 jquery help 9 41
Word Template Mail merge with vb.net 4 38
GRANT, REVOKE, DENY 4 15
Retention Policy for Backups 1 12
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now