执行存储过程时在SQL SERVER中出现截断错误

时间:2016-02-11 13:12:57

标签: sql-server stored-procedures

执行存储过程时出现以下错误

 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%'

错误是

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)
你能帮帮我吗?我附加了光标所在的iodatatmp的表设计和目标表 - Iodata

Iodata Design   IOdataTmp design

我检查了变量的长度......似乎一切都很好....请帮助我......提前谢谢

1 个答案:

答案 0 :(得分:2)

您获得的错误是由于IODataTmp中某些varchar类型列的大小大于IOData中的等效列的大小。

例如,IODataTmp.Holdername被声明为varchar(32),而IOData.Holdername被声明为varchar(20),因此如果IODataTmp.Holdername中的值超过IOData.Holdername它尝试插入@holdername时将失败的20个字符。您遇到的另一个问题是,您在存储过程中声明的用于保存值的变量的大小不同,varchar(100)被声明为<bean id="httpComponentsMessageSender" class="org.springframework.ws.transport.http.CommonsHttpMessageSender"> <property name="credentials"> <bean class="org.apache.commons.httpclient.UsernamePasswordCredentials"> <constructor-arg value="userName" /> <constructor-arg value="*******" /> </bean> </property> </bean> <int-ws:outbound-gateway id="uniqueId" request-channel="requestServiceChannel" reply-channel="replyChannel" uri="end point url" message-sender="httpComponentsMessageSender" marshaller="ServiceMarshaller" unmarshaller="ServiceMarshaller"> </int-ws:outbound-gateway>

解决方案是确保一段数据的所有列和可变大小都相同。