Question

In Jet, I want to test if certain conditions return any results.

I want a query which returns exactly one record: "true" if there are any results, "false" otherwise.

This works in MS SQL:

SELECT
  CASE
    WHEN EXISTS(SELECT * FROM foo WHERE <some condition>)
    THEN 1
    ELSE 0
  END;

This is what I have tried in Jet:

SELECT IIF(EXISTS(SELECT * FROM foo WHERE <some condition>), 0, 1);

which gives me the error:

Reserved error (-3025); there is no message for this error.

Any ideas?

NOTE I don't want to select "true" multiple times by tacking on a FROM clause at the end, because it could be slow (if the FROM table had many records) or undefined (if the table had 0 records).

Was it helpful?

Solution

How about:

SELECT TOP 1 IIF(EXISTS(
       SELECT * FROM foo 
       WHERE <some condition>), 0, 1) As f1 
FROM foo

Perhaps more clearly:

SELECT TOP 1 IIF(EXISTS(
       SELECT * FROM foo
       WHERE <some condition>), 0, 1) As F1 
FROM MSysObjects

OTHER TIPS

You might be able to use a count

SELECT DISTINCT IIF((SELECT COUNT(*) AS Result FROM [Data Set]), 1, 0) FROM [Data Set];

Most EXISTS queries can be re-written as a left join:

    SELECT
      CASE
        WHEN foo.col is NULL 
        THEN 0
        ELSE 1
    END;
   ... LEFT JOIN foo on <where condition>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top