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
Creating this example using Interactive SQL
Connect to the sample database from Interactive
SQL with a user ID of DBA
and
a password of SQL
.
In the SQL Statements pane, type the above function code.
If 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:
No IN, OUT, or INOUT keywords are required, as all parameters are IN parameters.
The RETURNS clause is required to specify the data type being returned.
The RETURN statement is required to specify the value being returned.