User's Guide
Part III. Using SQL Anywhere
Chapter 20. Using Procedures, Triggers, and BatchesProcedures can return results that are a single row of data, or multiple rows. In the former case, results can be passed back as arguments to the procedure, in the latter case, results are passed back as result sets. Procedures can also return a single value given in the RETURN statement.
For simple examples of how to return results from procedures, see "Introduction to procedures". For information about returning a single row, see "Returning results as procedure parameters". For information about returning multiple rows, see "Returning result sets from procedures".
A single value can be returned to the calling environment using the RETURN statement, which causes an immediate exit from the procedure. The RETURN statement takes the form:
RETURN expression
The value of the supplied expression is returned to the calling environment. To save the return value in a variable, an extension of the CALL statement is used:
CREATE VARIABLE returnval INTEGER ;returnval = CALL myproc() ;
Procedures can return results to the calling environment in the parameters to the procedure.
Within a procedure, parameters and variables can be assigned values in one of the following ways:
The following somewhat artificial procedure returns a value in an OUT parameter that is assigned using a SET statement:
CREATE PROCEDURE greater ( IN a INT,IN b INT,OUT c INT)BEGINIF a > b THENSET c = a;ELSESET c = b;END IF ;END
Single-row queries retrieve at most one row from the database. This type of query is achieved by a SELECT statement with an INTO clause. The INTO clause follows the select list and precedes the FROM clause. It contains a list of variables to receive the value for each select list item. There must be the same number of variables as there are select list items.
When a SELECT statement is executed, the database engine retrieves the results of the select statement and places the results in the variables. If the query results contain more than one row, the database engine returns an error. For queries returning more than one row, cursors must be used. For information about returning more than one row from a procedure, see "Returning result sets from procedures".
If the query results in no rows being selected, a row not found warning is returned.
The following procedure returns the results of a single-row SELECT statement in the procedure parameters.
To return the number of orders placed by a given customer:
CREATE PROCEDURE OrderCount (IN customer_ID INT,OUT Orders INT)BEGINSELECT COUNT("dba".sales_order.id)INTO OrdersFROM "dba".customerKEY LEFT OUTER JOIN "dba".sales_orderWHERE "dba".customer.id = customer_ID;END
This procedure can be tested in ISQL using the following statements, which show the number of orders placed by the customer with ID 102:
CREATE VARIABLE orders INT;CALL OrderCount ( 102, orders );SELECT orders;
If a procedure returns more than one row of results to the calling environment, it does so using result sets.
The following procedure returns a list of customers who have placed orders, together with the total value of the orders placed. The procedure does not list customers who have not placed orders.
CREATE PROCEDURE ListCustomerValue ()RESULT ("Company" CHAR(36), "Value" INT)BEGINSELECT company_name,CAST( sum( sales_order_items.quantity *product.unit_price)AS INTEGER ) AS valueFROM customerINNER JOIN sales_orderINNER JOIN sales_order_itemsINNER JOIN productGROUP BY company_nameORDER BY value desc;END
CALL ListCustomerValue ()
| Company | Value |
| Chadwicks | 8076 |
| Overland Army Navy | 8064 |
| Martins Landing | 6888 |
| Sterling & Co. | 6804 |
| Carmel Industries | 6780 |
| ... |
A procedure can return more than one result set to the calling environment. If a RESULT clause is employed, the result sets must be compatible: they must have the same number of items in the SELECT lists, and the data types must all be of types that can be automatically converted to the data types listed in the RESULT list.
The following procedure lists the names of all employees, customers, and contacts listed in the database:
CREATE PROCEDURE ListPeople()RESULT ( lname CHAR(36), fname CHAR(36) )BEGINSELECT emp_lname, emp_fnameFROM employee;SELECT lname, fnameFROM customer;SELECT last_name, first_nameFROM contact;END
The RESULT clause is optional in procedures. Omitting the result clause allows you to write procedures that return different result sets, with different numbers or types of columns, depending on how they are executed.
If you are not using this feature of variable result sets, it is recommended that you employ a RESULT clause, for performance reasons.
For example, the following procedure returns two columns if the input variable is Y, but only one column otherwise:
CREATE PROCEDURE names( IN formal char(1))BEGINIF formal = 'y' THENSELECT emp_lname, emp_fnameFROM employeeELSESELECT emp_fnameFROM employeeEND IFEND
The use of variable result sets in procedures is subject to some limitations, depending on the interface used by the client application.
For information about the DESCRIBE statement, see "DESCRIBE statement".