Solved

SQL trigger after update of a table

Posted on 2014-03-24
9
420 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
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.

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

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 article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

770 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