简体   繁体   中英

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.

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
String or binary data would be truncated.

On my line 10 I have the following code

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.

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.

To resolve this compare the size of columns in invoice with the size of the columns being inserted.

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; this won't work as you are trying to insert 70 characters in 50 varchar sized column.

Check source & destination column data type & it's length; and change it and try again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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