Solved

SQL Trigger - Invalid Column Name

Posted on 2013-01-24
4
1,511 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
  • 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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Mirror and Replication 5 26
C# SQL BULK INSERT CLASS 5 36
Sql query 34 22
Determine which columns have the exact value 1 24
Hello, In my precious Article  (http://www.experts-exchange.com/Database/Reporting/A_15280-Create-Project-in-Microstrategy-Part-I.html)we saw the Configuration part for Microstrategy which included Metadata Creation and DataSource Preparation as …
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

746 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

13 Experts available now in Live!

Get 1:1 Help Now