?
Solved

max length varchar

Posted on 2003-02-27
5
Medium Priority
?
1,056 Views
Last Modified: 2008-03-10
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.
0
Comment
Question by:hans_vd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 23

Accepted Solution

by:
adathelad earned 180 total points
ID: 8034445
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
 
LVL 14

Expert Comment

by:adwiseman
ID: 8034522
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
 

Expert Comment

by:Tyip
ID: 8034527
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
 
LVL 14

Expert Comment

by:adwiseman
ID: 8034566
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
 
LVL 6

Author Comment

by:hans_vd
ID: 8056208
I did it like Adathelad proposed
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question