Sql server 在SQL Server中打印文本字段内容的最简单方法

Sql server 在SQL Server中打印文本字段内容的最简单方法,sql-server,database,query-analyzer,Sql Server,Database,Query Analyzer,我需要使用MS查询分析器输出文本字段的内容。我试过这个: select top 1 text from myTable (其中文本是文本字段) 及 第一个只打印前2000个左右的字符,第二个只打印前8000个字符。有没有办法得到所有的文本 注: 必须使用SQL Server 7 我不认为您可以在MSSQL7中使用varchar(MAX),因此这里有一些东西可以为您提供所有数据(注意,我的理解是,您只想直观地查看数据,而不是将其放入变量或返回数据) 因此,这将打印整个字符串,以便您可以直观地看

我需要使用MS查询分析器输出文本字段的内容。我试过这个:

select top 1 text from myTable
(其中文本是
文本
字段)

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

注:

  • 必须使用SQL Server 7

我不认为您可以在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
将@limit声明为int,
@夏伦作为int,
@当前为int,
@字符为varchar(8000)
设置@limit=8000
选择顶部1@charLen=LEN(文本)
从myTable
设置@current=1
而@current<@charLen
开始
选择顶部1@chars=子字符串(文本、@current、@limit)
从myTable
打印@字符
设置@current=@current+@limit
结束

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

使用此存储过程。唯一不好的一面是每8000个字符就有一次换行:(

这最初发布在SQLServerCentral.com上

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
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*/