Solved

Conditional Trigger in SQL Server 2008

Posted on 2011-03-11
3
762 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
ID: 35112174
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
ID: 35112422
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
ID: 35112521

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
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 …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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