Re: Need basic ESQL/C help
Posted in 1997
>From: Dan Copeland <dcopelan@turing.cs.hmc.edu> >Date: Fri, 8 Aug 1997 10:12:49 -0700 >X-Informix-List-Id: <news.41423> > >I'm VERY new to ESQL/C (though not new to C/C++) and have been unable to >figure out some of the basic tenets of ESQL programming. > >For example, I wrote the following function: > >int dbalias (newname,oldname) > $parameter char newname[40]; > $parameter char oldname[40]; >{ > EXEC SQL CREATE SYNONYM :newname FOR :oldname; > > return 1; >} > > When I preprocess this code with the esql command, I get: > >esqlc: "dbio.ec", line 32: Error -33051: Syntax error on identifier or >symbol ':'. Look at the syntax diagram for CREATE SYNONYM in the Informix Guide to SQL: Syntax manual. It makes no mention of using host variables in the places you've tried to use host variables. That means you get syntax errors when you try to use a host variable in that location. The only way to do what you want is by creating the text of the statement dynamically and then executing it. Depending on your version, you may be able to use EXECUTE IMMEDIATE: int dbalias(char *newname, char *oldname) { $char buffer[120]; sprintf(buffer, "CREATE SYNONYM %s FOR %s", newname, oldname); EXEC SQL EXECUTE IMMEDIATE :buffer; return (sqlca.sqlcode == 0); } or you may have to PREPARE, EXECUTE and FREE: int dbalias(char *newname, char *oldname) { $char buffer[120]; sprintf(buffer, "CREATE SYNONYM %s FOR %s", newname, oldname); EXEC SQL PREPARE p_dbalias FROM :buffer; if (sqlca.sqlcode == 0) { EXEC SQL EXECUTE p_dbalias; EXEC SQL FREE p_dbalias; } return (sqlca.sqlcode == 0); } You really need to worry about the EXECUTE failing and the FREE succeeding, but refining the error handling is left as an exercise for the reader. In summary: you cannot use host variables in ESQL/C everywhere you'd like to be able to do so. Most noticably, I cannot think of a place where a table name can be supplied by a host variable. Where you need that functionality, using PREPARE and EXECUTE is the basic solution. Yours, Jonathan Leffler (johnl@informix.com) #include <disclaimer.h> PS: Warning I do not reply to messages with anti-spam in the return path.