Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to select fields without hard-coding?
Message
From
23/03/2008 15:58:50
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
 
General information
Forum:
Visual FoxPro
Category:
Other
Environment versions
Visual FoxPro:
VFP 9 SP1
Miscellaneous
Thread ID:
01304667
Message ID:
01304753
Views:
21
>Good morning,
>
>I need to create a routine of selecting (using SQL Select) only certain fields from a table/cursor. The fields that are to be selected should not be hard coded into the SQL Select. For example, if I could create an array where the fields would be stored, as e.g.:
>
>
>local array aFlds[3]
>aFlds[1]="myfld1"
>aFlds[2]="myfld2"
>aFlds[3]="myfld3"
>
>
>If I could then use this array in the SQL select to select only these fields. Is something like this possible?
>
>Any other suggestions?

Macro substitution was already suggested; let me add that you can decide whether it is more practical to ampersand the entire SELECT command, or individual clauses. For me, the field list is usually fixed, but the sort order is sometimes not, so I macro-substitute an individual clause, something like this:
if ThisForm.OpgSortBy = 1 && By client Code
  lcSortBy = "ClientCode"
else && By name
  lcSortBy = "LastName, FirstName"
endif
select ...;
  from ClientMain;
  order by &lcSortBy;
  into cursor Tmp;
  nofilter
...
But you might also create an entire SQL - SELECT command and macro-substitute it all at once:
lcSortBy = ... (as above)
lcWhere = "where IsActive"
lcCommand =;
  "select * from ClientMain ";
  + "from ClientMain ";
  + "order by " + lcSortBy;
  + lcWhere;
  + " into cursor Tmp nofilter"
Of course, ycould also add the new-lines, so the command is easier to read in the debugger.

Note that in the "order by" clause, the "order by" is outside the variable, but in the "where" clause, the "where" is part of the variable. This makes it easier to conditionally NOT use a WHERE clause. Otherwise, you would have to put a clause like "where .T.".
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform