sp_executesql with sql > 4000

  • I have a stored procedure where a SQL string is built depending on a bunch of input parameters. When the SQL is built, it is executed with sp_executesql. However, now I have a situation that the SQL becomes very large. I made the variable @SqlString varchar(8000) instead of nvarchar(4000), but it still doesn't work. I'm suspecting that sp_executesql can't handle a string larger than 4000. Can anyone confirm this and help me with a solution for this problem?

    Regards,

    Nils

  • You can use EXEC instead of sp_executesql in such a scenario:

    declare @col1 varchar(8000), @col2 varchar(8000), @col3 varchar(8000)

    set @col1 = 'select * from '

    set @col2 = 'sysobjects where '

    set @col3 = 'xtype = ''u'''

    exec (@col1 + @col2 + @col3)

Viewing 2 posts - 1 through 2 (of 2 total)

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