I have a stored procedure that I have scaled down considerably for the purpose of this question but in essence the issue I need assistance with is this.
If a row in table xyz is updated I need the ID’s to be appended to each other and output back to the calling application. The update works as expected, the problem is in the manner in which I am building the output @IPV_ID_Found (see commented section at the bottom of the code).
@IPV_Status varchar (50),
@IPV_ID_Found varchar(500) = 'A' OUTPUT
IF (@IPV_Status ='closed')
BEGIN
UPDATE TEST_TBL
SET
Status = 'xyz',
WHERE
ID = @IPV_ID
-- this works for one ID
SELECT @IPV_ID_Found = (CAST(@IPV_ID AS VARCHAR(500)))
-- this does not work for multiple IDs
SELECT @IPV_ID_Found = @IPV_ID_Found + (CAST(@IPV_ID AS VARCHAR(500))) + ','
-- neither does this
SET @IPV_ID_Found = @IPV_ID_Found + (CAST(@IPV_ID AS VARCHAR(500))) + ','
SELECT @IPV_ID_Found
END
See the changes:
then you will get a concatenated list of values, like
1,2,3,4,5BUT if you need to return a recordset, then you need not OUTPUT parameter, use the table variable instead: