Solved

How to make a trigger conditional

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

825 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