INFORMIX
Informix Guide to SQL: Syntax
Chapter 1: SQL Statements
Home Contents Index Master Index New Book

SQL Statements

CREATE CAST

Use the CREATE CAST statement to register a cast that converts data from one data type to another.

Syntax

(1 of 2)

Element Purpose Restrictions Syntax

source data type

The data type to be converted

The type must exist in the database at the time the cast is registered. Either the source data type or the target data type, but not both, can be a built-in type. Neither type can be a distinct type of the other. The type cannot be a collection data type.

Data Type, p. 1-859

target data type

The data type that results from the conversion

The type must exist in the database at the time the cast is registered. Either the source data type or the target data type, but not both, can be a built-in type. Neither type can be a distinct type of the other. The type cannot be a collection data type.

Data Type, p. 1-859

function name

The name of the function that you register to implement the cast

See "WITH Clause".

Function Name,
p.
1-963

Usage

A cast is a mechanism that the database server uses to convert one data type to another. The database server uses casts to perform the following tasks:

To create a cast, you must have the necessary privileges on both the source data type and the target data type. All users have permission to use the built-in data types. However, to create a cast to or from an opaque type, distinct type, or named row type requires the Usage privilege on that type.

The CREATE CAST statement registers a cast in the syscasts system catalog table. For more information on syscasts, see the chapter on system catalog tables in the Informix Guide to SQL: Syntax.

Source and Target Data Types

The CREATE CAST statement defines a cast that converts a source data type to a target data type. Both the source data type and target data type must exist in the database when you execute the CREATE CAST statement to register the cast. The source data type and the target data type have the following restrictions:

Explicit and Implicit Casts

To process queries with multiple data types often requires casts that convert data from one data type to another. You can use the CREATE CAST statement to create the following kinds of casts:

Explicit Casts

An explicit cast is a cast that you must specifically invoke, with either the CAST AS keywords or with the cast operator (::). The database server does not automatically invoke an explicit cast to resolve data type conversions. The EXPLICIT keyword is optional; by default, the CREATE CAST statement creates an explicit cast.

The following CREATE CAST statement defines an explicit cast from the rate_of_return opaque data type to the percent distinct data type:

The following SELECT statement explicitly invokes this explicit cast in its WHERE clause to compare the bond_rate column (of type rate_of_return) to the yyy column (of type percent):

Implicit Casts

The database server invokes system-defined casts to convert from one built-in data type to another built-in type that is not directly substitutable. For example, the database server performs conversion of a character type such as CHAR to a numeric type such as INTEGER through a system-defined cast.

An implicit cast is a cast that the database server can invoke automatically when it encounters data types that cannot be compared with system-defined casts. This type of cast enables the database server to handle automatically conversions between other data types.

To define an implicit cast, specify the IMPLICIT keyword in the CREATE CAST statement. For example, the following CREATE CAST statement specifies that the database server should automatically use the prcnt_to_char() function when it needs to convert from the CHAR data type to a distinct data type, percent:

This cast only provides the database server with the ability to automatically convert from the CHAR data type to percent. For the database server to convert from percent to CHAR, you need to define another implicit cast, as follows:

The database server would automatically invoke the char_to_prcnt() function to evaluate the WHERE clause of the following SELECT statement:

Users can also invoke implicit casts explicitly. For more information on how to explicitly invoke a casting function, see "Explicit Casts".

When a system-defined cast does not exist for conversion between data types, you can create user-defined casts to make the necessary conversion. Universal Server supports the following types of casts:

WITH Clause

The WITH clause of the CREATE CAST statement specifies the name of the user-defined function to invoke to perform the cast. This function is called the casting function. You must specify a function name unless the source data type and the target data type have identical representations. Two data types have identical representations when the following conditions are met:

The casting function must be registered in the same database as the cast at the time the cast is invoked, but need not exist when the cast is created. The CREATE CAST statement does not check permissions on the specified function name, or even verify that the casting function exists. Each time a user invokes the cast explicitly or implicitly, the database server verifies that the user has Execute privilege on the casting function.

References

See the CREATE FUNCTION statement in this manual for information about registering the functions that are used to implement casts. See the CREATE DISTINCT TYPE, CREATE OPAQUE TYPE, and CREATE ROW TYPE statements in this manual for information about creating new data types. See the DROP CAST statement in this manual for information about removing a cast from a database.

