I am using dynamic sql, to query the database:
DECLARE @VALUE VARCHAR(5000);
SET @VALUE = '123'
DECLARE @SQL5 NVARCHAR(MAX) = 'Select distinct item_id, attr_val from [dbo].[CONTRACT_ATTR] WHERE [ATTR_VAL] LIKE ''%@VALUE%'' AND [FIELD_ID] = 413 ORDER BY [attr_val]';
SET @SQL5 = replace(@SQL5, '@VALUE', @VALUE);
EXEC SP_executesql @SQL5;
These are the results:
I am trying to take the resulting (item_id) and run another select query. Something like this:
UNION
Select Column3 From @SQL5 where other_column = 1234
The results would be 3 columns for each row returned, the 2 original and the new one found in the 2nd select.
What am I doing wrong and how do I fix it?
