Royi Namir
Royi Namir

Reputation: 148644

Convert Sql Server Query from In -> Exists?

I want to convert this query :

SELECT *
FROM   myTable
WHERE   dateCreated = (
                       SELECT MAX(dateCreated)
                       FROM   myTable
                      )
      AND duty IS NULL
      AND CompanyPayingId   IN 
                                  (43081 ,43082 ,43084 ,43085)

to change from in to Exists query ( due to performance) :

However I'm having trouble converting the code cause I think I'll have to duplicate the where clause...

How can I convert this code ?

Upvotes: 0

Views: 260

Answers (1)

AnandPhadke
AnandPhadke

Reputation: 13506

I can do like this:

create table num(idn int)
insert into num values(43081) ,(43082),(43084) ,(43085)
select * from num
SELECT *
FROM   myTable m inner join num n
ON  m.dateCreated = (SELECT MAX(dateCreated) FROM   myTable)
      AND m.duty IS NULL
      AND m.CompanyPayingId=n.num

Upvotes: 1

Related Questions