CREATE PROCEDURE statement

Description

Creates a new procedure in the database.

Syntax

CREATE PROCEDUREowner.]procedure-name ( [ parameter, …] ) { 
[ RESULTresult-column, …)  | NO RESULT SET ]  
[ ON EXCEPTION RESUME ] compound statement 
| AT location-string | | [  DYNAMIC RESULT SETS integer-expression ] 
[ EXTERNAL NAME java-call  LANGUAGE JAVA ] 
}

Parameters

parameter:

parameter_mode parameter-name data-typeDEFAULT expression ] | SQLCODE | SQLSTATE

parameter_mode:

IN | OUT | INOUT

result-column:

column-name data-type

library-call:

'function-name@library.dll; …'

java-call:

'[ package-name.]class-name.method-name method-signature'

method-signature:

( [ field-descriptor, … ] ) return-descriptor

field-descriptor | return-descriptor:

Z | B | S | I | J | F | D | C | V |  [descriptor | Lclass-name;

Examples

Example 1

This procedure uses a case statement to classify the results of a query.

CREATE PROCEDURE ProductType (IN product_id INT, OUT type CHAR(10))
BEGIN
  DECLARE prod_name CHAR(20) ;
  SELECT name INTO prod_name FROM "DBA"."Products"
  WHERE ID = product_id;
  CASE prod_name
  WHEN 'Tee Shirt' THEN
    SET type = 'Shirt'			
  WHEN 'Sweatshirt' THEN
    SET type = 'Shirt'
  WHEN 'Baseball Cap' THEN
    SET type = 'Hat'
  WHEN 'Visor' THEN
    SET type = 'Hat'
  WHEN 'Shorts' THEN
    SET type = 'Shorts'
  ELSE
    SET type = 'UNKNOWN'
  END CASE ;
END

Example 2

This procedure uses a cursor and loops over the rows of the cursor to return a single value.

CREATE PROCEDURE TopCustomer (OUT TopCompany CHAR(35), OUT TopValue INT)
BEGIN
  DECLARE err_notfound EXCEPTION
 	FOR SQLSTATE '02000' ;
  DECLARE curThisCust CURSOR FOR
  SELECT CompanyName, CAST( 	  sum(SalesOrderItems.Quantity *
  Products.UnitPrice) AS INTEGER) VALUE
  FROM Customers
  LEFT OUTER JOIN SalesOrdes
  LEFT OUTER JOIN SalesorderItems
  LEFT OUTER JOIN Products
  GROUP BY CompanyName ;


  DECLARE ThisValue INT ;
  DECLARE ThisCompany CHAR(35) ;
  SET TopValue = 0 ;
  OPEN curThisCust ;
  CustomerLoop:
  LOOP
    FETCH NEXT curThisCust
    INTO ThisCompany, ThisValue ;
    IF SQLSTATE = err_notfound THEN
      LEAVE CustomerLoop ;
    END IF ;
    IF ThisValue > TopValue THEN
      SET TopValue = ThisValue ;
      SET TopCompany = ThisCompany ;
      END IF ;
  END LOOP CustomerLoop ;
  CLOSE curThisCust ;
END

Usage

CREATE PROCEDURE creates a procedure in the database. Users with DBA authority can create procedures for other users by specifying an owner. A procedure is invoked with a CALL statement.

The body of a procedure consists of a compound statement. For information on compound statements, see BEGIN … END statement.

NoteThere are two ways to create stored procedures: SQL92 and T-SQL. BEGIN TRANSACTION, for example, is T-SQL specific when using CREATE PROCEDURE syntax. Do not mix syntax when creating stored procedures.

CREATE PROCEDURE Parameter names must conform to the rules for other database identifiers such as column names. They must be a valid SQL data type and must be prefixed by IN, OUT or INOUT. See Chapter 3, “SQL Data Types” in Reference: Building Blocks, Tables, and Procedures. The keywords have the following meanings:

When procedures are executed using CALL, not all parameters need to be specified. If a default value is provided in the CREATE PROCEDURE statement, missing parameters are assigned the default values. If an argument is not provided in the CALL statement, and no default is set, an error is given.

SQLSTATE and SQLCODE are special parameters that output the SQLSTATE or SQLCODE value when the procedure ends (they are OUT parameters). Whether or not a SQLSTATE and SQLCODE parameter is specified, the SQLSTATE and SQLCODE special values can always be checked immediately after a procedure call to test the return status of the procedure.

The SQLSTATE and SQLCODE special values are modified by the next SQL statement. Providing SQLSTATE or SQLCODE as procedure arguments allows the return code to be stored in a variable.

RESULT The RESULT clause declares the number and type of columns in the result set. The parenthesized list following the RESULT keyword defines the result column names and types. This information is returned by the Embedded SQL DESCRIBE or by ODBC SQLDescribeCol when a CALL statement is being described. Allowed data types are listed in Chapter 3, “SQL Data Types” in Reference: Building Blocks, Tables, and Procedures.

For more information on returning result sets from procedures, see Chapter 1, “Using Procedures and Batches” in the System Administration Guide: Volume 2.

Some procedures can return more than one result set, with different numbers of columns, depending on how they are executed. For example, the following procedure returns two columns under some circumstances, and one in others.

CREATE PROCEDURE names( IN formal char(1))
BEGIN
  IF formal = 'n' THEN
    SELECT GivenName 
    FROM Employees
  ELSE
    SELECT Surname,GivenName 
    FROM Employees
  END IF
END

Procedures with variable result sets must be written without a RESULT clause, or in Transact-SQL. Their use is subject to the following limitations:

If your procedure returns only one result set, use a RESULT clause. The presence of this clause prevents ODBC and Open Client applications from describing the result set again after a cursor is open.

To handle multiple result sets, ODBC must describe the currently executing cursor, not the procedure’s defined result set. Therefore, ODBC does not always describe column names as defined in the RESULT clause of the procedure definition. To avoid this problem, use column aliases in the SELECT statement that generates the result set.

NO RESULT SET This clause declares that this procedure returns no result set. This is useful when an external environment needs to know that a procedure does not return a result set.

ON EXCEPTION RESUME This clause enables Transact-SQL -like error handling to be used within a Watcom-SQL syntax procedure.

If you use ON EXCEPTION RESUME, the procedure takes an action that depends on the setting of the ON_TSQL_ERROR option. If ON_TSQL_ERROR is set to CONDITIONAL (which is the default) the execution continues if the next statement handles the error; otherwise, it exits.

Error-handling statements include the following:

Do not use explicit error-handling code with an ON EXCEPTION RESUME clause.

For more information, see “ON_TSQL_ERROR option [TSQL]”.

AT location-string Create a proxy stored procedure on the current database for a remote procedure specified by location-string. The AT clause supports the semicolon (;) as a field delimiter in location-string. If no semicolon is present, a period is the field delimiter. This allows file names and extensions to be used in the database and owner fields.

Remote procedures can return only up to 254 characters in output variables.

For information on remote servers, see CREATE SERVER statement. For information on using remote procedures, see the section “Using remote procedure calls (RPCs)” in Chapter 4, “Accessing Remote Data” in the System Administration Guide: Volume 2.

DYNAMIC RESULT SETS This clause is for use with procedures that are wrappers around Java methods. If the DYNAMIC RESULT SETS clause is not provided, it is assumed that the method returns no result set.

EXTERNAL NAME LANGUAGE JAVA A procedure that uses EXTERNAL NAME with a LANGUAGE JAVA clause is a wrapper around a Java method.

If the number of parameters is less than the number indicated in the method-signature, the difference must equal the number specified in DYNAMIC RESULT SETS, and each parameter in the method signature in excess of those in the procedure parameter list must have a method signature of [Ljava/sql/ResultSet;.

Java method signatures A Java method signature is a compact character representation of the types of the parameters and the type of the return value.

The meanings of field-descriptor and return-descriptor are listed in Table 1-8.

Table 1-8: Java method signatures

Field type

Java data type

B

byte

C

char

D

double

F

float

I

int

J

long

Lclass-name;

an instance of the class-name class. The class name must be fully qualified, and any dot in the name must be replaced by a backslash. For example, java/lang/String

S

short

V

void

Z

boolean

[

use one for each dimension of an array

For example:

double some_method(
  boolean a,
  int b,
  java.math.BigDecimal c,
  byte [][] d,
  java.sql.ResultSet[] d ) {
}

would have the following signature:

'(ZILjava/math/BigDecimal;[[B[Ljava/sql/ResultSet;)D'

NoteAs procedures are dropped and created, databases created prior to Sybase IQ 12.6 may eventually reach the maximum proc_id limit of 32767, causing CREATE PROCEDURE to return an “Item already exists” error in Sybase IQ 12.6. For workaround, see “Insufficient procedure identifiers,” in Chapter 14, “Troubleshooting Hints,” in the System Administration Guide: Volume 1.


Side effects

Automatic commit.

Standards

Permissions

Must have RESOURCE authority. For external procedures, must have DBA authority.

See also

BEGIN … END statement

CALL statement

DROP statement

EXECUTE IMMEDIATE statement [ESQL] [SP]

GRANT statement

“Copy Definition utility (defncopy)” in Chapter 3, “Database Administration Utilities” of the Utility Guide