2008-10-20 9 views
5

Je dois envoyer le contenu d'un champ de texte à l'aide de MS Query Analyzer. Je l'ai essayé:Manière la plus simple d'imprimer le contenu d'un champ de texte dans SQL Server

select top 1 text from myTable 

(où le texte est un champ text)

et

DECLARE @data VarChar(8000) 
select top 1 @data = text from myTable 
PRINT @data 

La première imprime uniquement les premiers quelque 2000 caractères et la seconde n'imprime la première 8000 caractères. Y a-t-il un moyen d'obtenir tout le texte?

Notes:

  • doit travailler avec SQL Server 7

Répondre

8

Je ne pense pas que vous pouvez utiliser VARCHAR (MAX) dans MSSQL7, alors voici quelque chose qui vous donnera toutes les données (notez, ce que je comprends, c'est que vous voulez juste voir les données visuellement, et vous n'allez pas le mettre dans une variable ou le retourner).

Donc, cela va imprimer afin que vous puissiez voir visuellement hors toute la chaîne ce qui est dans le domaine:

DECLARE @limit as int, 
     @charLen as int, 
     @current as int, 
     @chars as varchar(8000) 

SET @limit = 8000 

SELECT TOP 1 @charLen = LEN(text) 
FROM myTable 

SET @current = 1 

WHILE @current < @charLen 
BEGIN 
    SELECT TOP 1 @chars = SUBSTRING(text,@current,@limit) 
    FROM myTable 
    PRINT @chars 

    SET @current = @current + @limit 
END 
1

Je ne l'ai pas utilisé Analyseur de requêtes dans un certain temps, mais vous pouvez régler le nombre maximum de caractères s'affiche dans la fenêtre des résultats dans la fenêtre Options. Voir la documentation MSDN.

0

http://shortfastcode.blogspot.com/2011/10/getting-around-sql-server-print-8000.html

Utilisez cette procédure stockée. Le seul inconvénient est que vous obtenez une ligne briser tous les 8000 charachters :(

CREATE PROCEDURE [dbo].[LongPrint] 
     @String NVARCHAR(MAX) 

AS 

/* 
Example: 

exec LongPrint @string = 
'This String 
Exists to test 
the system.' 

*/ 

/* This procedure is designed to overcome the limitation 
in the SQL print command that causes it to truncate strings 
longer than 8000 characters (4000 for nvarchar). 

It will print the text passed to it in substrings smaller than 4000 
characters. If there are carriage returns (CRs) or new lines (NLs in the text), 
it will break up the substrings at the carriage returns and the 
printed version will exactly reflect the string passed. 

If there are insufficient line breaks in the text, it will 
print it out in blocks of 4000 characters with an extra carriage 
return at that point. 

If it is passed a null value, it will do virtually nothing. 

NOTE: This is substantially slower than a simple print, so should only be used 
when actually needed. 
*/ 

DECLARE 
       @CurrentEnd BIGINT, /* track the length of the next substring */ 
       @offset tinyint /*tracks the amount of offset needed */ 

set @string = replace( replace(@string, char(13) + char(10), char(10)) , char(13), char(10)) 

WHILE LEN(@String) > 1 
BEGIN 

IF CHARINDEX(CHAR(10), @String) between 1 AND 4000 
    BEGIN 

SET @CurrentEnd = CHARINDEX(char(10), @String) -1 
      set @offset = 2 
    END 
    ELSE 
    BEGIN 
      SET @CurrentEnd = 4000 
      set @offset = 1 
    END 

PRINT SUBSTRING(@String, 1, @CurrentEnd) 

set @string = SUBSTRING(@String, @[email protected], 1073741822) 

END /*End While loop*/ 

Cela a été posté sur SQLServerCentral.com à http://www.sqlservercentral.com/scripts/Print/63240/

Questions connexes