Creating user-defined functions

You use the CREATE FUNCTION statement to create user-defined functions. However, you must have RESOURCE authority.

The following simple example creates a function that concatenates two strings, together with a space, to form a full name from a first name and a last name.

CREATE FUNCTION fullname (firstname CHAR(30),
lastname CHAR(30))
RETURNS CHAR(61)
BEGIN
   DECLARE name CHAR(61);
   SET name = firstname || ' ' || lastname;
   RETURN ( name );
END

StepsCreating this example using Interactive SQL

  1. Connect to the sample database from Interactive SQL with a user ID of DBA and a password of SQL.

  2. In the SQL Statements pane, type the above function code.

NoteIf you are using a tool other than Interactive SQL or Sybase Central, you may need to change the command delimiter away from the semicolon before entering the CREATE FUNCTION statement.

For a complete description of the CREATE FUNCTION syntax, including performance considerations and differences between Adaptive Server Anywhere and IQ, see Chapter 6, “SQL Statements,” in the Sybase IQ Reference Manual.

The CREATE FUNCTION syntax differs slightly from that of the CREATE PROCEDURE statement. The following are distinctive differences: