Oracle - Can't use * sign with other column in select clause

前端 未结 3 1873
时光说笑
时光说笑 2021-01-23 04:56

Sorry if it\'s trivial, but selecting column with * sign isn\'t working always, and I don\'t find reference to this behavior.

I can select table A

相关标签:
3条回答
  • 2021-01-23 05:35

    Why are the errors so misleading?

    The errors are correct. They just don't guess what you are trying to do. After

    select *
    

    the next keyword should be from, so anything else gives

    FROM keyword not found where expected
    

    After , there should be a valid expression such as a column name, not * which is unexpected, so you get

    ORA-00936: missing expression
    

    Perhaps it would be nice if Oracle wrote a special error message about the incorrect use of *, but so far they have not. You could propose it on the Oracle Database Ideas forum.

    0 讨论(0)
  • 2021-01-23 05:38

    The syntax diagram for select shows:

    The outermost path of that shows the plain, unprefixed * all-column wildcard on its own, and there is no loop back around for additional column expressions - all paths with a comma (to separate terms) are distinct from that plain * path.

    On the inner path that does allow a comma, and thus multiple expressions, you can only use .* prefixed by a table/view/alias, and can then follow (or precede) that with other expressions.

    (I really thought that was stated more clearly somewhere, but I can't find it anywhere in recent documentation...)

    Why i must use the alias for * sign ?

    select col,aa.* from A aa;
    

    That isn't quite accurate; you don't have to use an alias, you can use the table name directly if it isn't aliased, so this is also valid:

    select col,A.* from A;
    

    There is a school of thought that you shouldn't use a wildcard anyway, at least for anything except an ad hoc query - it's better to list all of the required column name explicitly, prefixed with the appropriate table name/alias particularly if there is a join, for clarity and to avoid unexpected issues with tables being modified. That's rather outside the scope of this question though *8-)

    0 讨论(0)
  • 2021-01-23 05:47

    The restriction is not so clearly stated in documentation, but you can find it by following this diagram.

    Here you see that if you use the *, you can't use anything else in select list

    0 讨论(0)
提交回复
热议问题