Re: Dynamic ESQL/C
Posted in 1998
Topics: Connectivity: ESQL/C, 4GL & Embedded SQL, Data Types & Schema Design
Robert Bresner wrote: > Have you tried reading the query in from the file and doing a PREPARE? EXEC SQL PREPARE myQuery FROM :sqlQuery // where sqlQuery is the string // that contains the query I've got something that generates the query based on user input. The one thing I distinguished was select vs. anything else, because select I had to allocate a return descriptor (and a return section). It's been several months since I wrote this, so I don't remembe how well a descriptor would work if you tried to do a generic sql for all statements. I've found the descriptor/describe very useful in returning unknown # of columns with unknown data types. Order of operation: EXEC SQL ALLOCATE DESCRIPTOR 'myDescriptor' EXEC SQL PREPARE myQuery FROM :sqlQuery EXEC SQL DECLARE myQueryCursor CURSOR FOR myQuery; EXEC SQL OPEN myQueryCursor; EXEC SQL DESCRIBE myQuery USING SQL DESCRIPTOR 'myDescriptor'; EXEC SQL GET DESCRIPTOR 'myDescriptor' :count=COUNT // gets # cols EXEC SQL FETCH myQueryCursor USING SQL DESCRIPTOR 'myDescriptor'; // Get the individual fields data. Need the INDICATOR in // there in case fields are null. GET DESCRIPTOR returns // an error if the value is null and you do not retrieve // INDICATOR. EXEC SQL GET DESCRIPTOR 'myDescriptor' VALUE :i :retColumn=NAME, :retData=DATA, :isnull=INDICATOR; I've probably given you more than you every wanted to know, but hey, it's cough medicine talking... If this sounds reasonable, read up on descriptor/describe in ESQL/C, ch 10 (well, that's where it is in my version, which is probably ancient). > So, today I learned ESQL/C. And I tried rewriting db-access. > It's a very simple program, and of course it doesn't work. The part that's > giving me trouble > has to do with the dynamic SQL part. According to this book I have, (I-ESQL/C > Programmer's Manual, Imfx Press), > before I start building a dynamic sql statement, I have to know what it is. If > it's a select, I use a certain structure. If it's a delete, I use another > structure... I'm hoping there's a better way than this... > > And so, my question: > How do I build and execute a dynamic sql statement, with ESQL/C, when the > sql is *so* dynamic, that even I don't know what it is? > > Thanks you for your time. Here's a recipe: > Why, thank you! Sounds good. Teresa