Microsoft
Software
Hardware
Network
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_InvoiceLinesInsertChe
ck, 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_InvoiceLinesIn
sertCheck]
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
,LineItemN
o_Int,Part
No,Custome
rPartNo,Qu
antity,Uni
tOfMeasure
,UnitPrice
,Discount,
VAT_Tax,PO
Number,POD
ate,OrderN
umber,Pack
ingSlip,Pl
antNumber,
Department
Number,Acc
ountNumber
,CompanyWa
rehouse,Sa
lesOrderCa
rrier,Line
ItemPONumb
er,LineIte
mReleaseNu
mber,MKPos
itionNumbe
r,Tracking
Number,Inv
oiceFormat
)
select
InvoiceNumber,CustomerNo,L
ineItemNo,
LineItemNo
_Int,PartN
o,Customer
PartNo,Qua
ntity,Unit
OfMeasure,
UnitPrice,
Discount,V
AT_Tax,PON
umber,PODa
te,OrderNu
mber,Packi
ngSlip,Pla
ntNumber,D
epartmentN
umber,Acco
untNumber,
CompanyWar
ehouse,Sal
esOrderCar
rier,LineI
temPONumbe
r,LineItem
ReleaseNum
ber,MKPosi
tionNumber
,TrackingN
umber,Invo
iceFormat
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
Secure Email for HIPAA compliance
windows installer problem
Need to find USB External Enclosure for 2.5" 15mm SATA drive
350ft Coax Cable installation for modem
How to connect treeview & listview or grid through Access database
xaml parse error silverlight 4 from 3
Drupal Newsletter - SimpleNews settings
2 ways in ASP.NET to open a different page?
Dual bands and my laptop
Joomla Module