Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to make a trigger conditional

Posted on 2001-08-13
2
Medium Priority
?
354 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.

609 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