Puedes hacerlo mediante cursores o mediante datastore:
Aquí un ejemplo con cursor en una función:
of_select( /*string as_tabla*/, /*string as_column*/, /*string as_where*/, /*ref string as_values[] */)
// Build the SQL Select statement
// funcion of_select( /*string as_tabla*/, /*string as_column*/, /*string as_where*/, /*ref string as_values[] */)
if isnull(as_tabla) or trim(as_tabla) = "" or isnull(as_column) or trim(as_column)="" or isnull(as_where) or trim(as_where)="" then
return -1
end if
ls_sqlstatement = "SELECT DISTINCT " + as_column + " FROM " + as_tabla + " WHERE " + as_where
// Execute the SQL
prepare sqlsa from :ls_sqlstatement using sqlca;
describe sqlsa into sqlda;
declare c_values_cursor dynamic cursor for sqlsa;
open dynamic c_values_cursor using descriptor sqlda;
fetch c_values_cursor using descriptor sqlda;
ll_rc = sqlca.SQLCode
// Retrieve the distinct values and add them to the array
do while sqlca.SQLCode = 0
choose case sqlda.OutParmType[1]
case TypeString!
ls_value = GetDynamicString (sqlda, 1)
case TypeDate!
ls_value = String (GetDynamicDate (sqlda, 1))
case TypeTime!
ls_value = String (GetDynamicTime (sqlda, 1))
case TypeDateTime!
ls_value = String (GetDynamicDateTime (sqlda, 1))
case else
ls_value = String (GetDynamicNumber (sqlda, 1))
end choose
as_values[UpperBound(as_values)+1] = ls_value
fetch c_values_cursor using descriptor sqlda;
ll_rc = sqlca.SQLCode
loop
close c_values_cursor;
return ll_rc
Con datastore:
of_select ( /*string as_tabla*/, /*string as_campo*/, /*string as_where*/, /*ref any aa_datos[] */)
string ls_select, ls_err,ls_dwsyntax
string ls_type
int li_cont,li_row
datastore ds
ds = create datastore
ls_select = 'SELECT '+as_tabla+'.'+as_campo+' as DATOS'+&
' FROM '+as_tabla
if not isnull(as_where) and as_where <> "" then
ls_select += ' WHERE '+as_where
end if
ls_dwsyntax = SQLCA.SyntaxFromSQL ( ls_select, "Style(Type=grid)", ls_err )
ds.Create ( ls_dwsyntax, ls_err )
IF ls_err <> '' THEN
destroy ds
return -1
end if
ds.SetTransObject ( SQLCA )
li_cont = ds.Retrieve()
if not li_cont > 0 then
destroy ds
return li_cont
end if
ls_type = lower(ds.describe("datos.ColType"))
if ls_type = "!" or ls_type = "?" then
is_error = "Error en el tipo de dato."
return -1
end if
li_cont = 0
for li_row = 1 to ds.rowcount()
li_cont ++
choose case left(ls_type,4)
case "char"
aa_datos[li_cont] = ds.getitemstring(li_row,"datos")
case "deci","numb"
aa_datos[li_cont] =ds.getitemnumber( li_row, "datos")
end choose
next
return li_cont