Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Error : String or binary data would be truncated

My table :

log_id                 bigint
old_value                  xml
new_value                  xml
module                 varchar(50)
reference_id           bigint
[transaction]          varchar(100)
transaction_status         varchar(10)
stack_trace                ntext
modified_on                datetime
modified_by                bigint

Insert Query :

INSERT INTO [dbo].[audit_log]
           ([old_value],[new_value],[module],[reference_id],[transaction]
           ,[transaction_status],[stack_trace],[modified_on],[modified_by])
     VALUES
            ('asdf','asdf','Subscriber',4,'_transaction',
            '_transaction_status','_stack_trace',getdate(),555)

Error :

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

Why is that ???

like image 297
Anuya Avatar asked Jun 01 '12 02:06

Anuya


People also ask

How do I fix string or binary data would be truncated in SQL Server?

To fix this error, patch to SQL Server 2016 SP2, CU6 or newer (including SQL Server 2017), and then turn on trace flag 460. You can enable it at the query level or at the server level.

What does string or binary data would be truncated mean in SQL?

The "String or binary data would be truncated" error indicates that the procedure is attempting to store something in the DBServerInfo table that is larger than the column allows. The two known reasons this can occur are: SQL Server has at least one database whose name exceeds 25 characters in length.

How do you know if a string or binary data will be truncated?

Check ANSI_WARNINGS option. When querying the ss stream in azure-synapse-analytics, the fields in the stream are longer than 8000 and the query reports an error. String or binary data would be truncated while reading column of type 'VARCHAR(8000)'. Check ANSI_WARNINGS option.

Why string or binary data would be truncated The statement has been terminated?

The error occurs when the value persisted in a field is higher (in characters count) than the one the database column max value allows. For example if there is a simple text field which by default generates a 255 max char column in the database, the error will be thrown if entering in this field 256+ characters.


2 Answers

You're trying to write more data than a specific column can store. Check the sizes of the data you're trying to insert against the sizes of each of the fields.

In this case transaction_status is a varchar(10) and you're trying to store 19 characters to it.

like image 194
Tremmors Avatar answered Oct 13 '22 08:10

Tremmors


this type of error generally occurs when you have to put characters or values more than that you have specified in Database table like in this case: you specify transaction_status varchar(10) but you actually trying to store
_transaction_status which contain 19 characters. that's why you faced this type of error in this code..

like image 45
jaideep Avatar answered Oct 13 '22 07:10

jaideep