Solved

Conditional Trigger in SQL Server 2008

Posted on 2011-03-11
3
753 Views
Last Modified: 2012-05-11
I have a simple trigger that updates employee mail stops when they are relocated in our real estate system. I have two conditions:

1. if the employee becomes a remote worker, their mail stop is updated to the mail stop of the building they vacated. This field is held in the TIAAB0.AWS_MSTOP field.

2. If they move from one seat to another seat within the company portfolio, then their mail stop is updated to the mail stop associated with their new seat location. This value is held in the TIAAA0.MAILSTOP field.

Both conditions update the same field for the employee mail stop value which is held in the TIAAN0.MSTOP field. The trigger resides on the TIAAMH table which records the occurrence of the employee moving.

I am getting the following error when I run this trigger:
Msg 4104, Level 16, State 1, Procedure MAILSTOP_UPDATE, Line 14
The multi-part identifier "INSERTED.TORMID" could not be bound.
Msg 4104, Level 16, State 1, Procedure MAILSTOP_UPDATE, Line 21
The multi-part identifier "INSERTED.TORMID" could not be bound.

 I have tried changing the conditional value to I.TORMID and TIAAMH.TORMID but i still get the same error. Does anyone see what my issue might be?


CREATE TRIGGER MAILSTOP_UPDATE
   ON  TIAAMH
   AFTER INSERT
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

IF INSERTED.TORMID LIKE 'ZAWS%'
BEGIN
UPDATE TIAAN0
SET MSTOP = TIAAB0.AWS_MSTOP
FROM TIAAB0 INNER JOIN INSERTED I ON TIAAB0.BLDGCODE = SUBSTRING(I.TORMID,1,4)
END

IF INSERTED.TORMID NOT LIKE 'ZAWS%'
BEGIN
UPDATE TIAAN0
SET MSTOP = TIAAA0.MAILSTOP
FROM TIAAA0 INNER JOIN INSERTED I ON TIAAA0.BLDGCODE+TIAAA0.RMID = I.TORMID
END
END
GO
0
Comment
Question by:Littleghostface
  • 2
3 Comments
 
LVL 39

Expert Comment

by:lcohan
Comment Utility
Updated below and you should consider what hapens if it is possible to BULK INSERT in the table because then you will have to extend the trigger code with a loop for each inserted row.

CREATE TRIGGER MAILSTOP_UPDATE
   ON  TIAAMH
   AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

      IF EXISTS (SELECT * FROM INSERTED WHERE INSERTED.TORMID LIKE 'ZAWS%')
      BEGIN
            UPDATE TIAAN0
            SET MSTOP = TIAAB0.AWS_MSTOP
            FROM TIAAB0 INNER JOIN INSERTED I ON TIAAB0.BLDGCODE = SUBSTRING(I.TORMID,1,4)
      END

      IF NOT EXISTS (SELECT * FROM INSERTED WHERE INSERTED.TORMID LIKE 'ZAWS%')
      BEGIN
            UPDATE TIAAN0
            SET MSTOP = TIAAA0.MAILSTOP
            FROM TIAAA0 INNER JOIN INSERTED I ON TIAAA0.BLDGCODE+TIAAA0.RMID = I.TORMID
      END
END
GO
0
 

Author Comment

by:Littleghostface
Comment Utility
Good point on the loop. We will be batch updating the TIAAMH table becuase we post employee relocations in batches. How would I extend the trigger to add code for a loop for each inserted row.
0
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
Comment Utility

CREATE TRIGGER MAILSTOP_UPDATE
   ON  TIAAMH
   AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

--check if single insert do the update
IF (SELECT COUNT(*) FROM INSERTED) = 1
BEGIN -- check batch
      IF EXISTS (SELECT * FROM INSERTED WHERE INSERTED.TORMID LIKE 'ZAWS%')
      BEGIN
            UPDATE TIAAN0
            SET MSTOP = TIAAB0.AWS_MSTOP
            FROM TIAAB0 INNER JOIN INSERTED I ON TIAAB0.BLDGCODE = SUBSTRING(I.TORMID,1,4)
      END

      IF NOT EXISTS (SELECT * FROM INSERTED WHERE INSERTED.TORMID LIKE 'ZAWS%')
      BEGIN
            UPDATE TIAAN0
            SET MSTOP = TIAAA0.MAILSTOP
            FROM TIAAA0 INNER JOIN INSERTED I ON TIAAA0.BLDGCODE+TIAAA0.RMID = I.TORMID
      END
END -- check batch

--check batch here and update one at a time
IF (SELECT COUNT(*) FROM INSERTED) > 1
BEGIN -- loop here

      --put your loop code here and process one by one from INSERTED by ID either
      --in a CURSOR or WHILE/LOOP !! caution this may slow down your inserts so.....
      --you could just save all inserted ID's in a table, let the insert go in then process them all
      --later in a SQL job for instance...

END --loop for all records
     

END
GO
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sql Query Lookup based on range 2 28
Stored procedure with a parameter 6 19
How to calculate iops? 12 26
SQL Maintenance Plan 3 13
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

18 Experts available now in Live!

Get 1:1 Help Now