tags:

views:

218

answers:

8

This is the SQL query I wrote in MS Access 07:

IF EXISTS (Select * FROM MyTable) print 'Yes' else print 'No'

This is the error it's giving me when i run the query:

"Invalid SQL Statemenet; Expected DELETE, INSERT, PROCEDURE, SELECT, UPDATE"

The query is correct, as far as I know, I think it's from Access, can anyone help me please? Thanks

+8  A: 

IF EXISTS cannot be used in MS Access.

Take a look at this link, it might be what you are after.

Ardman
tried, the same thing
DaJackal
What do you mean by "tried, the same thing"? If you want help, you're going to have to do more than the web equivalent of mumbling.
David-W-Fenton
But you can use `...IIF(EXISTS...` e.g. `SELECT DISTINCT IIF(EXISTS (SELECT * FROM MyTable), 1, 0) FROM Customers;`
onedaywhen
A: 

Perhaps you need a BEGIN - END.

UPDATE: Sorry to be the bearer of bad news.

http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_22102849.html

Looks like MS Access has no control of flow execution in its SQL syntax. It only executes a single command. (based on the link above)

Apparently there is no conditional statement in MS Access.

pymendoza
tried, the same thing
DaJackal
looks like you can do the if exists statement in access
pymendoza
A: 

As far as I know: Access has a light version from SQL. You won't be able to use TSQL. You can try to use vba to achieve what you want.

VeeWee
Now that's what I thought, that this is rather a software problem than a sql query problem. But I need to use Access and i don't know how to solve it. Do you how other idea?
DaJackal
Access has modules and macros. You can use these to program your actions. You can run SQL in VBA via ADO I think, but there are also build in functions to test tables. Like dmax, dcount, dmin, ...
VeeWee
A: 

I have no idea about MS Access, but what about the following:

IF SELECT EXISTS (SELECT 1 FROM mytable WHERE somecondition) THEN...

The error message "Invalid SQL Statemenet; Expected DELETE, INSERT, PROCEDURE, SELECT, UPDATE" suggests you are missing one of those. You need to SELECT EXISTS, rather than just calling EXISTS.

This works in PostgreSQL, but I'm not sure if Access is different.

mattjf
Thanks, but no, it doesn't work here. It gives me the same error
DaJackal
If you have no idea about Access, DON'T POST YOUR ANSWER. -1
David-W-Fenton
Wow, I'm beside myself. So offering up a possible suggestion to a fairly similar problem, which exhibits similar behavior under SQL, is such a terrible thing as to not only require a downvote, but also for you to shout? Do you have a complex about your expertise? Are you concerned with the amount of space my comment takes up in the SO database? Don't worry, it's not MS Access, it can handle it.
mattjf
Do you think that if I provided Jet/ACE-specific SQL for a SQL Server question and the answer didn't work that it would be just fine and dandy? I'd expect to get crucified -- it's a wrong answer if the database involved can't do it, and it shows you're more interested in spouting off than in actually providing a solution (since you don't care enough to check if your solution actually works).
David-W-Fenton
I actually do think it'd be fine. I think SO is a wonderful site where people of all different backgrounds can offer up ideas, with the correct answers can be chosen from all of the responses. You live in a world where people trying to be helpful on a forum is grounds for being crucified? I'm really sorry for you. Honestly, I'm not trying to be condescending. The amount of rage you show towards incorrect Access posts is very troubling.
mattjf
Providing an untested answer for a platform you do not use is not being helpful -- it just pollutes the usefulness of SO. I think posting untested wrong answers deserves pushback. This particular answer is not useful at all as part of the solution to the original question (it just recapitulates the original error, which is using a SQL operator that the Jet/ACE SQL dialect does not support) and in my opinion should be deleted.
David-W-Fenton
(Wow! 2 people arguing on the internet over a trivial subject! I never thought it possible!) @ David, I agree that he should have reconsidered posting, but i think the real problem was the rudeness of your first comment. Without proper respect for your audience (mattjf), your comment just invites silly flame wars like this. You and I have chatted a few times via comments and you're normally much more polite than this.
PowerUser
+4  A: 

