I have written a complex query that will return me a list of IDs. Now I want to re-use this query so that I join the results with another query. For that I plan to put this into a Stored Proc or an UDF and then use it to insert into a temp table.
Something like below
1) Put the query in a Stored Proc and insert it into temp table
INSERT INTO #TEMP
EXEC SP_COMPLEX(@PARAM1,@PARAM2...@@PARAMN)
2) Put the query in a UDF and insert it into temp table
INSERT INTO #TEMP
SELECT ID_LIST FROM DBO.UDF_COMPLEX(@PARAM1,@PARAM2...@@PARAMN)
I can't see significance difference between the two when I run them for a result of 1000 IDs. But in real implementation the result may be a million rows.
For performance which one would be better ?