我想将查询的结果作为1个字符串连接起来。我该怎么做? 这是我的选择查询
SELECT CAST(mytable.totalamount AS VARCHAR(100)) + ' ' + mytable.curr AS mytotal
FROM(SELECT SUM(oitem_total_amount) AS totalamount
,unit_code_currency AS curr
FROM t_ord_item
WHERE basket_id=230
GROUP BY unit_code_currency
) AS mytable
这是我的结果集
4200.000000 CAD
3735.000000 CNY
19.200000 EUR
19.922175 USD
所以基本上我希望所有4个结果都是1个字符串,以便我可以显示为1个字段。 像
这样的东西4200.000000 CAD <br/>
3735.000000 CNY <br/>
19.200000 EUR <br/>
19.922175 USD
先谢谢
答案 0 :(得分:3)
MS SQL Server 2008架构设置:
CREATE TABLE Test_Table(Value NVARCHAR(1000))
INSERT INTO Test_Table VALUES
('4200.000000 CAD'),
('3735.000000 CNY'),
('19.200000 EUR'),
('19.922175 USD')
查询1 :
SELECT STUFF((SELECT '<br/> ' + Value
FROM Test_Table --<-- Use your Select Query and Table here
FOR XML PATH(''),TYPE).
value('.','NVARCHAR(MAX)'),1,6,'') AS List
<强> Results 强>:
| List |
|----------------------------------------------------------------------------|
| 4200.000000 CAD<br/> 3735.000000 CNY<br/> 19.200000 EUR<br/> 19.922175 USD |
答案 1 :(得分:1)
您可以使用COALESCE。
DECLARE @currString varchar(max)
SELECT @currString = COALESCE(@currString + ' < br/> ', '') + CAST(mytable.totalamount AS VARCHAR(100)) + ' ' + mytable.curr
FROM(SELECT SUM(oitem_total_amount) AS totalamount,unit_code_currency AS curr FROM #t_ord_item WHERE basket_id=230 GROUP BY unit_code_currency
) AS mytable
SELECT mytable = @currString
GO