You can't use IF EXISTS in Access. EXISTS() is allowed but only as an evaluative expression, not a procedural one. The EXISTS() expression returns True or False (in the form of -1 and 0).

Imagine that you could use a procedural expression... where are you trying to PRINT the result? There is no place for text results to be printed.

Please describe more clearly what problem you're trying to solve, and where you want the results to appear, and we can help you better.

Taking a wild guess, if you did want a query to return 'Yes' or 'No' then this can do it for you:

SELECT Iif(EXISTS (SELECT * FROM MyTable), "Yes", "No") AS MyTableHasRows
FROM Numbers
WHERE Num = 1;

There's a problem here, though: Access doesn't allow SELECT statements that don't refer to a table. (SQL Server allows this, and Oracle offers the fake DUAL table to select from.) One way around this is to select from a table that has only one row. My query above selects from a Numbers table that's in my test database, where only one row has Num = 1. Selecting from a table with many rows makes the engine do extra work for no reason.

If you want to do this checking in code, here's the best-performing way I can think of:

If CurrentDb().OpenRecordset("SELECT Top 1 * FROM MyTable").RecordCount > 0 Then
   Debug.Print "Yes"
Else
   Debug.Print "No"
End If

This is logically identical to EXISTS and will perform better than COUNT(*) which has to look at every row in the table.

Emtucifor
You CAN use `EXISTS` in Access e.g. Intermediate Microsoft Jet SQL for Access 2000 (http://msdn.microsoft.com/en-us/library/aa140015(office.10).aspx): "The EXISTS predicate is used in subqueries to check for the existence of values in a result set... "
onedaywhen
You can also do this: `SELECT DISTINCT IIF(EXISTS (SELECT * FROM MyTable), 1, 0) FROM Customers;`
onedaywhen
@onedaywhen Thank you for the correction! Please see my changes. Is that better? An undo would be appreciated.
Emtucifor
@Emtucifor: even better: I changed to an upvote.
onedaywhen
@onedaywhen - thanks! I also fixed my weird duplication (obviously a paste error)
Emtucifor
A: 

As others said before me, Access doesn't know IF EXISTS.
You could do something like this in VBA:

Public Sub Foo()

    If Not IsNull(DLookup("SomeColumn", "MyTable")) Then
        MsgBox "yes"
    Else
        MsgBox "no"
    End If

End Sub
haarrrgh
A: 

A few points:

  1. Access SQL uses iif(), not IF
  2. Access SQL doesn't use EXISTS
    Edit:
    Actually it does work, even though I couldn't find it in the help files. This works fine:
    Select (IIf(Exists (SELECT 1 FROM [Table1]),'Y','N')) as A from [Table1];
    /Edit
  3. DCount is the key:
    iif(
    DCount("AnyFieldName","TableName")>0,
    "Yes",
    "No"
  4. I suggest bookmarking this site for future reference. Really helpful.
    http://www.techonthenet.com/access/functions/
  5. The Access help files are actually pretty helpful and straightforward. They describe parameters, output, give examples, etc. I recommend using them frequently.
PowerUser
To be clear regarding point 2, Jet/Access SQL does have an `EXISTS` operator. It is perfectly valid to write a query using an expression like `IIf(Exists (SELECT 1 FROM [TableName]),'Y','N')`.
Cheran S
I made #2 on the basis that I've never heard of it before and I couldn't find it in the help files. Thanks for the correction:)
PowerUser
+1  A: 

If you want to know if a table has records in it, you can check CurrentDB.TableDefs("MyTable").Recordcount. That will never be 0 for a table with records in it.

Now, where you can use that is a different kettle of fish, but your question is so vague as to make it impossible to provide anything other than generalized approaches to solving the problem (which hasn't really been defined in the first place).

David-W-Fenton
Alternatively: `CurrentProject.Connection.OpenSchema(adSchemaStatistics, Array(Empty, Empty, "MyTable")).Fields("CARDINALITY").Value` -- p.s. congratulations on attaining your ms-access badge ;)
onedaywhen
I don't quite understand why it's giving me the bronze badge (100 upvotes) today when I earned the silver badge (400 upvotes) in the same tag some time ago. Seems to me something is screwed up somewhere along the line (only @Remou and I have the MS-ACCESS silver badge!).
David-W-Fenton