See the Data Types segment in this manual and Chapter 2 in the Informix Guide to SQL: Syntax for more information about data types, casting, and conversion.

In the Informix Guide to SQL: Tutorial, see Chapter 13 for examples that show how to create and use casts.

CREATE DATABASE

Use the CREATE DATABASE statement to create a new database.

Syntax

Element Purpose Restrictions Syntax

dbspace

The name of the dbspace where you want to store the data for this database; default is the root dbspace

The dbspace must already exist.

Identifier, p.1-966

pathname

The full pathname, including the file name, for the log file

You cannot specify an existing file.

The pathname and filename must conform to the conventions of your operating system.

Usage

The database that you create becomes the current database.

The database name that you use must be unique within the database server environment in which you are working. The database server creates the system catalog tables that contain the data dictionary, which describes the structure of the database in the dbspace. If you do not specify the dbspace, The database server creates the system catalog tables in the root dbspace.

When you create a database, you alone have access to it. The database remains inaccessible to other users until you, as DBA, grant database privileges. For information on granting database privileges, see the GRANT statement on page 1-461.

The following statement creates the vehicles database in the root dbspace:

The following statement creates the vehicles database in the research dbspace:

ESQL
In SQL APIs, the CREATE DATABASE statement cannot appear in a multistatement PREPARE operation.

ANSI-Compliant Databases

ANSI
You have the option of creating an ANSI-compliant database. ANSI-compliant databases differ from databases that are not ANSI compliant in the following ways:

Other slight differences exist between databases that are ANSI compliant and those that are not. These differences are noted as appropriate with the related SQL statement.

Logging on INFORMIX-Universal Server

In the event of a failure, INFORMIX-Universal Server uses the log to re-create all committed transactions in your database.

If you do not specify the WITH LOG clause, you cannot use transactions or the statements that are associated with databases that have logging (BEGIN WORK, COMMIT WORK, ROLLBACK WORK, SET LOG, and SET ISOLATION).

Designating Buffered Logging

The following example creates a database that uses a buffered log:

If you use a buffered log, you marginally enhance the performance of logging at the risk of not being able to re-create the last few transactions after a failure. (See the discussion of buffered logging in Chapter 9 of the Informix Guide to SQL: Tutorial.)

ANSI
An ANSI-compliant database does not use buffered logging.

Designating an ANSI-Compliant INFORMIX-Universal Server Database

The following example creates an ANSI-compliant database:

Creating an ANSI-compliant database does not mean that you receive ANSI warnings when you run the database. You must use the -ansi flag or the DBANSIWARN environment variable to receive warnings.

For additional information about -ansi and DBANSIWARN, see Chapter 3 in the Informix Guide to SQL: Syntax.

References

See the CLOSE DATABASE, CONNECT TO, DATABASE, DROP DATABASE, and START DATABASE statements in this manual.

In the Informix Guide to SQL: Tutorial, see the discussion of creating a database in Chapter 9.

CREATE DISTINCT TYPE

Use the CREATE DISTINCT TYPE statement to create a new distinct type. A distinct type is a data type based on a built-in type or an existing opaque type, a named row type, or another distinct type. Distinct types are strongly typed. Although the distinct type has the same physical representation as data of its source type, the two types cannot be compared without an explicit cast from one type to the other.

Syntax

Element Purpose Restrictions Syntax

distinct type

The name of the new data type

In an ANSI-compliant database, the combination of the owner and data type must be unique within the database. In a database that is not ANSI compliant, the name of the data type must be unique within the database.

Data Type, p. 1-859

source type

The name of an existing data type on which the new type is based

The type must be either a built-in type or a type created with the CREATE DISTINCT TYPE, CREATE OPAQUE TYPE, or CREATE ROW TYPE statement.

Data Type, p. 1-859

Usage

To create a distinct type in a database, you must have the Resource privilege. Any user with the Resource privilege can create a distinct type from one of the built-in data types, which are owned by user informix.

Important: You cannot create a distinct type on the SERIAL or SERIAL8 data type.
To create a distinct type from an opaque type, a named row type, or another distinct type, you must be the owner of the type or have the Usage privilege on the type.

