none
Executing Dynamic SQL larger than 8000 characters

    Question

  • Can anyone tell me if there is a way to get around the 8000 character limit for executing dynamic SQL statements?  I have tried everything I can think of to get around this limitation but I can not figure out a way around this. 

    Here are a few of the things that I have tried that have not worked

    • Using VARCHAR(MAX) instead on VARCHAR(8000)
    • Using NVARCHAR(MAX) instead of NVARCHAR(4000)
    • Using nTEXT (BLOBs are not support for variables)
    • Executing the statement via .NET using the SqlCommand.CommandText (it accepts a data type of String which is limited to 8000 characters)

    I can't believe this is sooo hard to figure out.  I know somebody has run into this before.  All help would be greatly appreciated.

    Wednesday, January 31, 2007 10:28 PM

Answers

  • Can you post the code. There shouldn't be a problem executing sql statement larger than 8000 via exec().

    e.g.

    declare @a varchar(8000),@b varchar(8000),@c varchar(8000)
    select @a='select top 1 name,''',@b=replicate('a',8000),@c=''' from sysobjects'
    exec(@a+@b+@c)

     

    Friday, February 02, 2007 4:59 PM
  • varchar(max) also should work just fine - could you please try something like the following?

    declare @cmd varchar(max)
    set @cmd = 'print /*' + replicate ('-', 7990);
    set @cmd = @cmd + replicate ('-', 7990) + '*/ getdate()';
    exec (@cmd)
    print datalength (@cmd)

    Feb 2 2007 2:23PM
    16000

    Friday, February 02, 2007 10:24 PM
  • you have to use the new sys.sp_sqlexec stored proc that accepts a parameter of type text. have used this on a numberof occassions with sql strings in excess of 8k limit.
    Sunday, February 04, 2007 8:52 PM

All replies

  • Create multiple 8000 char strings, break your string into 8000 char blocks and run "EXEC (@sql1+@sql2+@sql3+.......)"


    Wednesday, January 31, 2007 10:59 PM
  • Tom,

    Thanks for the help!  However, that did not work either.  My query is 8621 chars long I broke the query into two VARCHAR(8000) variables, one was 7900 and the other was 721.  Here is the error:

    The character string that starts with 'SELECT ...' is too long. Maximum length is 8000.

    Not sure why it is not working for me if it works for you... what is the data type fo the variables that you are using?

    Friday, February 02, 2007 2:33 AM
  • I haven't seen that error before.  However, I am usually executing multiple "commands", not 1 single command greater than 8000 chars.  That might be a limitation of SQL, the command buffer might only be 8000 chars.

    Maybe someone from MS can answer if that is a "command buffer limit"??


    You might have to break it further into multiple select statements.

    Friday, February 02, 2007 1:59 PM
  • Can you post the code. There shouldn't be a problem executing sql statement larger than 8000 via exec().

    e.g.

    declare @a varchar(8000),@b varchar(8000),@c varchar(8000)
    select @a='select top 1 name,''',@b=replicate('a',8000),@c=''' from sysobjects'
    exec(@a+@b+@c)

     

    Friday, February 02, 2007 4:59 PM
  • varchar(max) also should work just fine - could you please try something like the following?

    declare @cmd varchar(max)
    set @cmd = 'print /*' + replicate ('-', 7990);
    set @cmd = @cmd + replicate ('-', 7990) + '*/ getdate()';
    exec (@cmd)
    print datalength (@cmd)

    Feb 2 2007 2:23PM
    16000

    Friday, February 02, 2007 10:24 PM
  • you have to use the new sys.sp_sqlexec stored proc that accepts a parameter of type text. have used this on a numberof occassions with sql strings in excess of 8k limit.
    Sunday, February 04, 2007 8:52 PM
  • Thanks for all the help.  Looks like I have several options here.
    Sunday, February 04, 2007 10:15 PM