Mike Lewis
Programmer
I've flagged this post as a "helpful tip", but to be honest I'm not sure if it is a tip, a question, or a bug report.
Suppose you have a table that may contain up to four optional columns named extra_1, extra_2, extra_3, extra_4. In any given instance of the table, some or all those columns might or might not be present, in any combination.
Now suppose you want to get all of the fields from the table into a result set; for any missing optional fields, you want a blank column in the result set. You might do this:
But not surprisingly, that will give a "column not found" error if any of the optional fields is not present.
So you do this instead:
Now, you might expect that to work. After all, if a given optional field is missing, the IIF() would evaluate to .F., and the corresponding "extra" field would not be referenced. But it seems that this also gives a "column not found" error - even though the SELECT doesn't need to find the relevant column in the input table.
So, my first questions are: Has anyone seen this behaviour before? And is it what you would expect?
One solution might be to construct the SELECT statement dynamically and then macro-execute it. But here's a simpler solution:
This works, because, if the relevant column is not found, the SELECT will look for a memory variable with the same name instead.
Do you think this is a sensible solution? Are there any dangers or disadvantages?
Any comments or opinions would be appreciated.
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads
Suppose you have a table that may contain up to four optional columns named extra_1, extra_2, extra_3, extra_4. In any given instance of the table, some or all those columns might or might not be present, in any combination.
Now suppose you want to get all of the fields from the table into a result set; for any missing optional fields, you want a blank column in the result set. You might do this:
Code:
SELECT ID, Name, extra_1, extra_2, extra_3, extra_4 ;
FROM TheTable
But not surprisingly, that will give a "column not found" error if any of the optional fields is not present.
So you do this instead:
Code:
SELECT ID, Name, ;
IIF(TYPE("TheTable.extra_1")="C", Extra_1, " ") AS Extra_1, ;
IIF(TYPE("TheTable.extra_2")="C", Extra_2, " ") AS Extra_2 ;
IIF(TYPE("TheTable.extra_3")="C", Extra_3, " ") AS Extra_3 ;
IIF(TYPE("TheTable.extra_4")="C", Extra_4, " ") As Extra_4 ;
FROM TheTable
Now, you might expect that to work. After all, if a given optional field is missing, the IIF() would evaluate to .F., and the corresponding "extra" field would not be referenced. But it seems that this also gives a "column not found" error - even though the SELECT doesn't need to find the relevant column in the input table.
So, my first questions are: Has anyone seen this behaviour before? And is it what you would expect?
One solution might be to construct the SELECT statement dynamically and then macro-execute it. But here's a simpler solution:
Code:
LOCAL extra_1, extra_2, extra_3, extra_4
STORE "" TO extra_1, extra_2, extra_3, extra_4
SELECT ID, Name, extra_1, extra_2, extra_3, extra_4 ;
FROM TheTable
This works, because, if the relevant column is not found, the SELECT will look for a memory variable with the same name instead.
Do you think this is a sensible solution? Are there any dangers or disadvantages?
Any comments or opinions would be appreciated.
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro articles, tips and downloads