Tuesday, June 15, 2010

Executing dynamic queries with more than 4000 characters in SQL server

Executing dynamic queries with more than 4000 characters in SQL server.

DECLARE @sql_Body_1 VARCHAR(8000)

DECLARE @sql_Body_2 VARCHAR(8000)

DECLARE @sql_Body_3 VARCHAR(8000)

DECLARE @sql_Body_4 VARCHAR(8000)

DECLARE @sql_Body_5 VARCHAR(8000)

DECLARE @sql_Hold VARCHAR(8000)

SET @sql_Body_1=''

SET @sql_Body_2=''

SET @sql_Body_3=''

SET @sql_Body_4=''

SET @sql_Body_5=''

SET @sql_Hold=''

DECLARE @i INT

SELECT @i = 1

WHILE @i < 1000

BEGIN

SELECT @sql_Hold = 'insert into ...'

IF 3999 - LEN(@sql_Body_1) > LEN(@sql_Hold)

BEGIN

SELECT @sql_Body_1 = @sql_Body_1 + @sql_Hold

END

ELSE IF 3999 - LEN(@sql_Body_2) > LEN(@sql_Hold)

BEGIN

SELECT @sql_Body_2 = @sql_Body_2 + @sql_Hold

END

ELSE IF 3999 - LEN(@sql_Body_3) > LEN(@sql_Hold)

BEGIN

SELECT @sql_Body_3 = @sql_Body_3 + @sql_Hold

END

ELSE IF 3999 - LEN(@sql_Body_4) > LEN(@sql_Hold)

BEGIN

SELECT @sql_Body_4 = @sql_Body_4 + @sql_Hold

END

ELSE IF 3999 - LEN(@sql_Body_5) > LEN(@sql_Hold)

BEGIN

SELECT @sql_Body_5 = @sql_Body_5 + @sql_Hold

END

SELECT @i= @i + 1

END

EXEC (@sql_Body_1+ ' ' + @sql_Body_2+ ' ' + @sql_Body_3+ ' ' + @sql_Body_4+ ' ' + @sql_Body_5)

No comments:

Post a Comment