This will definitely help if the question contains some dimensions of a non-optimized solution (data sizes, timings). There are many methods that can be considered here, some of which are listed in other answers. I assume that the reason you don't want to repeat the same query is performance.
If all applications of the cached identifier set consist of joining the entire set into additional tables, the solution should certainly not include caching the identifier set outside the database. Data should not be moved back and forth if you can avoid this.
In some cases (when cursors or extremely complex SQL are not involved) it may be better (even if it is not intuitive) to not perform caching and simply join repetitive SQL to all desired queries. In the end, each query must be passed on the basis of one of the combined tables, and then the performance largely depends on the availability of indexes needed to join and quickly evaluate all the remaining information.
The most intuitive approach to "caching" a set of identifiers in a database is a temporary table (if it is named #something , it is private for connection and therefore can be used by parallel independent clients or it can be called ##something and be global). If there are many records in the table, indexes are needed. For optimal performance, the index should be a clustered index (only one per valid table) or created only after creating this set, where creating the index is a little faster.
Indexed views are preferred for temporary tables, unless the baseline data is read only during the entire process or when you can and want to ignore such updates to keep the entire set of reports consistent with the set. However, the ability of indexed views to always accurately predict baseline data occurs by slowing down these updates.
Another answer to this question refers to stored procedures. This is pretty much a way to organize your code. However, if you go this route, it is preferable to avoid using temporary tables, since such references to the temporary table prevent the compilation of the stored procedure; go to view or indexed views if you can.
Whatever approach you take, do not guess the performance characteristics and query optimizer behavior. Learn how to display query execution plans (in SQL Server Management Studio) and make sure that you see index calls, as opposed to nested loops that combine several large data sets; add only indexes that explicitly and dramatically change the performance of your queries. A well-chosen index can often change the performance of a query 1000 times, so itβs pretty hard to find out, but itβs important for success.
Last but not least, make sure you use UPDATE STATISTICS when re-building the database (and nightly during production), or your query optimizer will not be able to put the indexes you created into their best use.