max length varchar

Hi, I have a stored procedure that creates and executes SQL insert statements using dynamic SQL.  This procedure worked fine until today.  The length of the insert statement exceeded 8000 characters and it seems like the varchar datatype cannot hold more than 8000 characters.

Anyone knows an easy workaround?
Can I use another datatype, that allows more than 8000 characters?

I hope I don't have to redesign my programs, just because sometimes the query gets too long.

My database is SQL Server 7.
LVL 6
hans_vdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

adatheladCommented:
VARCHAR is limited to 8000. Only TEXT can store more, but you cannot use this datatype for your purpose.
The EXECUTE statement does support strings longer than 8000 so this example is valid:

DECLARE @pstrSQLPart1 VARCHAR(8000)
DECLARE @pstrSQLPart2 VARCHAR(8000)
EXECUTE(@pstrSQLPart1 + @pstrSQLPart2)

So, you can get round it by using multiple VARCHAR(8000) variables.

Alternatively, see if you can do the query in smaller stages (e.g. using temporary tables). Also, look into using user defined functions and views to makes things concise.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adwisemanCommented:
You can use text, give me a sec to dig up some old script.  I've done the same thing your talking about.

It's not atractive though.

ADW
0
TyipCommented:
If you cannot add another parameter, you can do this:

create procedure proc ( @sqltext text )
as
declare @sqltext1  varchar(4000)
declare @sqltext2  varchar(4000)
declare @sqltext3  varchar(4000)

select  @sqltext1 = substring( @sqltext, 1, 4000),
        @sqltext2 = substring( @sqltext, 4001, 4000),
        @sqltext3 = substring( @sqltext, 8001, 4000)

exec ( @sqltext1 + @sqltext2 + @sqltext3 )

0
adwisemanCommented:
No, adathelad's right, you can't use a datatype text as a variable.  This example shows how to pull a long script from a text field in a table and execute it.

DECLARE table_script CURSOR
SCROLL STATIC FOR
SELECT table_key, table_name
FROM Northwind.dbo.dd_tables
ORDER BY table_key

OPEN table_script

FETCH FIRST FROM table_script
INTO @key, @name

WHILE @@FETCH_STATUS = 0
BEGIN
      -- The script field in the dd_table is a datatype of TEXT
      -- This routine allows up to 72000 char to be loaded and executed
      SELECT @script1 = ISNULL(substring(script, 0, 8000), ''),
      @script2 = ISNULL(substring(script, 8000, 8000), ''),
      @script3 = ISNULL(substring(script, 16000, 8000), ''),
      @script4 = ISNULL(substring(script, 24000, 8000), ''),
      @script5 = ISNULL(substring(script, 32000, 8000), ''),
      @script6 = ISNULL(substring(script, 40000, 8000), ''),
      @script7 = ISNULL(substring(script, 48000, 8000), ''),
      @script8 = ISNULL(substring(script, 56000, 8000), ''),
      @script9 = ISNULL(substring(script, 64000, 8000), '')
      FROM Northwind.dbo.dd_tables
      where table_key = CONVERT(VARCHAR, @key)

      EXEC(@script1 + @script2 + @script3 + @script4 + @script5 + @script6 + @script7 + @script8 + @script9)

   FETCH NEXT FROM table_script
   INTO @key, @name
END

CLOSE table_script
DEALLOCATE table_script
0
hans_vdAuthor Commented:
I did it like Adathelad proposed
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.