How a database language implements binding affects runtime efficiency and flexibility. Binding at compile time, called static or early binding, increases efficiency because the definitions of database objects are looked up then, not at run time. On the other hand, binding at run time, called dynamic or late binding, increases flexibility because the definitions of database objects can remain unknown until then.
Designed primarily for high-speed transaction processing, PL/SQL increases efficiency by bundling SQL statements and avoiding runtime compilation. Unlike SQL, which is compiled and executed statement- by-statement at run time (late binding), PL/SQL is processed into machine-readable p-code at compile time (early binding). At run time, the PL/SQL engine simply executes the p-code.
CREATE PROCEDURE create_table AS BEGIN CREATE TABLE dept (deptno NUMBER(2), ...); -- illegal ... END;
In the next example, the compiler cannot bind the table reference in the DROP TABLE statement because the table name is unknown until the procedure is executed:
CREATE PROCEDURE drop_table (table_name IN VARCHAR2) AS BEGIN DROP TABLE table_name; -- illegal ... END;
CREATE PROCEDURE drop_table (table_name IN VARCHAR2) AS cid INTEGER; BEGIN /* Open new cursor and return cursor ID. */ cid := DBMS_SQL.OPEN_CURSOR; /* Parse and immediately execute dynamic SQL statement built by concatenating table name to DROP TABLE command. */ DBMS_SQL.PARSE(cid, 'DROP TABLE ' || table_name, dbms_sql.v7); /* Close cursor. */ DBMS_SQL.CLOSE_CURSOR(cid); EXCEPTION /* If an exception is raised, close cursor before exiting. */ WHEN OTHERS THEN DBMS_SQL.CLOSE_CURSOR(cid); RAISE; -- reraise the exception END drop_table;
For more information about package DBMS_SQL, see Oracle7 Server Application Developer's Guide.