Can I refer to column names through bind variables in Oracle?

I am trying to access a column name to order a query in an application that interacts with an Oracle database. I want to use a bind variable so that I can dynamically change the order of the request.

The problem I ran into is that the database seems to be ignoring column order.

Does anyone know if there is a way to refer to a database column through a bind variable, or if possible?

for example my request

SELECT * FROM PERSON ORDER BY :1

(where it :1will be attached to PERSON.NAME) The query does not return the results in alphabetical order, I am worried that the database interprets this as: -

SELECT * FROM PERSON ORDER BY 'PERSON.NAME' 

which obviously will not work.

Any suggestions are greatly appreciated.

+3
3

. .

. , , , , .

SQL, . , SQL, .

: , , -

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

. . .

+6

JDBC. , - . , , :

    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);

    String columnName = getColumnName(input);
    Statement select = conn.createStatement();
    ResultSet result = select.executeQuery("SELECT * FROM PERS ORDER BY "+columnName);
+2

ResultSet = select.executeQuery( "SELECT * FROM PERS ORDER BY" + columnName);

NEW . , , , "" , , , . , , , - , . SQL , .

0

Source: https://habr.com/ru/post/1704193/


All Articles