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

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

我正在创建一个 Web 应用程序,我在其中对我的存储过程执行 select 命令,但我想将相同的获取数据插入另一个 table。

所以我试着做了类似下面的事情

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   

当我执行存储过程时

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

我收到以下错误

Msg 8152, Level 16, State 13, Procedure profinalinstexpensesonid, Line 10
String or binary data would be truncated.

在第 10 行我有以下代码

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

我不知道这里有什么问题?

错误消息指出 invoice table 中列的大小小于插入其中的数据的大小。

例如,如果列 brandname 的数据类型为 varchar(50),而您尝试插入超过 50 个字符,则会导致错误。

要解决此问题,请将 invoice 中列的大小与要插入的列的大小进行比较。

您需要检查发票 table 的列大小以及要从中填充数据的 select 列表中的列。

假设您要从 table2 中插入数据类型为 varchar(70) 的列 "B",列 "A" 中的数据类型为 varchar(50),在 table1;这将不起作用,因为您正试图在 50 varchar 大小的列中插入 70 个字符。

检查源列和目标列的数据类型及其长度;并更改它并重试。