2017-06-14 71 views
2

我使用下面的代码来连接我所有的结果到一个记录(由回车符分隔)东西,“对于XML路径”在SQL服务器

SELECT [ConcactColumn] = STUFF((
     SELECT CHAR(10) + t.column 
     FROM #table t 
     FOR XML PATH('')), 1, 1, '' 
    ) 

唯一的问题是我不能够有当我需要至少放置5,000,000个字符时,结果超过了43个字符。

我该如何做到这一点?先谢谢你。

+0

5,000,000陡峭的问为了这。好奇意图是什么?也许有更好的方法来解决这个问题。另一个想法是尝试输出到文本 - CTRL + T。 – Jesse

回答

2

SSMS中字符串的大小是有限的。但是,一个XML的规模并不

  • 右键点击进入查询窗口
  • 选项
  • 结果网格
  • 组XML为 “umlimited”

那就试试这个:

SELECT STUFF(
    (
     SELECT CHAR(13) + CHAR(10) + t.name 
     FROM sys.objects t 
     FOR XML PATH(''),TYPE).value('text()[1]','nvarchar(max)'), 1, 2, '') 
FOR XML PATH(''); 

我剪掉了2个字符STUFF()由于CHAR(13)+CHAR(10)。有了CHAR(10)只有你必须将其更改为...),1,1,'')

如果第一行可能会留空白,你可以去无STUFF

SELECT 
    (
     SELECT CHAR(10) + t.name 
     FROM sys.objects t 
     FOR XML PATH(''),TYPE 
    ).value('text()[1]','nvarchar(max)') 
FOR XML PATH(''); 

单击XML,你看这是由只有你的机器容量限制的结果......

+0

谢谢,我知道可以设置大小限制,但我分心,并且从来没有找到回来的路。我们总是可以依靠你 –

2

如果你正试图从结果网格复制,走在这里http://connect.microsoft.com/SQLServer/feedbackdetail/view/951995/ssms-can-not-paste-more-than-43679-characters-from-a-column-in-grid-mode

偷看也许这可以帮助

Declare @S varchar(max) = '' 
Select @S = @S + char(10) + t.column 
From #table t 
Where t.column is not null 

Select Len(@S) 

SELECT [ConcactColumn] = @S 
+0

它将它作为一个命令,并没有真正产生任何结果。 命令成功完成。 –

+0

这将有助于获得一个很长的字符串,但你会如何得到它? 'PRINT'会削减这一点,'SELECT'也会削减它...我只是在XML结果视图中放置了一个答案如何解决这个问题*这个结果视图的大小没有限制...... – Shnugo

1

这曾经给我适合。我喜欢这个存储过程,虽然我希望我写了,但我没有(在评论中称赞)。

CREATE PROCEDURE dbo.LongPrint @string nvarchar(MAX) 
AS 
/* 
Source: 
https://ask.sqlservercentral.com/questions/3102/any-way-around-the-print-limit-of-nvarcharmax-in-s.html 

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

您将使用PROC这样的:

DECLARE @results varchar(max); 

SELECT @results = STUFF((
     SELECT CHAR(10) + t.column 
     FROM #table t 
     FOR XML PATH('')), 1, 1, ''); 

EXEC dbo.LongPrint @results; 

所以我们清楚:该解决方案是不设计为炽热的牢度。

+0

这段代码是2009年编写的。 ..那些日子很棒,但现在你可以使用无限输出到XML ... – Shnugo