Sybase Adaptive Server IQ 12.4.2 Administration And Performance Manual page 274

Table of Contents

Advertisement

Using cursors in procedures
Notes
254
-- 7. Close the cursor
CLOSE ThisCompany;
END
The
procedure has the following notable features:
TopCustomerValue
The "error not found" exception is declared. This exception is used later in
the procedure to signal when a loop over the results of a query has
completed.
For more information about exceptions, see "Errors and warnings in
procedures".
Two local variables
results from each row of the query.
The cursor
ThisCompany
list of company names and the total value of the orders placed by that
company.
The value of
TopValue
The
cursor is opened.
ThisCompany
The LOOP statement loops over each row of the query, placing each
company name in turn into the variables
is greater than the current top value,
ThisValue
are reset to
ThisName
The cursor is closed at the end of the procedure.
The LOOP construct in the
exiting after the last row is processed. You can rewrite this procedure in a more
compact form using a FOR loop. The FOR statement combines several aspects
of the above procedure into a single statement.
CREATE PROCEDURE TopCustomerValue2(
OUT TopCompany CHAR(36),
OUT TopValue INT )
BEGIN
-- Initialize the TopValue variable
SET TopValue = 0;
-- Do the For Loop
CompanyLoop:
FOR CompanyFor AS ThisCompany
CURSOR FOR
SELECT company_name AS ThisName ,
CAST( sum( sales_order_items.quantity *
and
ThisName
ThisValue
is declared. The SELECT statement produces a
is set to an initial value of 0, for later use in the loop.
ThisName
and
.
ThisValue
procedure is a standard form,
TopCompanyValue
product.unit_price ) AS INTEGER )
are declared to hold the
and
. If
ThisValue
and
TopCompany
TopValue

Advertisement

Table of Contents
loading

Table of Contents