Once a distinct type is defined, only the type owner and the DBA can use it. The owner of the type can grant other users the Usage privilege on the type.

A distinct type has the same storage structure as its source type.The following statement creates the distinct type birthday, based on the built-in data type, DATE:

INFORMIX-Universal Server uses the same storage method for the distinct type as it does for the source type of the distinct type. However, a distinct type and its source type cannot be compared in an operation unless one type is explicitly cast to the other type.

Support Functions and Casts

When you create a distinct type, Universal Server automatically defines two explicit casts:

Because the two types have the same representation (the same length and alignment), no support functions are required to implement the casts.

You can create an implicit cast between a distinct type and its source type. However, to create an implicit cast, you must first drop the default explicit cast between the distinct type and its source type.

All support functions and casts that are defined on the source type can be used on the distinct type. However, casts and functions that are defined on the distinct type are not available to the source type.

Manipulating Distinct Types

When you manipulate data of the distinct type and its source type, you must explicitly cast one type to the other. This means that to insert or update a column of one type with values of the other type, you must explicitly cast the data to be inserted or updated. In addition, you cannot use a relational operator to add, subtract, multiply, divide, compare, or otherwise manipulate two values, one of the source type and one of the distinct type.

For example, suppose you create a distinct type, dist_type, that is based on the NUMERIC data type. You then create a table with two columns, one of type dist_type and one of type NUMERIC.

To directly compare the distinct type and its source type or assign a value of the source type to a column of the distinct type, you must cast one type to the other, as the following examples show:

References

For information and examples that show how to use and cast distinct types, see Chapter 13 of the Informix Guide to SQL: Tutorial.

See the CREATE OPAQUE TYPE and CREATE ROW TYPE statements in this manual for information about how to create opaque types and row types.

See the CREATE FUNCTION statement in this manual for information about registering support functions for a type. See the CREATE CAST statement in this manual for information about registering these functions as casts.

For information about how to remove opaque types and row types from a database, see the DROP TYPE and DROP ROW TYPE statements in this manual.

For information about how to create a table that references a data type, see the CREATE TABLE statement in this manual.

For information about built-in data types, user-defined types, and named row types, see the Data Types segment in this manual.

CREATE FUNCTION

Use the CREATE FUNCTION statement to register an external function or to write and register an SPL function.

Syntax

Element Purpose Restrictions Syntax

pathname

The pathname to a file in which compile-time warnings are stored

The specified pathname must exist on the computer where the database resides.

The pathname and filename must conform to the conventions of your operating system.

Usage

A function is a user-defined routine that can accept arguments and returns one or more values. INFORMIX-Universal Server supports functions written in the following languages:

    An SPL function can return one or more values.

    An external function can return only one value.

The entire length of a CREATE FUNCTION statement must be less than 64 kilobytes. This length is the literal length of the statement, including blank space and tabs.

Routines, Functions, and Procedures

The generic term routine includes both procedures and functions. A procedure is a routine that can accept arguments but does not return any values. A function is routine that can accept arguments and returns one or more values. INFORMIX-Universal Server treats any routine that includes a Return clause as a function.

Legacy Procedures
In earlier Informix products, the term stored procedure was used for both SPL procedures and SPL functions. However, the database server distinguishes between procedures and functions, even when they are written in SPL. When you use CREATE FUNCTION to write an SPL routine, you create an SPL function.

SPL

SPL Functions

SPL functions are routines written in Stored Procedure Language (SPL) that return one or more values.

Use one CREATE FUNCTION statement, with SQL and SPL statements embedded between CREATE FUNCTION and END FUNCTION, to write and register an SPL function. Unlike external functions, you do not need to write the function and register it in separate steps.

SPL functions are parsed, optimized (as far as possible), and stored in the system catalog tables in executable format. The body of an SPL function is stored in the sysprocbody system catalog table. Other information about the function is stored in other system catalog tables, including sysprocedures, sysprocplan, and sysprocauth. For more information about these system catalog tables, see Chapter 1, "System Catalog," in the Informix Guide to SQL: Syntax.

You must use the END FUNCTION keywords with an SPL function.

Place a semicolon after the Return clause or the Modifier clause, whichever comes last. Place another semicolon at the end of the entire statement, after the END FUNCTION, DOCUMENT, or WITH LISTING IN clause.

