sp_executesql problem

  • sp_executesql can only process 4000 characters, but my query exceeds more than 4000 characters. I don't have any parameter and I don't have any outout.  So I tried to do the following

    DECLARE @SQL1 NVARCHAR(4000), @SQL2 NVARCHAR(4000), @ReturnCode INT

    SET @SQL1 = N'Insert into TABLEA SELECT .....'

    SET @SQL2 = N'''A''  col1, ''B'' col2 FROM ftable1 WHERE...'

    EXEC('EXEC sp_executesql N''' + @SQL1 + @SQL2 + '''')

    I supposed to get a returncode from the query.  I don't know how to do it in this way.   Second it did not work, it kept saying something wrong in @SQL1.

    I know I can use EXEC (@SQL) that will allow me to use 8000 characters but I need to find out the result of the query if it runs fine.  

    Can someone help me either using EXEC(@SQL) and get the status of the query or how to get sp_executesql to work?

    Thanks

     

     

     

     

     

     

     

     

     

  • If you are using SQL2005 use this

    declare

    @sql nvarchar(max)

    declare

    @ret int

    set

    @sql=N'Select * from sysobjects;'

    exec

    @ret=sp_executesql @sql

    select

    @ret

  • Unfortunately I am using SQL Server 2000.

  • I tried this and it worked.

    declare @sql1 nvarchar(4000),

     @sql2 nvarchar(4000)

    declare @ret int

    set @sql1=N'Select * from '

    set @sql2=N'sysobjects;'

    --exec @ret=sp_executesql @sql

    exec(@sql1 + @sql2)

    --select @ret

  • This can allow upto 16000

    declare @sql1 varchar(8000),

     @sql2 varchar(8000),

    @ret int

    set @sql1=N'Select * from '

    set @sql2=N'sysobjects;'

    exec( @sql1 + @sql2)

    I think you need to capture return code, that is not possible with this 

  • That is the reason why I need to use sp_executesql.

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

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