Is it possible to refer to column names via bind variables in Oracle?

前端 未结 3 1522
生来不讨喜
生来不讨喜 2020-12-19 13:30

I am trying to refer to a column name to order a query in an application communicating with an Oracle database. I want to use a bind variable so that I can dynamically chang

3条回答
  •  我在风中等你
    2020-12-19 14:18

    No. You cannot use bind variables for table or column names.

    This information is needed to create the execution plan. Without knowing what you want to order by, it would be impossible to figure out what index to use, for example.

    Instead of bind variables, you have to directly interpolate the column name into the SQL statement when your program creates it. Assuming that you take precautions against SQL injection, there is no downside to that.

    Update: If you really wanted to jump through hoops, you could probably do something like

    order by decode(?, 'colA', colA, 'colB', colB)
    

    but that is just silly. And slow. Don't.

提交回复
热议问题