Alex
Alex

Reputation: 2959

Multiple attributes condition in T-SQL

I'd like to check if a "couple" of attributes is in a the result of another request. I tried the following query but the syntax isn't good.

SELECT ID
FROM Table1
WHERE (Col_01, Col_02) IN
(
    SELECT Col_01, Col_02
    FROM Table2
)

Is-it possible to do something like that in T-SQL ?

Upvotes: 0

Views: 247

Answers (1)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239754

You can use EXISTS and a correlated subquery:

SELECT ID
FROM Table1 t1
WHERE EXISTS
(
    SELECT *
    FROM Table2 t2
    WHERE t2.Col_01 = t1.Col_01 AND
          t2.Col_02 = t1.Col_02
)

You initial attempt was a good one though - some database systems do allow us to use rowset constructors to create arbitrary tuples, and the syntax is quite similar to what you showed, but they're not supported in T-SQL in this part of the syntax, so you have to go this slightly more verbose route.

Upvotes: 1

Related Questions