views:

833

answers:

4

Hello, In SQL Server 2005 I am trying to query a varchar(MAX) column which has some rows with text data that exceed the 8192. Yet, In Management Studio I have under Tools --> Options --> Query Results --> Results to Text --> Max numbers of characters displayed in each column = 8192, which is a maximum. Accordingly, it seems the truncation on these rows occurs only due to the limitation imposed by text output.

The only thing I see to get around this is to use a SUBSTRING function to grab say the first 8000 chars, then the next 8000 chars etc. etc. But this is ugly and error prone.

I should mention that SSIS and BCP are not options for me.

Does anyone have a better suggestion? Thanks!

A: 

The truncation you are talking about only happens in Management Studio. If you pull the column into another app, it will not be truncated.

There's no way you're using Query Analzyer to talk to SQL Server 2005. Do you mean Management Studio?

Joel Coehoorn
I think he does mean Management Studio.If you switch to "Results to Grid", it shows the max per column as 65535. Not sure if that will get you any closer, though - sounds like you're trying to code-gen in SQL?
GalacticCowboy
Sorry, I do mean Management Studio.
Ash Machine
+3  A: 

Does anyone have a better suggestion?

Option 1. Don't get the query results through that tool. Get the query results from your own App that doesn't have this limitation on resulting output.

Option 2. Don't return the data through general result sets, instead issue PRINT commands which are not limitted in this fashion.

David B
+1 for `print`.
Ian Kemp
-1 for `print` not working, in SSMS 2008. Still truncates to 8192 max for me. Okay, a bit mean to actually downvote, but it's an ephemeral downvote.
Gavin Schultz-Ohkubo
@Gavin, print doesn't truncate - it must be something in your code.
David B
@David, simplest SQL I can think of that fails in SSMS 2008: PRINT SPACE(8192) + 'If no truncation, this should print.' I insist that print is truncating.
Gavin Schultz-Ohkubo
Print is definitely truncating, please modify your answer
Jaco Pretorius
PRINT truncates expressions, but does not truncate variables. http://msdn.microsoft.com/en-US/library/ms176047(v=SQL.90).aspx Logically, this would be due to PRINT using varchar(8000) to evaluate the expression.
David B
A: 

If given a choice I would have the query return the data as "For XML Auto" or "For XML Raw" or "For XML explicit" that way the limitations are much higher and you can do much more with the outputed results.

Avitus
This also truncates the output...
Jaco Pretorius
A: 

My solution was a bit round-about but got me there (as long as the output is less than 65535 characters):

  1. In SQL Management Studio, set the limit for grid results to 65535 (Tools > Options > Query Results > SQL Server > Results to Grid > Non XML data)
  2. Run the query, output to grid
  3. Right-click the results, choose "Save Results As..." and save the results to a file
  4. Open the file in notepad or similar to get the output
Luke Sampson