Is it possible to save the results of a query into a variable and be able to use it in an IN statement
Example:
...
declare @ListOfValues ?DATA TYPE? = select Values from tblValueList
while exists (select top 1 RecordID From @tblResults)
begin
If @variable in (@ListOfValues)
begin
...
Since it is bad practice to query inside of a loop (assumption from front end development), what is the best practice to do something like this in back end development?
Well, the answer to your question is “No”. Also, I don’t fully understand your pseudo-SQL. Where does @variable come from?
In any case, there are three answers to your question:
(1) The simplest is to store the list of values in a temporary table. Then you can delete each one in the while loop.
(2) You can use cursors to go through the elements.
(3) You could concatenate the values into a string, and do complicated string manipulation in the while loop.