如何在sql中用单引号连接列值?

goo*_*yui 6 sql sql-server

如何在sql中用单引号和逗号连接列值?

select tpaa_id  from dbo.Sheet
where tpaa_id is not null
Run Code Online (Sandbox Code Playgroud)

目前查询返回,值为..

ABC123
ABC456
Run Code Online (Sandbox Code Playgroud)

我们有大约 1000 条记录。

我希望返回

'ABC123',
'ABC456',
Run Code Online (Sandbox Code Playgroud)

And*_*yev 5

您可以使用变量进行连接:

declare @result nvarchar(max)

select @result  = isnull(@result + ', ', '') + '''' + tpaa_id  + '''' 
from dbo.Sheet
where tpaa_id is not null

select @result
Run Code Online (Sandbox Code Playgroud)


小智 5

使用这种结构

SELECT CONCAT(CHAR(39), MyString ,CHAR(39)) FROM Table

return '<MyString>'
Run Code Online (Sandbox Code Playgroud)


小智 2

要获取带逗号的值,请使用以下语句...

选择字段 1 || ',' 来自表名;