Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes JDBC and Relational Databases 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
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
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: 3718

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.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: How to use * / Column in sql query
It's not a secret anymore!