Examples
The following example creates a SPL function:

For more information on writing SPL functions, see Chapter 14, "Creating and Using SPL Routines," in the Informix Guide to SQL: Tutorial.

EXT

External Functions

External functions are functions you write in an external language that INFORMIX-Universal Server supports. For this release, INFORMIX-Universal Server supports external functions written in C. To create external functions, follow these steps:

    1. Write the function in an external language, such as C, that INFORMIX-Universal Server supports.

    2. Compile the function and store the compiled code in a shared library.

    3. Register the function in the database server with the CREATE FUNCTION statement.

When INFORMIX-Universal Server executes an external function, the database server invokes the external object code.

The database server does not store the body of an external function directly in the database, as it does for SPL functions. Instead, the database server stores only a pathname to the compiled version of the function. You specify this pathname in the External Routine Reference clause.

The database server does store information about an external function in several system catalog tables, including sysprocbody and sysprocauth. For more information on these system catalog tables, see Chapter 1, "System Catalog," in the Informix Guide to SQL: Syntax.

With external functions, the END FUNCTION keywords are optional.

Example
The following example registers an external C function named equal() in the database. This function takes two arguments of the type basetype1 and returns a single value of type BOOLEAN. The external routine reference name specifies the path to the C shared library where the function object code is actually stored. This library contains a function basetype1_equal(), which is invoked during execution of the equal() function.

DBA Option

The level of privilege necessary to execute a routine depends on whether the routine is created with the DBA keyword.

If you create a function with the DBA option, it is known as a DBA-privileged function. You need the DBA privilege to create or execute a DBA-privileged function.

If you do not use the DBA option, the function is known as an owner-privileged function. If the function is owner privileged, and if the database is ANSI compliant, anyone can execute the function.

If you create an owner-privileged routine in a database that is not ANSI compliant, the NODEFDAC environment variable prevents privileges on that routine from being granted to PUBLIC. See the Informix Guide to SQL: Syntax for further information on the NODEFDAC environment variable.

Function Name

Because INFORMIX-Universal Server offers routine overloading, you can define more than one function with the same name, but different parameter lists. You might want to overload functions if you are defining a type hierarchy or a system of distinct types or casts. When you overload functions, you can create a function for each new data type that you define.

The process of overloading routines and the routine resolution rules are described briefly in "Routine Resolution".

The syntax of the Function Name segment is described in "Function Name".

Parameter List

SPL
To define the parameters for an SPL function, specify a parameter name and a data type for each parameter. For more information about defining parameters, see
"Routine Parameter List".

EXT
To define the parameters for an external routine, you can specify a name and you must specify a data type for each parameter. For more information on the syntax of the parameter list, see
"Routine Parameter List".

With both SPL functions and external functions, you can specify an OUT parameter, so that the function can be used with a Statement Local Variable in SQL statements. The OUT parameter is described in more detail in "Routine Parameter List".

Return Clause

INFORMIX-Universal Server considers any routine that is created with a Return clause to be a function. Informix recommends that you use the CREATE FUNCTION statement, not CREATE PROCEDURE, to create functions. For external routines, this rule is strictly enforced.

The syntax of the Return clause is described in "Return Clause".

Specific Name

You can specify a specific name for an SPL procedure or an external procedure. A specific name is a name that is unique in the database. A specific name is useful because more than one procedure can have the same name due to routine overloading.

The syntax of the specific name is described in "Specific Name".

Function Modifier

SPL
When you write an SPL function, you can specify the modifier NOT VARIANT with a WITH clause. Both modifiers apply to Boolean functions. The function modifiers are described in
"Routine Modifier".

EXT
In the CREATE FUNCTION statement, you can specify any of a list of function modifiers with a WITH clause. For more information on the function modifiers, see
"Routine Modifier".

Statement Block

SPL
In an SPL function, you must specify an SPL statement block instead of an External Routine Reference clause. The syntax of the statement block is described in
"Statement Block".

External Routine Reference

EXT
When you register an external function, you must specify an External Routine Reference clause. The External Routine Reference clause specifies the pathname to the procedure object code, which is stored in a shared library. The External Routine Reference clause also specifies the name of the language in which the procedure is written. For more information on the External Routine Reference clause, see
"External Routine Reference".

