Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 change what to order the query by.

The problem that I am having is that the database seems to be ignoring the order by column.

Does anyone know if there is a particular way to refer to a database column via a bind variable or if it is even possible?

e.g my query is

SELECT * FROM PERSON ORDER BY :1

(where :1 will be bound to PERSON.NAME) The query is not returning results in alphabetical order, I am worried that the database is interpreting this as:-

SELECT * FROM PERSON ORDER BY 'PERSON.NAME' 

which will obviously not work.

Any suggestions are much appreciated.

like image 538
Scottm Avatar asked Mar 04 '09 10:03

Scottm


People also ask

What is the use of bind variables in Oracle?

You can use bind variables for such things as storing return codes or debugging your PL/SQL subprograms. Because bind variables are recognized by SQL*Plus, you can display their values in SQL*Plus or reference them in other PL/SQL subprograms that you run in SQL*Plus.

How do you refer to bind variables?

After the declaration of bind variable in SQL* Plus, we can access that variable, or we can say that reference the bind variable in Pl/SQL by using a colon (:) immediately followed by the variable name. In the above example, we first type a colon (:) immediately followed by the variable name.

How do you refer to bind variables in PL SQL?

Unlike user variables which you can access simply by writing their name in your code, you use colon before the name of bind variable to access them or in other words you can reference bind variable in PL/SQL by using a colon (:) followed immediately by the name of the variable as I did in the previous section.

Which of the following is another name for bind variable?

Bind parameters—also called dynamic parameters or bind variables—are an alternative way to pass data to the database.


2 Answers

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.

like image 90
Thilo Avatar answered Oct 26 '22 19:10

Thilo


As you are using JDBC. You can rewrite your code, to something without bind variables. This way you can also dynamically change the order-by e.g.:

String query = "SELECT * FROM PERS ";
if (condition1){
  query = query+ " order by name ";
// insert more if/else or case statements
} else {
  query = query+ " order by other_column ";
}
Statement select = conn.createStatement();
ResultSet result = select.executeQuery(query);

Or even:

String columnName = getColumnName(input);
Statement select = conn.createStatement();
ResultSet result = select.executeQuery("SELECT * FROM PERS ORDER BY "+columnName);
like image 40
Edwin Avatar answered Oct 26 '22 21:10

Edwin