Я пытаюсь написать sp для вставки данных в мою таблицу.В моем sp у меня есть поле tracingno, что большая часть моего реализованного кода связана с этим.но я сталкиваюсь с этой ошибкой:
Преобразование не удалось при преобразовании значения varchar 'SRFCHE-600-R-1000000' в тип данных int.тип tracingno varchar, но я не могу понять, почему я столкнулся с этой ошибкой.
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[spInsertRecieptTracing_tblRecieptTracing]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[spInsertRecieptTracing_tblRecieptTracing]
GO
create procedure spInsertRecieptTracing_tblRecieptTracing (
@RecieptTracingID UniqueIdentifier,
@RecieptItemRef UniqueIdentifier,
@TracingNo varchar(4000),
@ExecuteID UniqueIdentifier,
@Quantity Decimal(25,5),
@ExpireDateStr varchar(4000),
@ExpireDate datetime,
@ManufacturingDateStr varchar(4000),
@LotNo varchar(4000),
@IsBarCode Bit,
@ManufacturingDate DateTime,
@PartOptionName varchar(1000) = null,
@partcode varchar (100)
)
with Encryption
as
declare @_tracingno varchar (1000)
if exists (
select *
from ims.tblsettings where IsPartInTracingno = 1
)
begin
if exists (
select top 1 TracingNo
from ims.tblRecieptTracing
where TracingNo like '%-%')
begin
set @_tracingno = (
select max( right(TracingNo, charindex('-', reverse(TracingNo) + '-') - 1))
from ims.tblRecieptTracing
)
end
else
begin
set @_tracingno = 1000000
end
end
else
begin
set @_tracingno=''
end
if(@IsBarCode is null)
set @IsBarCode =0;
if(@IsBarCode =0)
begin
SET @TracingNo = NULL;
end
IF(EXISTS(
SELECT *
FROM IMS.tblRecieptTracing
WHERE RecieptItemRef = @RecieptItemRef
) and @IsBarCode = 0)
update IMS.tblRecieptTracing
set ExecuteID=@ExecuteID,
Quantity = @Quantity,
ExpireDateStr = @ExpireDateStr,
ExpireDate = @ExpireDate,
PartOptionName = @PartOptionName
Where RecieptItemRef = @RecieptItemRef
ELSE
BEGIN
if(@TracingNo is null)
begin
if exists(select * from ims.tblsettings where IsPartInTracingno=1)
begin
SELECT @TracingNo = @partcode + '-' + cast(@_tracingno as varchar)
FROM IMS.tblRecieptTracing ;
IF(@TracingNo IS NULL )
begin
SET @TracingNo = 1000000;
SET @TracingNo = @TracingNo +1 ;
end
SET @TracingNo=cast (@TracingNo as varchar)
end
else
--select
SELECT @TracingNo = cast (max( right(TracingNo, charindex('-', reverse(TracingNo) + '-') - 1))as varchar)
from ims.tblRecieptTracing;
IF(@TracingNo IS NULL )
SET @TracingNo = 1000000;
SET @TracingNo = @TracingNo + 1 ;
end
insert into IMS.tblRecieptTracing (
RecieptTracingID ,
RecieptItemRef ,
TracingNo ,
ExecuteID ,
Quantity ,
ExpireDateStr ,
ExpireDate,
ManufacturingDateStr,
LotNo,
ManufacturingDate,
PartOptionName)
values(
@RecieptTracingID,
@RecieptItemRef,
@TracingNo ,
@ExecuteID ,
@Quantity ,
@ExpireDateStr ,
@ExpireDate,
@ManufacturingDateStr,
@LotNo,
@ManufacturingDate,
@PartOptionName);
END
Я думаю, что основная часть здесь:
SELECT @TracingNo=cast (max( right(TracingNo, charindex('-', reverse(TracingNo) + '-') - 1))as varchar) from ims.tblRecieptTracing;