declare @tempContractID VARCHAR(MAX); -- you can't build a string as an int
SELECT @tempContractID = COALESCE(@tempContractID,'')
+ CONVERT(VARCHAR(11),ContractID) -- needs to be a string, as error says
+ ',' FROM dbo.Icn_Contracts -- please always use schema prefix
where title like '%t';
select @tempContractID as allcontrcats;
Though I prefer this method because, if you want to rely on the order of the output, you can (if you add an ORDER BY
to the above query, the resulting order is still undefined).
SELECT @tempContractID = STUFF((SELECT ','
+ CONVERT(VARCHAR(11), ContractID)
FROM dbo.Icn_Contracts
WHERE title LIKE '%t'
ORDER BY ContractID -- well-defined
FOR XML PATH(''), TYPE).value(N'./text()[1]', N'nvarchar(max)'),1,1,'');