Search code examples
sqlsql-servert-sqlssms

TSQL same SELECT with variable WHERE condition


I need to run the following simple query from SQL SERVER Management Studio

SELECT a FROM myTable WHERE b = 'X';

The value of X corresponds to a text file as

x1
x2
x3
...
x40000

In ither words, I need to loop through the values in the text file.

This must be simple but I cannot really find a way to do it other than the naive case of running the query 40000 times.

EDIT: I was already told off in the comments that I should simply import the values into a temporary table and then JOIN as required. That is fair. Appreciate the comment.

Any idea will be welcome!

Thanks


Solution

  • If I well understood the case: you have two data sources, the first is a SQL Table and the Other is a Text file and you make a Loop generate the result.

    In this case, I would suggest you import the data of txt into a table at once, Then you make join between the two tables so that can be done in one query execution.