2017-06-14 74 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