Subject RE: [firebird-support] Converting FireBird Table from horizontal to vertical
Author Leyne, Sean
Allan,

> I've created a query to transfer all the ID's which have data in the
> FEATURES column. After running the query, I edited the query so that it
> would transfer the ID's which have valid data in FEATURES_2 column,
> edited it again for FEATURES_3, and so on, till I'd edited and executed
> the query 30 times.
>
> Having done it the "hard way", I now wish to make this into a procedure
> ... and since I'm such a rookie, I've not yet been able to figure out
> how to write a short procedure, with a WHILE DO loop, to transfer one
> FEATURES column at a time.
>
> I'm hoping that someone here will be willing and able to point me in
> the right direction.
>
> I loved the days when I worked with Paradox and it would record macros,
> which I'd cut/paste, edit and combine and VOILA, it was done :)

Turn the problem "around".

What you want to do is create a single SELECT which uses the UNION to create a single set of rows to be inserted into the new table.

So, think in these terms:

SELECT ID, Features FROM Table WHERE Features IS NOT NULL
UNION
SELECT ID, Features_1 FROM Table WHERE Features_1 IS NOT NULL
UNION
SELECT ID, Features_2 FROM Table WHERE Features_2 IS NOT NULL
...
UNION
SELECT ID, Features_x FROM Table WHERE Features_x IS NOT NULL


Sean