The first type of dynamic SQL EXEC SQL EXECUTE IMMEDIATE:p SQL; 1 . Applies to non-SELECT statements only 2. Embedded SQL statements cannot contain input host variables
voidMain () {EXEC SQL whenever SQLERROR do Sqlerr (); EXEC SQL Connect:serverid; printf ("Connect ok!\r\n"); Charbuf[ -]={0}; while(1) {memset (buf,0,sizeof(BUF)); memset (Psql,0,sizeof(Psql)); printf ("\ r \ n SQL:"); Gets (BUF); if(buf[0]=='0') { Break; } strcpy (PSQL,BUF); //Dynamic SQL the first typeEXEC SQL EXECUTE IMMEDIATE:p SQL; EXEC SQL COMMIT; printf ("SQL statement executed successfully! \ r \ n"); } //Submit DisconnectEXEC SQL COMMIT RELEASE; System ("Pause");}
The second type of dynamic SQL ' SQL statements ' ; The EXEC SQL EXECUTE identifier USING The host variable 1, the host variable 2,1. Applies only to non-select statement 2. Embedded SQL statements can contain input host variables and indicator variables, the number and type of which are known at precompiled time.
voidMain () {EXEC SQL whenever SQLERROR do Sqlerr (); EXEC SQL Connect:serverid; printf ("Connect ok!\r\n"); while(1) {memset (name1,0,sizeof(name1)); printf ("\ r \ n Enter the ID of IDA to be modified:"); scanf ("%d",&ida1); GetChar (); printf ("\ r \ n Enter the value of name to modify:"); Gets (NAME1); if(ida1==0) { Break; } //Dynamic SQL the second type//prepare a fixed SQL statement//Mypresql: Preparing the identifier for the SQL statement//: A and: B: Not a host variable, but a placeholderEXEC SQL PREPARE Mypresql from'Update T2 set name=:a where Ida=:b'; //Execute Dynamic SQLEXEC SQL EXECUTE mypresql using:name1,:ida1; EXEC SQL COMMIT; printf ("SQL statement executed successfully! \ r \ n"); } //Submit DisconnectEXEC SQL COMMIT RELEASE; System ("Pause");}
The third type of dynamic SQL 1 input host variable number fixed query condition fixed 2. Output host variable number fixed return result fixed
voidMain () {EXEC SQL whenever SQLERROR do Sqlerr (); EXEC SQL Connect:serverid; printf ("Connect ok!\r\n"); while(1) {memset (name1,0,sizeof(name1)); printf ("\ r \ n Enter the value of name to query:"); Gets (NAME1); if(name1[0]=='0') { Break; } //Dynamic SQL the third type//prepare a fixed SQL statement//Mypresql: Preparing the identifier for the SQL statement//: A and: B: Not a host variable, but a placeholderEXEC SQL PREPARE Mypresql from'Select Ida,idb,name from T2 where Name=:a'; //Defining CursorsEXEC SQL DECLARE c CURSOR for Mypresql; //open a cursor, pass in a parameterEXEC SQL OPEN c using:name1; //Querying DataEXEC SQL whenever not FOUND does break; while(1{EXEC SQL fetch C into:ida2,:idb2,:name2:name2_ind; if(name2_ind==-1) {memset (name2,0,sizeof(name2)); strcpy (name2,"NULL"); } printf ("ida=%d,idb=%d,name=%s\r\n", ida2,idb2,name2); } //Close CursorsEXEC SQL CLOSE C; } //Submit DisconnectEXEC SQL COMMIT RELEASE; System ("Pause");}
Database proc Programming IX