将文本附加到sql select查询的每一行

时间:2013-06-06 14:38:56

标签: sql-server-2008

我有这样的查询

SELECT COUNT(ID) 'Records Affected', TYPE FROM MASTER
GROUP BY TYPE

此输出为

Records Affected            TYPE
----------------            ---- 
4                            F1
3                            F2
5                            F3

现在我想更改查询,以便输出如下

Records Affected
----------------
The number of records affected for F1 is : 4 
The number of records affected for F2 is : 3
The number of records affected for F3 is : 5

"The number of records affected for " + TYPE + " is : " + COUNT.

如何将默认文本添加到结果集的每一行,而不是附加到前端。我想简化我只是将DataGrid中的记录显示为摘要的任务。

5 个答案:

答案 0 :(得分:10)

您可以使用以下内容轻松连接字符串。您将使用+将字符串连接到type列和count。请注意,count需要转换为varchar才能生效:

SELECT 
  'The number of records affected for '+ type +
    ' is : '+ cast(COUNT(ID) as varchar(50)) as'Records Affected'
FROM yt
GROUP BY TYPE;

请参阅SQL Fiddle with Demo

答案 1 :(得分:1)

只需将文字放入您的查询中:

SELECT 'The number of records affected for ' + TYPE + ' is : ' + CAST(COUNT(ID) as VARCHAR(20)) AS 'Records Affected' FROM MASTER
GROUP BY TYPE

答案 2 :(得分:1)

SELECT "The number of records affected for " + TYPE + " is : " + COUNT(ID) AS [Records Affected]
FROM Master 
GROUP BY TYPE

答案 3 :(得分:0)

试试这个:

SELECT 'The number of records affected for ' + TYPE + ' is : ' + 
STR(X.[Records Affected]) AS [Records Affected]
FROM (SELECT COUNT(ID) 'Records Affected', TYPE FROM MASTER GROUP BY TYPE) X

答案 4 :(得分:0)

使用此查询:

UPDATE bookmark_linx SET link_url=(SELECT CONCAT(link_url, '?raw=true')) WHERE link_url LIKE '%dropbox%'