user1491884
user1491884

Reputation: 649

Querying against result of EXEC

My EXEC:

SET @Sql = "SELECT r.Id FROM Record r where r.Price > 100"

How do I query against the result of @Sql? Ideally something like this:

SELECT * FROM Record r JOIN Patient p 
ON r.Id = p.recordId 
WHERE r.Id IN (EXEC(@Sql))

Upvotes: 2

Views: 39

Answers (1)

gotqn
gotqn

Reputation: 43626

Maybe this:

SET @Sql = 'SELECT r.Id FROM Record r where r.Price > 100'

CREATE TABLE #DataSource 
(
    [ID] INT
);

INSERT INTO #DataSource ([ID])
EXEC(@SQL);

SELECT * 
FROM Record r 
JOIN Patient p 
    ON r.Id = p.recordId 
WHERE r.Id IN (SELECT [ID] FROM #DataSource);

Upvotes: 1

Related Questions