问题
If
SELECT ID FROM T2
fails with the following message:
Error: ORA-00904: "ID": invalid identifier
why doesn't
SELECT *
FROM T1
WHERE ID IN
(
SELECT ID FROM T2
)
fail? (it returns all entries from T1)
Is it possible to change this default behavior?
(running the same query, but getting an error instead of all rows)
I have:
- T1 with ID as column
- T2 with ID2 as column (T2 doesn't contain ID)
but let's say that I use SELECT ID FROM T2
(see the example above) instead of SELECT ID2 FROM T2
by mistake. In this situation nothing wrong happens because I use SELECT ... IN SELECT ...
, but it could produce important damages if it would be replaced by DELETE ... IN SELECT ...
.
回答1:
The behaviour is explained in this question.
But you also asked: "Is it possible to change this default behavior? (running the same query, but getting an error instead of all rows)" and expanded on that in a comment with "I want to force the failure for the same query, not to change the query to get the wanted result".
No, it isn't possible to change the behaviour. It's doing what the documentation says it should do:
Oracle resolves unqualified columns in the subquery by looking in the tables named in the subquery and then in the tables named in the parent statement.
You can't make it stop looking at the parent statement and only resolve the unqualified alias inside the subquery. You would have to change the query to make it error. There is no reason not to qualify your identifiers, and plenty of reasons why you should, including that it can prevent you masking mistakes in your code.
回答2:
Yes.
SELECT *
FROM T1
WHERE ID IN
(
SELECT T2.ID FROM T2
)
来源:https://stackoverflow.com/questions/34774242/force-outer-select-to-fail-if-the-inner-select-contains-an-invalid-identifier