Solved

SQL trigger after update of a table

Posted on 2014-03-24
9
419 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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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:ScottPletcher
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:ScottPletcher
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

In this article I will describe the Detach & Attach 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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Viewers will learn how the fundamental information of how to create a table.

911 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

20 Experts available now in Live!

Get 1:1 Help Now