Solved

SQL trigger after update of a table

Posted on 2014-03-24
9
421 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
ID: 39952163
What do you mean by the term "system damage"?  Can you elaborate?
0
 

Author Comment

by:SimonPrice33
ID: 39952203
Sorry, that was auto correct on the iPad.

System date it was meant to say
0
 
LVL 3

Expert Comment

by:englanddg
ID: 39952209
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 19

Accepted Solution

by:
Bhavesh Shah earned 500 total points
ID: 39952486
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
 

Author Comment

by:SimonPrice33
ID: 39953331
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:Scott Pletcher
ID: 39953777
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
ID: 39965915
perfect thank you
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39966570
You do realize that trigger/UPDATE will always update every row, right?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39968171
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

840 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