2
votes

I am working with some SQL queries on DB2. Is it possible to select all the columns in a table and also specify certain conditions using the "as" keyword within that select statement? For example, is this query possible:

select
  *,
  col1 + col2 as sum1,
  col3 - col4 as dif1
from 
  table;

Whenever I attempt this, I am getting the SQL0104 error and it is saying "Token , was not valid. Valid tokens: FROM INTO".

Thank you for your help.

EDIT: This query is running inside an SQLRPGLE program on an AS400.

1
Yes it is possible, at least with current versions of Db2-LUW. As you have obfuscated your query , no further advice is possible unless you give facts about your environment (versions, platforms) and the actual query. - mao
....yeesssss, but you generally don't want to, at least at the outermost level: If the underlying table changes it can cause programs to randomly break, since the data-structure used on the program side (especially if RPGLE!) will most likely not have been updated. Doing SELECT * can have other performance issues as well, since you may be selecting columns that could be otherwise eliminated from the query. - Clockwork-Muse

1 Answers

9
votes

Put your table alias in front of the *. So:

select
  t.*,
  col1 + col2 as sum1,
  col3 - col4 as dif1
from 
  table t;