3

How to select columns in SQL Server 2008 using WHERE and IN, having 2 columns in the where clause. Here is what I have, however I am getting some error message.

select * from tbl
where (col1, col2) in (VALUES ('x1','y1'),('x2','y2'),....);

Msg 4145, Level 15, State 1, Line 2
An expression of non-boolean type specified in a context where a condition is expected, near ','.

Please advise.

0

1 Answer 1

1

I would do something along the lines of

select * from tbl where col 1 in (x1,x2) and col 2 in (y1,y2)

Unless you are looking for specific combinations such as xy and y1 together, you didn't specify enough in your question.

6
  • Actually, I am having 2 columns and 100 rows in a xl sheet that needs to be queried against one table. Each row is a pair. All the 100 rows needs to be queried once against that table. Does it make sense now ?
    – Harisudhan
    Commented Sep 21, 2016 at 17:50
  • Is there a reason you cannot simply import the Excel spreadsheet into a table and join against the 'tbl' for Spreadsheet.col1=tbl.col1, Spreadsheet=tbl.col2? Commented Sep 21, 2016 at 17:54
  • If you can't import it, you can always use OpenRowSet to the Excel spreadsheet and do the join that way.
    – SS_DBA
    Commented Sep 21, 2016 at 17:57
  • This query is not equivalent to what the OP wants to do. Commented Sep 21, 2016 at 18:01
  • Hi Scott, I dont want to import the data into the table. All I need is just to run a select and find out whether all the records present on the system based on COL1 and COL2 pair.
    – Harisudhan
    Commented Sep 21, 2016 at 18:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.