Sunday, February 26, 2012

exists question

this syntax works

... WHERE EXISTS( SELECT * FROM tbl1 WHERE id1 = id3)

but this syntax does not work

... WHERE EXISTS( SELECT * FROM tbl1 WHERE id1 = id3) Or

EXISTS( SELECT * FROM tbl2 WHERE id2 = id3)

Is there some syntax that allows multiple EXISTS in a WHERE clause?

You really haven't given enough information; this query compiles and executes:

create table tbl1 (id1 integer, id3 integer)
go
create table tbl2 (id2 integer , id3 integer)
go

select 'It works' as aTest
WHERE EXISTS( SELECT * FROM tbl1 WHERE id1 = id3) Or

EXISTS( SELECT * FROM tbl2 WHERE id2 = id3)

You need to provide basic schema for tbl1 and tbl2.

|||

thanks for the reply. I retyped my query from scratch and then it worked so I must have had some char in the query that I couldn't see. In my case the query was needed to remove orphaned entries from the docs table

DELETE FROM tblDocs

WHERE ( NOT EXISTS( SELECT * FROM tblDocSummary WHERE tblDocSummary.DocID = tblDocs.DocID) and

( NOT EXISTS( SELECT * FROM tblCorrSummary WHERE tblCorrSummary.DocID = tblDocs.DocID) and

... a few more of the same sort of thing

I kept getting a syntax error even though each line by itself was ok. That lead me to believe that multiple EXISTS were not allowed but apparently they are.

No comments:

Post a Comment