DOCUMENT Clause

The quoted string in the DOCUMENT clause provides a synopsis and description of the routine. The string is stored in the sysprocbody system catalog table and is intended for the user of the routine.

SPL
To find the description of the SPL procedure update_by_pct, shown in
"SPL Functions", enter a query such as the following:

The preceding query returns the following text:

An SPL routine, external routine, or application program can query the system catalog tables to fetch the DOCUMENT clause and display it for a user.

EXT
You can use a DOCUMENT clause at the end of the CREATE FUNCTION statement, whether or not you use END FUNCTION.

WITH LISTING IN Clause

The WITH LISTING IN option specifies a filename where compile-time warnings are sent. This listing file is created on the database server when you compile an SPL or external routine.

If you specify a filename but not a directory in the WITH LISTING IN clause, INFORMIX-Universal Server uses the home directory on the database server as the default directory. If you do not have a home directory on the server, the file is created in the root directory.

If you do not use the WITH LISTING IN option, the compiler does not generate a list of warnings.

Privileges Necessary for Using CREATE FUNCTION

You must have the Resource privilege on a database to create a function within that database.

The owner of a privilege grants the Execution privilege for that function to other users. If a function has a commutator function, any user who executes the function must have Execute privilege on both the function and its commutator. If a function has a negator function, any user who executes the function must have Execute privilege on both the function and its negator.

Routine Resolution

In Universal Server, you can have more than one instance of a routine with the same name but different parameter lists, as in the following situations:

Routine resolution is the process of determining which instance of a function to execute, given the name of a routine and a list of arguments. For more information on routine resolution, refer to the Extending INFORMIX-Universal Server: User-Defined Routines manual.

PREPARE Statement

E/C
You can use a CREATE FUNCTION statement only within a PREPARE statement. If you want to create a function for which the text is known at compile time, you must put the text in a file and specify this file with the CREATE FUNCTION FROM statement. For more information, see the CREATE FUNCTION FROM statement on
page 1-134.

References

See the CREATE PROCEDURE, CREATE FUNCTION FROM, DROP FUNCTION, DROP ROUTINE, GRANT, EXECUTE FUNCTION, PREPARE, UPDATE STATISTICS, and REVOKE statements in this manual.

In the Informix Guide to SQL: Tutorial, see the discussion of creating and using SPL routines in Chapter 14.

In the Extending INFORMIX-Universal Server: User-Defined Routines manual, see the discussion of how to create and use external functions.

CREATE FUNCTION FROM

Use the CREATE FUNCTION FROM statement to create a new function. The actual text of the CREATE FUNCTION statement resides in a separate file.

Syntax

Element Purpose Restrictions Syntax

filename

The pathname and filename of the file that contains the full text of a CREATE FUNCTION statement. The default pathname is the current directory.

The specified file must exist.

The pathname and filename must conform to the conventions of your operating system.

variable name

The name of a program variable that holds the value of filename

The file that is specified in the program variable must exist.

The name must conform to language-specific rules for variable names.

Usage

An INFORMIX-ESQL/C program cannot directly create a stored function or an external function. That is, it cannot contain the CREATE FUNCTION statement. However, you can create these functions within an ESQL/C program with the following steps:

For example, suppose that the following CREATE FUNCTION statement is in a separate file, called del_ord.sql:

In the ESQL/C program, you can create the delete_order() stored function with the following CREATE FUNCTION FROM statement:

The filename that you provide is relative. If you provide a simple filename (as in the preceding example), the client application looks for the file in the current directory.

Important: The ESQL/C preprocessor does not process the contents of the file that you specify. It just sends the contents to the database server for execution. Therefore, there is no syntactic check that the file that you specify in CREATE FUNCTION FROM actually contains a CREATE FUNCTION statement. However, to improve readability of the code, Informix recommends that you match these two statements. If you are not sure whether the routine is a function or a procedure, use the CREATE ROUTINE FROM statement in the ESQL/C program.

References

See the CREATE FUNCTION, CREATE PROCEDURE, CREATE PROCEDURE FROM, and CREATE ROUTINE FROM statements in this manual.

In the Informix Guide to SQL: Tutorial, see the discussion of creating and using stored functions in Chapter 14.




Informix Guide to SQL: Syntax, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.