Solved

How to make a trigger conditional

Posted on 2001-08-13
2
304 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
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 100 total points
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now