-2

enter image description here

When I am using an expression as a column(without alias) in my query, the driver returns the column number, instead of the name

How can I fetch the column names, instead of column numbers?

enter image description here

Ashish Ramtri
  • 81
  • 2
  • 10
  • What name do you expect to get for the column `sum(a.salary)`? – forpas Jul 29 '21 at 13:57
  • 1
    change the query to `"SELECT sum(a.SALARY) as SUM_SALARY, a.ID from Q.STAFF a group by a.ID"` ( in other words, give a name (an alias) to the column containing the sum ) – mao Jul 29 '21 at 14:12
  • @mao, forpas - So basically I don't want the end-user to include the alias name next to the expression column explicitly, or in case if he/she forgets to include one, then also, how can I fetch the column expression itself as the alias names, just like in other DBs(Ex.- MySQL), it gets included automatically. – Ashish Ramtri Jul 30 '21 at 05:44
  • I believe Db2 does not offer that functionality, i.e you need to specify the alias, or the colum-name will be its ordinal position in the resultSet. – mao Jul 30 '21 at 10:08

1 Answers1

0

If you want to retrieve columns by name, then give that column a name using AS.

Change your query to:

select sum(a.salary) as total_salary, a.id from q.staff a group by a.id
The Impaler
  • 45,731
  • 9
  • 39
  • 76
  • So basically I don't want the end-user to include the alias name next to the expression column explicitly, or in case if he/she forgets to include one, then also, how can I fetch the column expression itself as the alias names, just like in other DBs(Ex.- MySQL), it gets included automatically. – Ashish Ramtri Jul 30 '21 at 05:47
  • I'm afraid different engines behave differently. DB2 is perfectly compliant with SQL and JDBC in this respect. Maybe your code could intervene and name any unnamed column. – The Impaler Jul 30 '21 at 13:53