Create varchar from SELECT result
DECLARE @result varchar(MAX)
SELECT
NAME
FROM
Table
WHERE
ID = @Id
I need开发者_如何学C to fill @result
with result Names, separated by ','.
SELECT
returns 'A','B' and 'C', the @result
should be 'A, B, C'.
It is Microsoft SQL Server 2008.DECLARE @result varchar(MAX)
SET @result = '';
SELECT
@result = @result + NAME + ','
FROM
Table
WHERE
ID = @Id
SET @result = SUBSTRING(@result, 1, LEN(@result) - 1)
SELECT @result
Enjoy :D
DECLARE @CSV varchar(max)
SET @CSV = ''
SELECT @CSV = @CSV + Col1 + ',' FROM Table WHERE ...
SET @CSV = LEFT(@CSV, LEN(@CSV) -1)
PRINT @CSV
Try this
declare @result varchar(max)
select @result = COALESCE(@result + ', ', '') + name
from table
DECLARE @Contribution AS VARCHAR(MAX) SET @Contribution = ( SELECT RTRIM(Ltrim(C.strname)) + '; ' FROM tbl M INNER JOIN tbl2 ON tbl2.ID =tbl.CID WHERE M.PID =@PID ORDER BY C.Orderby FOR XML PATH('') )
Print @Contribution
精彩评论