简体   繁体   English

System.Data.SqlClient.SqlException:字符串或二进制数据将被截断

[英]System.Data.SqlClient.SqlException: String or binary data would be truncated

I am creating a web app in which I am executing a select command on my stored procedure, but I want to insert the same fetched data into another table. 我正在创建一个Web应用程序,在其中对存储过程执行select命令,但是我想将相同的提取数据插入到另一个表中。

So I tried to do something like the following 所以我尝试做类似下面的事情

CREATE PROCEDURE profinalinstexpensesonid  
    (@from varchar(5000),                
     @to varchar(5000),                
     @trainer varchar(5000),  
     @sonvinid varchar(5000)              
    )            
AS                
BEGIN    
    INSERT INTO invoice(sonvinid, tid, date, brandname, zone, location, area, venuename, venue, instructore, amount)  
        SELECT
            instructoreexpense.sonvinid,  
            sonvininsert.trainer,               
            CONVERT(VARCHAR, sonvininsert.date, 105) AS date,
            sonvininsert.brandname,                
            SUBSTRING(sonvininsert.zone, 1, 1) AS zone,                
            sonvininsert.location,                
            sonvininsert.area,                
            companysonvinunitvenue.venuename,              
            sonvininsert.venue,                
            sonvininsert.instructore,                            
            instructoreexpense.amount  
        FROM
            instructoreexpense                 
        LEFT OUTER JOIN
            sonvininsert ON sonvininsert.sonvinid = instructoreexpense.sonvinid 
                         AND sonvininsert.status = '0'                  
        LEFT OUTER JOIN 
            finalinstructoreexpense ON finalinstructoreexpense.sonvinid = instructoreexpense.sonvinid                  
        LEFT OUTER JOIN
            companysonvinunitvenue ON companysonvinunitvenue.id =  sonvininsert.comsonvinid                           
        WHERE
            sonvininsert.date BETWEEN CONVERT(DATETIME, @from, 105)
                                  AND CONVERT(DATETIME, @to, 105) 
            AND sonvininsert.trainer = (SELECT empname 
                                        FROM trainerdetails 
                                        WHERE trid = @trainer)  
            AND instructoreexpense.sonvinid NOT IN (SELECT CAST(Item AS INTEGER)  
                                                    FROM SplitString(@sonvinid, ','))
        ORDER BY 
            instructoreexpense.sonvinid  
END   

and when I execute the stored procedure like 当我执行存储过程时

exec profinalinstexpensesonid '01-01-2013','01-01-2017','andrews'

I am getting the following error 我收到以下错误

Msg 8152, Level 16, State 13, Procedure profinalinstexpensesonid, Line 10 Msg 8152,第16级,状态13,第10行,程序profinalinstexpensesonid
String or binary data would be truncated. 字符串或二进制数据将被截断。

On my line 10 I have the following code 在我的第10行中,我有以下代码

insert into invoice(sonvinid, tid, date, brandname, zone, location, area, venuename, venue, instructore, amount)

I don't know what is wrong here? 我不知道这是怎么回事?

The error message states the size of a column in invoice table is less compared to the size of the data being inserted into it. 该错误消息指出, invoice表中的列的大小小于要插入其中的数据的大小。

For example if column brandname has data type varchar(50) and you are trying to insert more than 50 characters then it will cause error. 例如,如果列brandname数据类型为varchar(50)而您尝试插入的字符超过50 ,则将导致错误。

To resolve this compare the size of columns in invoice with the size of the columns being inserted. 要解决此问题,请将invoice中的列大小与要插入的列大小进行比较。

You need to check column size of invoice table as well as columns in select list from which you are populating data. 您需要检查发票表的列大小以及要从中填充数据的选择列表中的列。

Let's say you are inserting column "B" having data type as varchar(70) from table2 in column "A" having data type varchar(50) in table1; 假设您要从表2中将数据类型为varchar(70)列“ B”插入表1中的数据类型为varchar(50)列“ A”中; this won't work as you are trying to insert 70 characters in 50 varchar sized column. 当您尝试在50个varchar大小的列中插入70个字符时,此操作将无效。

Check source & destination column data type & it's length; 检查源列和目标列的数据类型及其长度; and change it and try again. 并更改它,然后重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM