SQL Server:将Select语句添加到Select语句中

时间:2015-12-28 07:37:35

标签: sql-server

我想在以下选择查询中 VM.Address VM.City 之间添加逗号

DECLARE @Address NVARCHAR(MAX);
set @Address = ' SELECT  [Address] = CONCAT(VM.Address,' ', VM.City) 

FROM  [dbo].[VendorMaster] VM
    WHERE IsActive = 1 AND VendorID = 6
ORDER BY VendorName'

EXECUTE SP_EXECUTESQL @Address

2 个答案:

答案 0 :(得分:3)

尝试在地址和城市中间的引号内使用逗号:

set @Address = ' SELECT  [Address] = CONCAT(VM.Address,'+''','''+', VM.City) 

答案 1 :(得分:0)

尝试

set @Address = ' SELECT  [Address] = STUFF(VM.Address + COALESCE(', ' + VM.City, '') , 1, 2, '')