aspose file tools*
The moose likes JDBC and the fly likes How to use * / Column in sql query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to use * / Column in sql query" Watch "How to use * / Column in sql query" New topic

How to use * / Column in sql query

Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

Dear All

I have 5 column
1 Col = Code numeric [ 1,2,3 ]
2 Col = Name varchar [David, Anand, Peter ]
3 Col = Salary numeric [ 5000,6000,70000]
4 col = factor char [ * , * , / ]
5 Col = rate numeric [ 10,10,0.5]

How can I use column # 4 in SQL query. Please help.

i.e. select code,name,salary, (salary + factor + rate) as amount from salary_table . This query is not working

Love is GOD and GOD is Love.
Anand Karia
IT Concretor.......
M/s. Anand Karia Concreting IT
Martin Vajsar

Joined: Aug 22, 2010
Posts: 3433

Do I understand it right that you want to use the operator stored in the factor column? It's not possible to do it directly - SQL doesn't evaluate expressions dynamically. The closest you can get probably is:

Hopefully your database supports the case expression, but even if it doesn't, it probably has an if function or a similar construct.

You'll need to hard code every possible operator into the query. It would be a good idea to put a check constraint on the factor column to only allow characters representing supported operators.
Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

Oh Yah.... Sorry I forgot.

Thank you a lot Martin.
It is sorta covered in the JavaRanch Style Guide.
subject: How to use * / Column in sql query
Similar Threads
Hiding / showing / resizing columns dynamically in a table
Data Access Object Design Questions
String Tokenizer help
Its Very Urgent !!!! Help Me Now !!!!!!