Solved

SQL Trigger - Invalid Column Name

Posted on 2013-01-24
4
1,573 Views
Last Modified: 2013-01-26
Hi

I am trying to run the trigger below but keep getting the following error:

(Msg 207, Level 16, State 1, Procedure trg_UpdateCost, Line 22
Invalid column name 'xfPositionTitle'.
Msg 207, Level 16, State 1, Procedure trg_UpdateCost, Line 23
Invalid column name 'xfAvailabilityFacilityType'.)




CREATE trigger [dbo].[trg_UpdateCost] on [dbo].[Contact]
after insert
as
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

    -- Insert statements for trigger here
   
    Declare @RECID varchar(32),
    @PositionTitle varchar(50),
    @FacilityType Varchar(25)
   
      SELECT @RECID = Inserted.RecID,
      @PositionTitle = xfPositionTitle,
      @FacilityType = xfAvailabilityFacilityType
      
      FROM INSERTED
      
      If
      xfPositionTitle = 'Physiotherapist'
      and xfAvailabilityFacilityType = 'Regis'
      
      BEGIN
            update Contact
            SET xfAvailabilityChargeOutRate = '75.00'
            from contact
            where recid = @RECID
      END


END


HELP???
0
Comment
Question by:ahmzb1990
[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
  • 2
4 Comments
 
LVL 39

Expert Comment

by:appari
ID: 38817198
try this
CREATE trigger [dbo].[trg_UpdateCost] on [dbo].[Contact]
after insert
as
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

    -- Insert statements for trigger here
    
--    Declare @RECID varchar(32),
--    @PositionTitle varchar(50),
--    @FacilityType Varchar(25)
--    
--      SELECT @RECID = Inserted.RecID,
--      @PositionTitle = xfPositionTitle,
--      @FacilityType = xfAvailabilityFacilityType
--     
--      FROM INSERTED
--      
--      If @PositionTitle = 'Physiotherapist'
--			and @FacilityType = 'Regis'
--      BEGIN
--            update Contact
--            SET xfAvailabilityChargeOutRate = '75.00'
--            from contact
--            where recid = @RECID
--      END


	update C
		SET xfAvailabilityChargeOutRate = '75.00'
    from contact C join inserted ins
         on ins.recid = C.recid
	where ins.xfPositionTitle = 'Physiotherapist'
		and ins.xfAvailabilityFacilityType = 'Regis'

END

Open in new window


modified your existing code too and commented out that part. but your code handles only single row inserts.
0
 

Author Comment

by:ahmzb1990
ID: 38817303
Thanks mate that worked perfectly, only thing is i need to use the if statement.

Reason being is there will be a few conditions i.e.

If  ins.xfPositionTitle = 'Eye Care'
and xfAvailabilityFacilityType = 'Non Regis'

then SET xfAvailabilityChargeOutRate = '90.00'

please help??
0
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 38817346
for that just copy the update statement i added and change the conditions and value.
CREATE trigger [dbo].[trg_UpdateCost] on [dbo].[Contact]
after insert
as
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

    -- Insert statements for trigger here
    
--    Declare @RECID varchar(32),
--    @PositionTitle varchar(50),
--    @FacilityType Varchar(25)
--    
--      SELECT @RECID = Inserted.RecID,
--      @PositionTitle = xfPositionTitle,
--      @FacilityType = xfAvailabilityFacilityType
--     
--      FROM INSERTED
--      
--      If @PositionTitle = 'Physiotherapist'
--			and @FacilityType = 'Regis'
--      BEGIN
--            update Contact
--            SET xfAvailabilityChargeOutRate = '75.00'
--            from contact
--            where recid = @RECID
--      END


	update C
		SET xfAvailabilityChargeOutRate = '75.00'
    from contact C join inserted ins
         on ins.recid = C.recid
	where ins.xfPositionTitle = 'Physiotherapist'
		and ins.xfAvailabilityFacilityType = 'Regis'

	update C
		SET xfAvailabilityChargeOutRate = '90.00'
    from contact C join inserted ins
         on ins.recid = C.recid
	where ins.xfPositionTitle = 'Eye Care'
		and ins.xfAvailabilityFacilityType = 'Non Regis'
END

Open in new window

0
 

Author Closing Comment

by:ahmzb1990
ID: 38823427
Worked like a charm
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

695 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