Solved

How to make a trigger conditional

Posted on 2001-08-13
2
343 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 100 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
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.

739 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