0

如何連接選擇查詢中的行? (在優勢Data Architect中)在選擇查詢中連接行(在Advantage Data Architect中)

我試着運行下面的腳本:

第一個腳本:

declare @str string; 
set @str = ''; 
select @str = @str + field_1 from my_table1 

但我得到的結果,所有的行包含「假」,像這樣的畫面:

enter image description here

第二個腳本:

declare @str string; 
select @str = coalesce(@str + ', ','') + field_1 from my_table1 

這一次,所有行都是空的(注意:「my_table1」中的字段不爲空)。

圖片:

enter image description here

我試着在網上搜索優勢Data Architect中的解決方案,但我無法找到一個解決方案。

+0

表格的定義是什麼?特別是什麼數據類型是你試圖查詢的字段? –

+0

你試圖實現什麼?請清楚地說明你有什麼數據和輸出應該是什麼樣子。 –

+0

@JensMühlenhoff對不起,很久的回答。謝謝您的回答。 我想連接選擇查詢一列的結果。結果列的類型是char。總之,我想從包含選擇查詢的列中得到包含結果的字符串,並用任何符號(',')分隔開來。例如 - 'result1,result2,result3 ...' – netwer

回答

1

我假設你想在MySQL中使用GROUP_CONCAT或在Oracle/Postgres中使用string_agg

對於一般的算法是這樣的:

DECLARE @S STRING; 

DECLARE CURSOR C AS 
SELECT 
    CONVERT(field_1, SQL_CHAR) AS "val" 
FROM 
    my_table1; 

OPEN C; 

WHILE FETCH C do 
    -- Since @S is NULL for the first row this will ensure 
    -- that the result does not start with the separator. 
    @S = COALESCE(@S + ', ' + C.val, C.val); 
END; 

CLOSE C; 

SELECT @S; 

A general function can be found on the ADS forum.

PS:這是splitting a string into separate rows相反。

+0

謝謝你的例子。我認爲這個解決方案對我有好處。 – netwer