Solved

Conditional Trigger in SQL Server 2008

Posted on 2011-03-11
3
804 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
[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
3 Comments
 
LVL 40

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 40

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

632 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