Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1070
  • Last Modified:

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.
0
hans_vd
Asked:
hans_vd
1 Solution
 
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now