Solved

SQL trigger after update of a table

Posted on 2014-03-24
9
429 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

691 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