Frage

Since a few days I'm working with Access 2013 after some years with earlier versions and there seem to have been made some changes to the internal SQL-engine. I'm working on a join query as follows:

SELECT T.STUKLIJSTNUMMER, SR.STUKLIJSTNUMMER, SR.SUB_STUKLIJSTNR
FROM STUKREG AS SR 
INNER JOIN (SELECT * FROM STUKREGsubs2 WHERE SUB_STUKLIJSTNR<>'') AS T ON 
SR.STUKLIJSTNUMMER=T.SUB_STUKLIJSTNR;

STUKREG is a self-referencing table. The STUKREGsubs2 query looks as follows (which works as expected):

SELECT S.STUKLIJSTNUMMER, SR.STUKLIJSTNUMMER, SR.SUB_STUKLIJSTNR
FROM STUKREG AS SR INNER JOIN STUKREGsubs1 AS S ON
SR.STUKLIJSTNUMMER=S.SUB_STUKLIJSTNR;

Of which STUKREGsubs1 is a query (which works as expected):

SELECT SR.STUKLIJSTNUMMER, SR.SUB_STUKLIJSTNR, VAL(SR.STUKLIJSTNUMMER) AS SORDER
FROM STUKREG AS SR
WHERE ABS='Sub' AND CSTR(VAL(SR.STUKLIJSTNUMMER))=SR.STUKLIJSTNUMMER
ORDER BY SR.STUKLIJSTNUMMER;

The query has always worked (in earlier versions) according to my knowledge, but now Access complains: 'The specified field 'STUKLIJSTNUMMER' could refer to more than one table listed in the FROM clause of your SQL statement'. I know what this means but I don't understand why the error occurs, because I'm clearly distinguishing between the source tables/queries. Is it because of the usage of another query in the join part? Any help is appreciated!

War es hilfreich?

Lösung

Make sure to alias columns with the same name from different tables, so that their names in the resulting query, are unique.

So for your STUKREGsubs2 query, add an alias to one of the STUKLIJSTNUMMER-outputs, like this:

SELECT S.STUKLIJSTNUMMER, SR.STUKLIJSTNUMMER AS STUKLIJSTNUMMER_2, SR.SUB_STUKLIJSTNR
FROM STUKREG AS SR INNER JOIN STUKREGsubs1 AS S ON
SR.STUKLIJSTNUMMER=S.SUB_STUKLIJSTNR

Then, make sure that T.STUKLIJSTNUMMER in your first query, refers to the correct column in STUKREGsubs2 (either STUKLIJSTNUMMER from the STUKREGsubs1-query or STUKLIJSTNUMMER_2 from the STUKREG-table).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top