SQL >> Cannot sort a row of size 8096[...]

  • Hi,

    I have a strange error in one of the query that I am doing. And, I am un-sure on how to resolve it. Here is the query :

    SELECT o.name, c.number, c.colid, c.text

      FROM

       syscomments c

       INNER JOIN sysobjects o ON c.id = o.id

      WHERE

       (o.xtype = 'P') AND (

        (c.text LIKE '%a%')

        OR

        (o.name like '%a%')

      &nbsp

      ORDER BY

       o.name,

       c.number,

       c.colid

     
    Error Message :
    Server: Msg 1540, Level 16, State1, Line 3
    Cannot sort a row of size 8096, which is greater than the allowable maximum of 8094.
     
    As far as i know, name seen to be of the type sysname, number is of type int and colid is also of type int. Those three togheter, if they are put together, can't go over the 8094 limit. I know that the biggest row is the text, length of 4k, yet, does SQL server take that row in consideration when he sort? Even though it is not specify inside the ORDER BY clause?
     
    I might be missing something somewhere or maybe not. Tell me if you need more detail to help me solve or avoid this kind of error.
     
    Thanks
  • (From what I know...)

    Yes it includes the text (varchar) column.  (even though you think it's 4k because of nvarchar it may be 2 bytes per position).

    So, in order to do the order by it needs to move the data to a temp table and that's where it wants to choke.  It may happen to work if you have an index that supports the order and filter (order by & where clauses).

    The other way I've hacked around this is to convert the text column to varchar (from nvarchar) so that max length of all columns is under 8k, or if that fails, make assumptions on the text field that none is bigger than like 3k, and convert that one on select.

    (like select a, b, convert(varchar(4000), t) from...)

  • name is sysname(128) = 128

    number and colid are smallint = 4

    Text is nvarchar(4000) = 8000

    So you can definitly bust the 8094 limit there. But as John suggested, you can convert the text to varchar (unless you use chinese text in there) and it'll go down 4000 characters.

  • Done, and it worked.

    Thanks to both of you guys.

  • John,

    Thanks for your solutions. I tried changed all my nvarchar to varch and the problem went away. Thanks a bunch.

     

    Dom

  • I stand corrected.  Sysname is Nvarchar(128)... Just in case anyone reads this later on.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply