Re: Dynamic SQL with cursors
Posted in 1998
Topics: Connectivity: ESQL/C, 4GL & Embedded SQL
> > Basically, I have an existing application with a function that has a > parameter x. I have several cursors that I am opening, and inside my > function I want to fetch from one of these cursors depending on the value of > x (basically I want to alternate between cursors depending on x). > It seems like I should be able to use dynamic SQL to do this, building the > name of the cursor to be used, etc with sprintfs, but my documentation says > that you can't use prepare/execute for declare, execute, fetch and open > statements (which just happen to be the ones I need!). I tried to do it > anyway just for giggles but I end up getting a syntax error. > What you need to do is Based on the value of x, prepare a statement. A sample ESQL/C code is attached. Use the same idea for 4gl ie: switch (x) { case 1 : strcpy( tmp_str, "Select * from xyz where pqr = 1001"); case 2 strcpy(tmp_Str, "select from junk where abc=123 "); ..... } exec sql prepare my_stmt from :tmp_str; exec sql declare my_cursor cursor for my_stmt; and so on.... Hope this helps... cheers abhilash