HP (Hewlett-Packard) 544530-001 Computer Accessories User Manual


 
SQL>
For more information, see the “/ Command” (page 82), “RUN Command” (page 100), or “REPEAT
Command” (page 98).
Preparing and Executing SQL Statements
You can prepare, or compile, an SQL statement by using the PREPARE statement and later
execute the prepared SQL statement by using the EXECUTE statement.
“Preparing an SQL Statement” (page 60)
“Setting Parameters” (page 61)
“Displaying the Parameters of the Session” (page 61)
“Resetting the Parameters” (page 61)
“Executing a Prepared SQL Statement” (page 62)
Preparing an SQL Statement
Use the PREPARE statement to compile an SQL statement for later execution with the EXECUTE
statement. You can also use the PREPARE statement to check the syntax of an SQL statement
without executing the statement. For example, this PREPARE statement compiles a SELECT
statement named empsal and detects a syntax error:
SQL>prepare empsal from
+>select salary from employee
+>where jobcode = 100;
*** ERROR[4082] Table, view or stored procedure NEO.INVENT.EMPLOYEE does not exist or is inaccessible.
*** ERROR[8822] The statement was not prepared.
SQL>
You can then correct the syntax of the SQL statement and prepare it again:
SQL>prepare empsal from
+>select salary from persnl.employee
+>where jobcode = 100;
--- SQL command prepared.
To specify a parameter to be supplied later, either in a SET PARAM statement or in the USING
clause of an EXECUTE statement, use one of these types of parameters in the SQL statement:
Named parameter, which is represented by ?param-name
Unnamed parameter, which is represented by a question mark (?) character
For example, this prepared SELECT statement specifies unnamed parameters for salary and job
code:
SQL>prepare findemp from
+>select * from persnl.employee
+>where salary > ? and jobcode = ?;
--- SQL command prepared.
This PREPARE statement prepares another SELECT statement named empcom, which has one
named parameter, ?dn, for the department number, which appears twice in the statement:
SQL>prepare empcom from
+>select first_name, last_name, deptnum
+>from persnl.employee
+>where deptnum <> ?dn and salary <=
+>(select avg(salary)
+>from persnl.employee
+>where deptnum = ?dn);
--- SQL command prepared.
For the syntax of the PREPARE statement, see the Neoview SQL Reference Manual.
60 Running Commands Interactively in the Neoview Script Interface