Informix SELECT
The SELECT statement contains input variables and output variables.
Input variables are passed to the database as part of the execution
and the substitution as described above for DELETE, INSERT, and
UPDATE. Output variables are used to return values based on the
result of the SELECT statement.
Example 1
Assume you enter the following statement:
|
1 |
SELECT emp_name, emp_salary<br> INTO :emp_name_var, :emp_salary_var<br> FROM employee WHERE emp_id = :emp_id_var; |
Here emp_id_var, emp_salary_var,
and emp_name_var are PowerScript variables defined
within the scope of the script that contains the SELECT statement,
emp_id_var is processed as described in the DELETE
example above.
Both emp_name_var and emp_salary_var
are output variables that will be used to return values from the
database. The data types of emp_name_var and emp_salary_var
should be the PowerScript data types that best match the Informix
data type. When the data types do not match perfectly, PowerBuilder converts
them.
How big should numeric output variables be?
For numeric data, the output variable must be large enough
to hold any value that may come from the database.
Assume the value for emp_id_var is 691 as
in the previous example. When the SELECT statement executes, the
database receives the following statement:
|
1 |
SELECT emp_name, emp_salary<br> FROM employee WHERE emp_id = 691; |
If no errors are returned on the execution, data locations
are internally bound for the result fields. The data returned into
these locations is converted if necessary and the appropriate PowerScript
variables are set to those values.
Example 2
This example assumes the default transaction object (SQLCA)
has been assigned valid values and a successful CONNECT has executed.
It also assumes the data type of the emp_id column in the
employee table is CHARACTER[10].
The user enters an employee ID into the line edit sle_Emp
and clicks the button Cb_Delete to delete the employee.
The script for the Clicked event in the CommandButton Cb_Delete
is:
|
1 |
// Make sure we have a value.<br>if sle_Emp.text <> "" then<br> <br>// Since we have a value, let's try to delete it.<br> DELETE FROM employee WHERE emp_id = :sle_Emp.text;<br> <br>// Test to see if the DELETE worked.<br> if SQLCA.sqlcode = 0 then<br> <br>// It seems to have worked, let user know.<br>MessageBox( "Delete",&<br>"The delete has been successfully processed!")<br> else <br> <br>// It didn't work.<br>MessageBox( "Error", &<br>"The delete failed. Employee ID is not valid.")<br> end if<br>else<br> <br>// No input value. Prompt user.<br>MessageBox( "Error", &<br>"An employee ID is required for "+"delete!" ) <br> end if |
Error checking
Although you should test the SQLCode after every SQL statement,
these examples show statements to test the SQLCode only to illustrate
a specific point.
Example 3
This example assumes the default transaction object (SQLCA)
has been assigned valid values and a successful CONNECT has executed.
The user wants to extract rows from the employee table and insert
them into the table named extract_employees.
The extraction occurs when the user clicks the button Cb_Extract.
The boolean variable YoungWorkers is set to TRUE or FALSE elsewhere
in the application.
The script for the Clicked event for the CommandButton Cb_Extract
is:
|
1 |
integer EmployeeAgeLowerLimit<br>integer EmployeeAgeUpperLimit<br> <br>// Do they have young workers?<br>if ( YoungWorkers = TRUE ) then<br> <br>// Yes - set the age limit in the YOUNG range.<br>// Assume no employee is under legal working age.<br> EmployeeAgeLowerLimit = 16<br> <br>// Pick an upper limit. <br> EmployeeAgeUpperLimit = 42<br>else <br> <br>// No - set the age limit in the OLDER range.<br> EmployeeAgeLowerLimit = 43<br> <br>// Pick an upper limit that includes all<br>// employees.<br> EmployeeAgeUpperLimit = 200<br>end if <br>INSERT INTO extract_employees(emp_id,emp_name)<br> SELECT emp_id, emp_name FROM employee<br> WHERE emp_age >= :EmployeeAgeLowerLimit <br> AND emp_age <= :EmployeeAgeUpperLimit; |