东西和'对于Xml路径'在Sql Server中

时间:2017-06-14 21:42:34

标签: xml tsql concatenation

我使用以下代码将我的所有结果连接到一个记录(由回车符分隔)

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

唯一的问题是,当我需要至少5,000,000时,我无法获得超过43,420个字符的结果。

我怎样才能做到这一点?提前谢谢你。

3 个答案:

答案 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

答案 1 :(得分: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('');

由于STUFF(),我使用CHAR(13)+CHAR(10)删除了2个字符。只有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,您会看到仅受机器容量限制的结果...

答案 2 :(得分: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, @CurrentEnd+@offset, 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;

所以我们要明确:此解决方案专为超强坚固而设计。