Just add an extra variable called lcAdditionalFields, set it to appropriate value starting with , and then build your SQL using lcFields + lcAdditionalFields.
BTW, I prefer TEXT ENDTEXT method here instead.
>I'm using this in a report, works great except that there are fields on the report that may not be in the SQL results. I need to be able to include this in the SQL if the number of records (m.ix or lnReportColumnCount) is only 4:
>
>" ' ' AS Fld5, ' ' AS Fld6, ' ' AS Fld7, ' ' AS Fld8"
>
>But this if the number of records is 6:
>
>" ' ' AS Fld7, ' ' AS Fld8"
>
>This is a section of the block of code:
>
> m.ix = 0
> SCAN
> m.ix = m.ix + 1
> lcFields = lcFields + IIF(m.ix > 1, ', ','') + ALLTRIM(FieldName) + ' AS Fld'+TRANSFORM(ix)
> ENDSCAN
> lnReportColumnCount = m.ix
> RunSQL = 'SELECT ' + lcFields + ' FROM ' + lcTable + ' ORDER BY ' + lcOrderField + ' INTO CURSOR ReportCursor'
> &RunSQL
>
If it's not broken, fix it until it is.
My Blog