Link to home
Start Free TrialLog in
Avatar of ahmzb1990
ahmzb1990

asked on

SQL Trigger - Invalid Column Name

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???
Avatar of appari
appari
Flag of India image

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.
Avatar of ahmzb1990
ahmzb1990

ASKER

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??
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Worked like a charm