Sybase Adaptive Server Enterprise Update
Database stored procedures that perform updates only and do
not return result sets are handled in much the same way as procedures
that return result sets. The only difference is that after the EXECUTE procedure_name statement
is executed, no result sets are pending and no CLOSE statement is
required.
Using the SQL Code property
If you know for sure that a particular procedure can never
return result sets, then the EXECUTE statement is all that is needed.
If there is a procedure that may or may not return a result set,
you can test the SQLCode property of the referenced transaction
object for +100 (the code for NOT FOUND) after the EXECUTE.
The following table shows all the possible values for SQLCode
after an EXECUTE:
|
Return code |
Means |
|---|---|
|
0 |
The EXECUTE was successful and at least This code is returned even if the result set is empty. |
|
+100 |
Fetched row not found. |
|
-1 |
The EXECUTE was not successful and no |
Example 1
Assume the default transaction object (SQLCA) has been assigned
valid values and a successful CONNECT has been executed. Also assume
the description of the Adaptive Server Enterprise procedure good_employee
is:
|
1 |
// Adaptive Server Enterprise good_employee<br>// stored procedure:<br>CREATE PROCEDURE good_employee AS<br> UPDATE employee <br> SET emp_salary=emp_salary * 1.1<br> WHERE emp_status = 'EXC' |
This example illustrates how to execute a stored procedure
that does not return any result sets:
|
1 |
// Declare the procedure.<br>DECLARE good_emp_proc PROCEDURE<br>FOR good_employee; |
|
1 |
// Execute it.<br>EXECUTE good_emp_proc; |
|
1 |
// Test return code. Allow for +100 since you do<br>// not expect result sets.<br>if SQLCA.sqlcode = -1 then |
|
1 |
// Issue error message since it failed.<br> MessageBox("Stored Procedure Error!", &<br> SQLCA.sqlerrtext)<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 2
Assume the default transaction object (SQLCA) has been assigned
valid values and a successful CONNECT has been executed. Also assume
the description of the Adaptive Server Enterprise procedure get_employee
is:
|
1 |
// Adaptive Server Enterprise get_employee<br>// stored procedure:<br> CREATE PROCEDURE get_employee @emp_id_parm<br> int AS SELECT emp_name FROM employee<br> WHERE emp_id = @emp_id_parm |
This example illustrates how to pass parameters to a database
stored procedure. Emp_id_var has
been set elsewhere to 691:
|
1 |
// Declare the procedure.<br>DECLARE get_emp_proc PROCEDURE FOR<br> get_employee @emp_id_parm = :emp_id_var; |
|
1 |
// Declare a destination variable for emp_name.<br>string    emp_name_var |
|
1 |
// Execute the stored procedure using the<br>// current value for emp_id_var.<br>EXECUTE get_emp_proc; |
|
1 |
// Test return code to see if it worked.<br>if SQLCA.sqlcode = 0 then |
|
1 |
// Since we got a row, fetch it and display it.<br> FETCH get_emp_proc INTO :emp_name_var; |
|
1 |
// Display the employee name.<br> MessageBox("Got my employee!",emp_name_var) |
|
1 |
// You are all done, so close the procedure.<br> CLOSE Get_emp_proc; |
|
1 |
end if |