Prerequisites: In MS Access 2010 create the following tables:
CREATE TABLE ATBL(ID INT);
INSERT INTO ATBL(ID) VALUES (1);
INSERT INTO ATBL(ID) VALUES (2);
INSERT INTO ATBL(ID) VALUES (3);
CREATE TABLE BTBL(ID INT);
INSERT INTO BTBL(ID) VALUES (1);
INSERT INTO BTBL(ID) VALUES (2);
Also create a view called BVIEW which uses the following SELECT statement:
SELECT A.ID FROM ATBL AS A WHERE A.ID = 1 OR A.ID = 2
Now BVIEW should have the same contents as BTBL. Nevertheless the following two queries will return different results:
SELECT A.ID FROM ATBL AS A WHERE EXISTS (SELECT 1 FROM BTBL AS B WHERE B.ID=A.ID)
SELECT A.ID FROM ATBL AS A WHERE EXISTS (SELECT 1 FROM BVIEW AS B WHERE B.ID=A.ID)
The first query returns two records (1 and 2), but the second query returns all records from ATBL. What's wrong here? Am I missing something?
A view is really a saved SQL SELECT statement. At least, this is what a saved view in MS Access is. And you use the same inner variables A and B. IMHO, they are getting mixed. The last line really looks as
SELECT A.ID FROM ATBL AS A WHERE EXISTS (SELECT 1 FROM (SELECT A.ID FROM ATBL AS A WHERE A.ID = 1 OR A.ID = 2) AS B WHERE B.ID=A.ID)
Try to change some inner names, for example:
SELECT AA.ID FROM ATBL AS AA WHERE AA.ID = 1 OR AA.ID = 2
So, the last line will look as
SELECT A.ID FROM ATBL AS A WHERE EXISTS (SELECT 1 FROM (SELECT AA.ID FROM ATBL AS AA WHERE AA.ID = 1 OR AA.ID = 2) AS B WHERE B.ID=A.ID)
So, as we see here, MS Access even doesn't know how to isolate aliases!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With