Subject Re: Re[2]: [firebird-support] select column if it exists
Author Walter R. Ojeda Valiente
What about:

SELECT
   A,
   B,
   COALESCE(C, 'some value here')
FROM
   MyTable

Greetings.

Walter.


On Tue, Oct 10, 2017 at 2:56 PM, 'Daniel Miller' dmiller@... [firebird-support] <firebird-support@yahoogroups.com> wrote:
 

I don't think you could express it in the SELECT directly (someone else
will probably build a statement refuting that) but you could do:

SELECT r.RDB$FIELD_NAME FROM RDB$RELATION_FIELDS r where
r.RDB$RELATION_NAME=<your table name here>

and then process that result set to build your appropriate SELECT for
the target table. So I suppose you could build a statement with a
quantity of derived sets using IIF and WHERE EXISTS - the question is do
you need to express this as single SELECT, or can you accomplish your
goal either via your calling program or a stored procedure?
--
Daniel

On 10/9/2017 1:36:20 AM, "Elmar Haneke elmar@...
[firebird-support]" <firebird-support@yahoogroups. com> wrote:

>
>>Is there a way to include a column in a SELECT but substitute a value
>>if
>>it doesn't? I need my code to work with different versions of my db
>>schema.
>>
>>eg I want to SELECT A, B, C, ... but C might not exist.
>>
>>SELECT * would work of course, except it will fetch a bunch of columns
>>I
>>don't need.
>
>You can read the list of fields available in advance and modify your
>query ommitting missing columns.
>
>
>Elmar
>
>
>
>----------------------------- -------
>Posted by: Elmar Haneke <elmar@...>
>----------------------------- -------
>
>+++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++ +++++++
>
>Visit http://www.firebirdsql.org and click the Documentation item
>on the main (top) menu. Try FAQ and other links from the left-side
>menu there.
>
>Also search the knowledgebases at
>http://www.ibphoenix.com/ resources/documents/
>
>+++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++ +++++++
>----------------------------- -------
>
>Yahoo Groups Links
>
>
>