9

I have a SQL stored procedure that accepts a parameter of type VARCHAR(MAX). As far as I know and according to all I read about, the max size for such strings is 2GB: MSDN

For some reason when passing a string larger than 8KB I get:

String or binary data would be truncated.

Why do I get this error message, and how can I resolve it?

4
  • 1
    suggest you post the stored proc and calling code Jul 26, 2011 at 5:19
  • are you able to post the rest of the proc? It may be unrelated to that parameter.
    – SteveCav
    Jul 26, 2011 at 5:19
  • indeed the doc is not clear; it seems the limit is 8k, except if you write "max"... what I know by using oracle dbms, however, is that varchar is limited to 4k; I may be wrong, I haven't checked it carefully and I prefer clob/blob for arbitrarely large data Jul 26, 2011 at 5:20
  • Sorry for the false alarm, when started to copy the procedure I saw something weird (copying the string to a wrong param)...so, problem solved. 10x a lot
    – Lior Ohana
    Jul 26, 2011 at 5:25

4 Answers 4

8

According to BoL (the link you specified) there is a difference in interpretation. The maximum amount you can use in a query (the n part) is 8000. For storage purposes the varchar(max) can handle 2GB on disk.

It is just interpretation of datatypes for querying and storage purposes. So bottom line, you can only use 8000 chars in a query....

8

to avoid this problem, you have to cast your string first to varchar(max):

column = cast(other_column as varchar(max))

this way any string longer than max (8000 or 4000, depending on version) will be truncated to the maximum length.

1

You are passing a string bigger than the column in database, right? Try to increase the size of your column.

1

You are passing a string bigger than the column in database.

Like your database table is as follow:

 EXECUTIVE  varchar(15) 

But your insert string like (in vb.net):

cmd.Parameters.Add("@EXECUTIVE", Data.SqlDbType.VarChar, 150).Value = CmbExecutive.Text.ToString()

Not the answer you're looking for? Browse other questions tagged or ask your own question.