skip to main content

Embedded SQL Statements : EXECUTE

EXECUTE
The EXECUTE statement executes a SQL statement.
General format
Format 1
EXEC SQL [AT Database]
  EXECUTE Prepared-Statement [ USING Host-Variable, ... ] [ INTO Host-Variable, ... ]
Format 2
EXEC SQL [AT Database]
  EXECUTE IMMEDIATE Statement [ USING Host-Variable, ... ] [ INTO Host-Variable, ... ]
Format 3
EXEC SQL [AT Database]
  EXECUTE {BEGIN} [Plsql]
Syntax rules
1.
Prepared-Statement is a Nonnumeric Literal, as defined in the Definitions section of the Preface of this document.
2.
3.
Statement can be a host variable or a Nonnumeric Literal containing a complete SQL Statement.
4.
Plsql is a Nonnumeric Literal containing a PLSQL code.
General rules
1.
Prepared-Statement must be previously defined by a PREPARE statement.
2.
3.
Database identifies the active connection that will execute the query and must be previously defined using a Format 4 DECLARE statement.
Examples
Format 1 - Execute a prepared statement to count records that meet a criteria
exec sql
        "select count(*) from cust_table where cust_code > ?"
move 1990 to min-key
exec sql
display "Count of records with key > " min-key " : " the-count
Format 2 - Execute inmmediate a statement to count records that meet a criteria
move 1990 to min-key
exec sql
             "select count(*) from cust_table where cust_code > ?"
display "Count of records with key > " min-key " : " the-count
Format 3 - To be used with Oracle Databases only using PL/SQL
exec sql
        FOR emp_rec IN (SELECT empno, sal, comm FROM emp) LOOP
           bonus := (emp_rec.sal * 0.05) + (emp_rec.comm * 0.25);
           INSERT INTO bonuses VALUES (emp_rec.empno, bonus);

Copyright (c) 2017 Veryant
Contact us
Please share your comments on this manual or on any
Veryant product documentation with the email button at the top left