Contents IndexThe structure of procedures and triggers Using cursors in procedures and triggers

User's Guide
   Part III. Using SQL Anywhere
     Chapter 20. Using Procedures, Triggers, and Batches
      Returning results from procedures

Procedures 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".

Top of page


Returning a value using the RETURN statement

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() ;

Top of page


Returning results as procedure parameters

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:

Using the SET statement

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)
     BEGIN
         IF a > b THEN
             SET c = a;
         ELSE
             SET c = b;
         END IF ;
     END

Using single-row SELECT statements

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:

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;

Notes

Top of page


Returning result sets from procedures

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)
     BEGIN
         SELECT company_name,
             CAST( sum(    sales_order_items.quantity *
                             product.unit_price)
                             AS INTEGER ) AS value
         FROM customer
             INNER JOIN sales_order
             INNER JOIN sales_order_items
             INNER JOIN product
         GROUP BY company_name
         ORDER BY value desc;
     END

Company Value
Chadwicks 8076
Overland Army Navy 8064
Martins Landing 6888
Sterling & Co. 6804
Carmel Industries 6780
...  

Notes

Top of page


Returning multiple result sets from procedures

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) )
     BEGIN
         SELECT emp_lname, emp_fname
         FROM employee;
         SELECT lname, fname
         FROM customer;
         SELECT last_name, first_name
         FROM contact;
     END

Notes

Top of page


Returning variable result sets from procedures

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))
     BEGIN
         IF formal = 'y' THEN
             SELECT emp_lname, emp_fname
             FROM employee
         ELSE
             SELECT emp_fname
             FROM employee
         END IF
     END

The use of variable result sets in procedures is subject to some limitations, depending on the interface used by the client application.

Top of page


Contents IndexThe structure of procedures and triggers Using cursors in procedures and triggers