Tuesday 12 June 2012

String or binary data would be truncated. The statement has been terminated.


   Msg 8152, Level 16, State 14, Line 1. String or binary data would be truncated. The statement has been terminated. Here "String or binary data would be truncated" indicates that you have defined a table or any other data capturing object in such a way that its defined field is of smaller length and you are inserting data of larger length into that field.

To get a clear idea, see the below example :


CREATE TABLE MstEmployee
(
 EmpId      BIGINT IDENTITY
,EmpName    VARCHAR(20)
,EmpAddress VARCHAR(200)
)


--------  Successful Insert ----------

INSERT INTO MstEmployee(EmpName,EmpAddress)
VALUES ('Shubha','J.B. Nagar, Andheri East, Mumbai')

(1 row(s) affected)


-------- Insert Query throwing Exception ----------

INSERT INTO MstEmployee(EmpName,EmpAddress)
VALUES ('Venkteshan Raghwan Reddy','RCF colony,Chickmagalur')

Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated.
The statement has been terminated.



   In the above example, when inserting field values less than the declared length then its inserting without any issue. But when we are trying to insert the second row, in which EmpName 'Venkteshan Raghwan Reddy' has a length greater than 20 characters as defined, it throws the exception.

This type of issues also come when we are using some other datatypes as well.

 So while creating the data objects, declare an appropriate length..



Reference: Govind Badkur(http://govindbadkur.blogspot.com)

No comments:

Post a Comment