5

我需要使用 MS Query Analyzer 输出文本字段的内容。我试过这个:

select top 1 text from myTable

(其中文本是一个text字段)

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

第一个只打印前 2000 个左右的字符,第二个只打印前 8000 个字符。有没有办法获取所有的文本?

笔记:

  • 必须与 SQL Server 7 一起使用
4

3 回答 3

9

我不认为你可以在 MSSQL7 中使用 varchar(MAX),所以这里有一些东西可以给你所有的数据(注意,我的理解是你只是想直观地看到数据,你不会放它在一个变量中或返回它)。

因此,这将打印出整个字符串,以便您可以直观地看到该字段中的内容:

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
于 2008-10-20T22:54:49.640 回答
1

我有一段时间没有使用查询分析器了,但是您可以在选项窗口中调整结果窗口中显示的最大字符数。请参阅MSDN文档。

于 2008-10-20T23:24:24.523 回答
0

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

使用这个存储过程。唯一的缺点是每 8000 个字符换行一次 :(

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, @CurrentEnd+@offset, 1073741822)

END /*End While loop*/

这最初发布在 SQLServerCentral.com 上,网址为http://www.sqlservercentral.com/scripts/Print/63240/

于 2011-10-20T10:15:00.400 回答