[英]Getting Truncation error in SQL SERVER while executing Stored procedure
I am getting the below error while executing a stored procedure 执行存储过程时出现以下错误
USE [Smart2uat]
GO
/****** Object: StoredProcedure [dbo].[SPJOB_ALLLOC] Script Date: 2/11/2016 12:37:40 PM ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[SPJOB_ALLLOC] AS
declare @cardno as varchar(8)
declare @iodate as datetime
declare @iotime as varchar(8)
declare @holdername as varchar(100)
declare @IO_MSKID as varchar(7)
declare @IO_LOCATION_CODE as varchar(3)
declare @IO_COMPANY_CODE as varchar(3)
declare @IO_ACTIVITY_CODE as varchar(6)
declare @IO_FIRST_NAME as varchar(20)
declare @IO_THIRD_NAME as varchar(20)
declare @IO_EMPLOYEE_CODE as varchar(3)
declare @rows as integer
declare curatt1 cursor for
select iodate,cardno,iotime,holdername from iodatatmp where isnull(cardno,'')<>'' and isnull(cardno,'') not like 'XXXX%' order by iotime
open curatt1
fetch next from curatt1 into @iodate,@cardno,@iotime,@holdername
WHILE @@FETCH_STATUS = 0
BEGIN
if not exists(select CardNo from iodata where iodate= @iodate and cardno= @cardno)
begin
select @IO_MSKID = ''
select @IO_MSKID=MSKID,@IO_LOCATION_CODE=LOCATION_CODE,@IO_COMPANY_CODE=COMPANY_CODE,@IO_ACTIVITY_CODE=ACTIVITY_CODE,@IO_FIRST_NAME=FIRST_NAME,@IO_THIRD_NAME=THIRD_NAME,@IO_EMPLOYEE_CODE=EMPLOYEE_CODE from Employee_Mast where card_number = @cardno
IF @IO_MSKID <> ''
insert into iodata(cardno,iodate,iotime,holdername,IO_MSKID,IO_LOCATION_CODE,IO_COMPANY_CODE,IO_ACTIVITY_CODE,IO_FIRST_NAME,IO_THIRD_NAME,IO_EMPLOYEE_CODE)
values(@cardno,@iodate,@iotime,@holdername,@IO_MSKID,@IO_LOCATION_CODE,@IO_COMPANY_CODE,@IO_ACTIVITY_CODE,@IO_FIRST_NAME,@IO_THIRD_NAME,@IO_EMPLOYEE_CODE)
end
fetch next from curatt1 into @iodate,@cardno,@iotime,@holdername
END
close curatt1
deallocate curatt1
delete from iodata where cardno like 'XXXX%'
error is 错误是
Msg 8152, Level 16, State 14, Procedure SPJOB_ALLLOC, Line 31
String or binary data would be truncated.
The statement has been terminated.
(0 row(s) affected)
Can you please help me..? 你能帮我么..? I am attaching the table design of
iodatatmp
on which the cursor is fetching and the destination table - Iodata
我附加了
iodatatmp
在其上获取游标的iodatatmp
的表设计和目标表Iodata
I have checked the length of the variable...and seems everything fine....Kindly help me..Thanks in advance 我检查了变量的长度...似乎一切都很好....请帮助我..谢谢
The error you are getting is caused by the fact that the size of some of the varchar type columns in IODataTmp
are larger than the equivalent column in IOData
. 您得到的错误是由于
IODataTmp
中某些varchar类型的列的IODataTmp
大于IODataTmp
中的等效列的IOData
。
For instance IODataTmp.Holdername
is declared as varchar(32)
and yet IOData.Holdername
is declared as varchar(20)
and so if you have a value in IODataTmp.Holdername
that is longer than 20 characters it will fail when it tries to insert into IOData.Holdername
. 例如,
IODataTmp.Holdername
声明为varchar(32)
,而IOData.Holdername
声明为varchar(20)
,因此,如果IODataTmp.Holdername
中的值大于20个字符,则当它尝试插入时将失败。 IOData.Holdername
。 Another issue you have is that variables you have declared in the stored procedure to hold the values are a different size again, @holdername
is declared as varchar(100)
. 您遇到的另一个问题是,您在存储过程中声明的用于保存值的变量再次具有不同的大小,
@holdername
被声明为varchar(100)
。
The solution is to ensure that all the column and variable sizes for a piece of data are the same. 解决方案是确保一条数据的所有列和变量大小都相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.