DB2 - Banco de dados - PREPARE - www.cadcobol.com.br
The PREPARE statement is used by application programs to dynamically prepare an SQL statement for execution. The PREPARE statement creates an executable SQL statement, called a prepared statement, from a character string form of the statement, called a statement string.
This statement can only be embedded in an application program. It is an executable statement that cannot be dynamically prepared.
For statements where authorization checking is performed at statement preparation time (DML), the privileges held by the authorization ID of the statement must include those required to execute the SQL statement specified by the PREPARE statement. The authorization ID of the statement might be affected by the DYNAMICRULES bind option.
For statements where authorization checking is performed at statement execution time (DDL, GRANT, and REVOKE statements), no authorization is required to use this statement; however, the authorization is checked when the prepared statement is executed.
For statements involving tables that are protected with a security policy, the rules associated with the security policy are always evaluated at statement execution time.
If the authorization ID of the statement holds EXPLAIN, SQLADM, or DBADM authority, the user may prepare any statement; however, the ability to execute the statement is re-checked at statement execution time.
A parameter marker is a question mark (?) or a colon followed by a name (:name) that is used where a host variable could be used if the statement string were a static SQL statement. For an explanation of how parameter markers are replaced by values, see OPEN and EXECUTE.
If the parameter marker is named, the name can include letters, numbers, and the symbols @, #, $, and _. The name is not folded to upper case.
Named parameter markers have the same syntax as host variables, but the two are not interchangeable. A host variable has a value and is used directly in a static SQL statement. A named parameter marker is a placeholder for a value in a dynamic SQL statement and the value is provided when the statement is executed.
There are two types of parameter markers:
CAST(? AS data-type)
UPDATE EMPLOYEE SET LASTNAME = TRANSLATE(CAST(? AS VARCHAR(12))) WHERE EMPNO = ?
Typed parameter markers can be used in dynamic SQL statements wherever a host variable is supported and the data type is based on the promise made in the CAST function.
Untyped parameter markers can be used in dynamic SQL statements as long as the data type of the parameter marker can be derived based on the context in the SQL statement (SQLSTATE 42610).
The following example results in an error since in the first context, c1 would resolve to a string data type, but in the second context, c1 would resolve to a numeric data type:
SELECT 'Hello' || c1, 5 + c1 FROM (VALUES(?)) AS T(c1)
However, the following statement is successful since the parameter marker associated with the derived column, c1, would resolve to a numeric data type for both contexts:
SELECT 7 + c1, 5 + c1 FROM (VALUES(?)) AS T(c1)
Example 1: Prepare and execute a non-select-statement in a COBOL program. Assume the statement is contained in a host variable HOLDER and that the program will place a statement string into the host variable based on some instructions from the user. The statement to be prepared does not have any parameter markers.
EXEC SQL PREPARE STMT_NAME FROM :HOLDER END-EXEC. EXEC SQL EXECUTE STMT_NAME END-EXEC.
Example 2: Prepare and execute a non-select-statement as in example 1, except code it for a C program. Also assume the statement to be prepared can contain any number of parameter markers.
EXEC SQL PREPARE STMT_NAME FROM :holder; EXEC SQL EXECUTE STMT_NAME USING DESCRIPTOR :insert_da;
Assume that the following statement is to be prepared:
INSERT INTO DEPT VALUES(?, ?, ?, ?)
Thecolumns in the DEPT table are defined as follows:
DEPT_NO CHAR(3) NOT NULL, -- department number DEPTNAME VARCHAR(29), -- department name MGRNO CHAR(6), -- manager number ADMRDEPT CHAR(3) -- admin department number
To insert department number G01 named COMPLAINTS, which has no manager and reports to department A00, the structure INSERT_DA should have the values in Table 1 before issuing the EXECUTE statement.
Note: