Question : Insert not working within a trigger: Error converting data type varchar to numeric

Hello,

I am having trouble inserting into a table using a trigger.  The weird thing is that It was working late last week.  The only thing I added was an index to the table on InvoiceNumber and LineItemNo_Int.

If I disable the  trigger, the data is inserted.  When I enable the trigger, I get an error:

Msg 8114, Level 16, State 5, Procedure trig_InvoiceLinesInsertCheck, Line 27
Error converting data type varchar to numeric.

Table Def:
CREATE TABLE [dbo].[InvoiceLines](
      [InvoiceLinesID] [int] IDENTITY(1,1) NOT NULL,
      [InvoiceNumber] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [CustomerNo] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [LineItemNo] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [LineItemNo_Int] [int] NULL,
      [PartNo] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [CustomerPartNo] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [Quantity] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [UnitOfMeasure] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [UnitPrice] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [Discount] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [VAT_Tax] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [PONumber] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [PODate] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [OrderNumber] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [PackingSlip] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [PlantNumber] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [DepartmentNumber] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [AccountNumber] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [CompanyWarehouse] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [SalesOrderCarrier] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [LineItemPONumber] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [LineItemReleaseNumber] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [MKPositionNumber] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [TrackingNumber] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
      [InvoiceFormat] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]


Trigger Def:

alter TRIGGER [dbo].[trig_InvoiceLinesInsertCheck] ON [dbo].[InvoiceLines]
INSTEAD OF INSERT
AS
BEGIN
      set nocount on
      DECLARE @InvoiceNumber varchar(100)
      Declare @OrderNumber varchar(100)
      Declare @PositionNumber varchar(100)
      DECLARE @InvoiceFormat varchar(100)
      set @InvoiceFormat = (Select InvoiceFormat from INSERTED)
      if @InvoiceFormat = 'New Format'
      BEGIN
            SET @InvoiceNumber = (select InvoiceNumber from INSERTED)
            set @OrderNumber = (select OrderNumber from INSERTED)
            set @PositionNumber = (select LineItemNo from INSERTED)

            IF EXISTS (select * from InvoiceLines where InvoiceNumber = @InvoiceNumber and OrderNumber = @OrderNumber and LineItemNo = @PositionNumber)
            BEGIN
                  delete from InvoiceLines where InvoiceNumber = @InvoiceNumber and OrderNumber = @OrderNumber and LineItemNo = @PositionNumber
            END
      END
            insert into InvoiceLines
                  (InvoiceNumber,CustomerNo,LineItemNo,LineItemNo_Int,PartNo,CustomerPartNo,Quantity,UnitOfMeasure,UnitPrice,Discount,VAT_Tax,PONumber,PODate,OrderNumber,PackingSlip,PlantNumber,DepartmentNumber,AccountNumber,CompanyWarehouse,SalesOrderCarrier,LineItemPONumber,LineItemReleaseNumber,MKPositionNumber,TrackingNumber,InvoiceFormat)
            select
                   InvoiceNumber,CustomerNo,LineItemNo,LineItemNo_Int,PartNo,CustomerPartNo,Quantity,UnitOfMeasure,UnitPrice,Discount,VAT_Tax,PONumber,PODate,OrderNumber,PackingSlip,PlantNumber,DepartmentNumber,AccountNumber,CompanyWarehouse,SalesOrderCarrier,LineItemPONumber,LineItemReleaseNumber,MKPositionNumber,TrackingNumber,InvoiceFormat
            from INSERTED
END


Insert Query:

insert into InvoiceLines
      (
            InvoiceNumber,
            CustomerNo,
            LineItemNo,
            LineItemNo_Int,
            PartNo,
            CustomerPartNo,
            Quantity,
            UnitOfMeasure,
            UnitPrice,
            Discount,
            VAT_Tax,
            PONumber,
            PODate,
            OrderNumber,
            PackingSlip,
            PlantNumber,
            DepartmentNumber,
            AccountNumber,
            CompanyWarehouse,
            SalesOrderCarrier,
            LineItemPONumber,
            LineItemReleaseNumber,
            MKPositionNumber,
            TrackingNumber,
            InvoiceFormat
      )
values
      (
            '31775ABC',
            '013608',
            '20',
            20,
            'TESTPART',
            'TESTPART2',
            '2',
            '',
            '26.70',
            '',
            '',
            '166TEST',
            '',
            '31TEST',
            '',
            '',
            '',
            '',
            '',
            'UPAC',
            '',
            '',
            '',
            'TESTTRACKINGNUM',
            'New Format'
      )

Answer : Insert not working within a trigger: Error converting data type varchar to numeric

>dgmg: The application that is calling the insert statement has controls that will only allow a single row to be inserted at a time.

Still dangerous, due to the global nature of a trigger.  You should not assume ALL updates will ALWAYS originate from that application.  Especially, when proper trigger coding techniques are cheap insurance. :>)




>Any other ideas?
Reduce the insert to the first column only.  Hopefully it works. Successively add a few columns at a time until find the offending column/data.
Random Solutions  
 
programming4us programming4us