Force outer SELECT to fail if the inner SELECT con

2019-06-03 22:42发布

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 ....

2条回答
唯我独甜
2楼-- · 2019-06-03 23:06

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.

查看更多
三岁会撩人
3楼-- · 2019-06-03 23:08

Yes.

SELECT * 
    FROM T1
    WHERE ID IN 
        ( 
            SELECT T2.ID FROM T2
        )
查看更多
登录 后发表回答