?
Solved

How to make a trigger conditional

Posted on 2001-08-13
2
Medium Priority
?
349 Views
Last Modified: 2012-05-04
Hello SQL experts. I will be grateful for any help on this one.

I am trying to customise a package (Maximo) which uses MSSQL Server 7.

I haven't used SQL before, and I've had no training, so I'm struggling a bit.

I discovered "triggers" and have had some success using them.

The following code works OK to populate the field "author" when my user is inserting a new workorder record:

- - - - -
CREATE TRIGGER [labor_info_ins] ON [workorder]
FOR INSERT
AS

UPDATE workorder
    SET author =
    (SELECT name FROM labor
    WHERE laborcode = changeby)
- - - - - - -

My question is:

How do I make it populate "author" only if "author" is null. In other words, if the user actually enters an author name I want to use that, but if they don't I want to populate it by accessing the labor table.

I hope it is OK that I have categorised this question as easy. I imagine it is probably simple for someone who knows what they are doing (unlike me). If you think I am being too miserly please say so and I will upgrade the points.


Thanks

Iain Macrae
0
Comment
Question by:MacRae
[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
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 total points
ID: 6382942
First a small comment about your actual code

CREATE TRIGGER [labor_info_ins]
ON [workorder]
FOR INSERT
AS
UPDATE workorder
   SET author =
   (SELECT name FROM labor
   WHERE laborcode = changeby)
--> THIS will update the entire workorder table on every insert!!!

To solve this, you need to use the <inserted> table, i guess you have some primary key on your workorder table.

CREATE TRIGGER [labor_info_ins]
ON [workorder]
FOR INSERT
AS
UPDATE workorder
   SET author =
   (SELECT name FROM labor
   WHERE laborcode = changeby)
FROM workorder w
JOIN inserted i
ON w.primarykey = i.primarykey


Now let's tackle the problem about the autor field:
This is very easy now, because the inserted table contains the values that are actually inserted. If we use an additional where clause, we can eliminate those cases where the value for that field is given as null:

CREATE TRIGGER [labor_info_ins]
ON [workorder]
FOR INSERT
AS
UPDATE workorder
   SET author =
   (SELECT name FROM labor
   WHERE laborcode = changeby)
FROM workorder w
JOIN inserted i
ON w.primarykey = i.primarykey
WHERE i.author IS NOT NULL


Cheers
0
 

Author Comment

by:MacRae
ID: 6383224
Thanks very much angellll

It worked like a charm.

And thanks for pointing out the deficiency in my code - you have no doubt saved me some embarrassment. I kind of assumed that code in the trigger would only apply to the record being inserted, but I see now that this is not so.

As a sign of gratitude I have increased the points to 100.

Best Regards

Iain MacRae

0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

762 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