Re: HELP - Using a cursor in 4gl
Posted in 1996
Alberto Pardo Fernandez wrote: > > Hi. > > I need to recover data from a table in a 4gl program. I dont know neither the > table name nor the table structure at compile time. Although I can find it out > at run time, I cant declare a record with DEFINE my_record RECORD LIKE tabname.* > (tabname is a variable that holds the name of my table). > > How can I access to the data recovered by the cursor? Is there any way to access > the field values of the cursor? > > DEFINE tablename CHAR(20) > DEFINE my_field CHAR(20) > . > . > . > LET sql = "select * from ", tablename > > PREPARE query FROM sql > DECLARE my_cursor CURSOR FOR query > > FOREACH my_cursor > # Here I want to access the values of the fields of my_cursor > # How can I access the field whose name is the value of my_field > END FOREACH > > Thanks by advance. The real problem is the number of columns. You can find the column names, data types, and other details by getting info from systables and syscolumns. But, the FOREACH cursor requires a known (at compile time) number of values. So, the approach I would take would be so set some upper limit to the number of columns, and set up the SELECT statement so that it fetches table.*, "", "", "", ... for the number of "extra" columns you have set up. Fetch into a set of CHAR variables, and any that are fetched over the number of actual columns will be NULL. I *think* you could get away with having the FOREACH INTO have a number of variables exceeding the number of columns in the SELECT (i.e.; you don't really need to add the "", "", "", ... string to the select statement), but I personnally would be uncomfortable with not making sure the number of columns in the SELECT match the number of variables in the FOREACH. If you're confused by any of this, let me know and I'll try to work up an example over the weekend. I'm not saying I *like* any of the above, by the way; just suggesting a way to meet the requirements layed out. //////////////// ======================================================= ////////// // Dennis J. Pimple Informix Software, Inc. ////// / /// Principal Consultant 5299 DTC Blvd Suite 740 ///// // //// dennisp@informix.com Englewood CO 80111 //// // ///// /// // ////// recept: 303-850-0210 // // /////// direct: 303-740-5611 Opinions expressed are mine, / /////////// fax: 303-843-6408 and do not necessarily //////////////// http://www.informix.com reflect those of my employer