Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Conditional Trigger in SQL Server 2008

Posted on 2011-03-11
3
Medium Priority
?
820 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

705 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