![]() |
|
Home | Contents | Index | Master Index | New Book | ![]() |
![]() |
Defining and Using VariablesAny variable that you use in an SPL routine, other than a variable that is implicitly defined in the parameter list of the routine, must be defined in the body of the routine.The value of a variable is held in memory; the variable is not a database object. Therefore, rolling back a transaction does not restore the values of SPL variables. To define a variable in an SPL routine, use the DEFINE statement. DEFINE is not an executable statement. DEFINE must appear after the CREATE PROCEDURE statement and before any other statements. The examples in Figure 14-16 are all legal variable definitions. For more information on DEFINE, see the description of "DEFINE" of the Informix Guide to SQL: Syntax. An SPL variable has a name and a data type. The variable name must be a valid identifier, as described in the "Identifier" segment of the Informix Guide to SQL: Syntax.
In an SPL routine, local variables:
In the beginning of the SPL procedure in Figure 14-17, the integer variables x, y, and z are defined and initialized. The BEGIN and END statements mark a nested statement block in which the integer variables x and q are defined as well as the CHAR variable z. Within the nested block, the redefined variable x masks the original variable x. After the END statement, which marks the end of the nested block, the original value of x is accessible again.
Declaring Variables for Simple Large ObjectsA variable for a simple large object (a TEXT or BYTE object) does not contain the object itself, but rather a pointer to the object. When you define the variable, you must use the keyword REFERENCES before the data type, as Figure 14-19 shows.A variable for a simple large object does not contain the object itself but rather a pointer to the object.
If you define a variable of COLLECTION type, the variable can acquire different type assignments if it is reused within the same statement block, as in Figure 14-21. In this example, varA is an untyped collection variable that changes its data type to the data type of the collection currently assigned to it. The first LET statement makes varA a SET variable. The second LET statement makes varA a LIST variable. If you add another LET statement, you can assign varA still another data type. A variable of SET, MULTISET, or LIST type is a typed collection variable that holds only a collection of the type named in the DEFINE statement. Figure 14-22 shows how to define typed collection variables: Tip: You must always define the elements of a collection variable as NOT NULL. In this example, the variable a is defined to hold a SET of non-null integers; b will hold a MULTISET of non-null row types; and c will hold a LIST of non-null sets of non-null decimal values. In a variable definition, you can nest complex types in any combination or depth to match the data types stored in your database. If you have defined both typed and untyped collection variables in an SPL routine, you can assign any typed collection variable (SET, MULTISET, or LIST) to an untyped collection variable. However, you cannot assign a typed collection variable to another typed collection variable, unless they have the same type.
Declaring Row-Type VariablesRow-type variables hold data from named or unnamed row types. You can define a generic row variable, a named row variable, or an unnamed row variable. Suppose you define the named row types that Figure 14-23 shows.You can define a generic row variable that can hold any row-type value. Figure 14-24 shows how to use the ROW keyword without the name or definition of a row type to define a generic row variable. If you define a variable with the name of a named row type, the variable can only hold data of that row type. In Figure 14-25 the person variable can only hold data of employee_t type. The person variable cannot hold data of zip_t type, address_t type, or any other row type in the database. To define a variable that holds data stored in an unnamed row type, use the ROW keyword followed by the fields of the row type, as Figure 14-26 shows. Because unnamed row types are type-checked for structural equivalence only, a variable defined with an unnamed row type can hold data from any unnamed row type that has the same number of fields and the same type definitions. Therefore, the variable manager can hold data from any of the row types in Figure 14-27.
Declaring Opaque- and Distinct-Type VariablesOpaque-type variables hold data retrieved from opaque data types. Distinct- type variables hold data retrieved from distinct data types. If you define a variable with an opaque data type or a distinct data type, the variable can only hold data of that type.If you define an opaque data type named point and a distinct data type named centerpoint, you can define SPL variables to hold data from the two types, as Figure 14-28 shows. The variable a can only hold data of type point, and b can only hold data of type centerpoint.
If the column contains a collection, row type, or nested complex type, the variable has the complex or nested complex type defined in the column. In Figure 14-29, the variable loc1 defines the data type for the locations column in the image table.
Declaring PROCEDURE Type VariablesIn an SPL routine, you can define a variable of type PROCEDURE and assign the variable the name of an existing SPL routine or external routine. Defining a variable of PROCEDURE type indicates that the variable is a call to a user-defined routine, not a built-in routine of the same name.For example, the statement in Figure 14-30 defines length as an SPL procedure or SPL function, not as the built-in LENGTH function. This definition disables the built-in LENGTH function within the scope of the statement block. You would use such a definition if you had already created an SPL or external routine with the name LENGTH. Because Universal Server supports routine overloading, you can define more than one SPL routine or external routine with the same name. If you call any routine from an SPL routine, Universal Server determines which routine to use, based on the arguments specified and the routine determination rules. For information about routine overloading and routine determination, see the Extending INFORMIX-Universal Server: Data Types manual.
Using Subscripts with VariablesYou can use subscripts with variables of CHAR, VARCHAR, NCHAR, NVARCHAR, BYTE, or TEXT type. The subscripts indicate the starting and ending character positions that you want to use within the variable.Subscripts must always be constants. You cannot use variables as subscripts. Figure 14-31 illustrates how to use a subscript with a CHAR(15) variable. In this example, the customer's last name is placed between positions 4 and 7 of name. The first three characters of the customer's first name is retrieved into positions 1 through 3 of name. The part of the variable that is delimited by the two subscripts is referred to as a substring.
In the SELECT statement in Figure 14-32, customer.lname is a column name, and lname is a variable name.
Variables and SQL FunctionsIf you use the same identifier for an SPL variable as for an SQL function, the database server assumes that an instance of the identifier is a variable and disallows the use of the SQL function. You cannot use the SQL function within the block of code in which the variable is defined. The example in Figure 14-33 shows a block within an SPL procedure in which the variable called user is defined. This definition disallows the use of the USER function in the BEGIN ... END block.
Procedure Names and SQL FunctionsFor information about ambiguities between procedure names and SQL function names, see the Informix Guide to SQL: Syntax.
Although you must define a global variable with a default value, the variable is only set to the default the first time you use it. If you execute the two functions in Figure 14-35 in the order given, the value of gvar would be 4. But if you execute the functions in the opposite order, as Figure 14-36 shows, the value of gvar would be 7. Executing SPL routines is described in more detail in "Executing Routines".
If you do not assign a value to a variable, either by an argument passed to the routine or by a LET statement, the variable has an undefined value. An undefined value is different than a null value. If you attempt to use a variable with an undefined value within the SPL routine, you receive an error. You can assign a value to a procedure variable in any of the following ways:
The LET StatementWith a LET statement, you can use one or more variable names with an equal (=) sign and a valid expression or function name. Each example in Figure 14-37 is a valid LET statement.In Universal Server, you can assign a value to an opaque-type variable. You can also return the value of an external function or another SPL function to an SPL variable. In Universal Server, you can assign a value to an opaque-type variable, a row-type variable, or a field of a row type. You can also return the value of an external function or another SPL function to an SPL variable. Suppose you define the named row types zip_t and address_t of Figure 14-23. Anytime you define a row-type variable, you must initialize the variable before you can use it. Figure 14-38 shows how you might define and initialize a row-type variable. You can use any row-type value to initialize the variable. Once you define and initialize the row-type variable, you can write the LET statements that Figure 14-39 shows.
If you define an opaque-type center that defines a point as the center of a circle, and a function circum() that calculates the circumference of a circle, based on a point and the radius, you can write variable declarations for each. In Figure 14-40, c is an opaque type variable and d holds the value that the external function circum() returns. The detailed syntax of the LET statement is described in the Informix Guide to SQL: Syntax.
Use the CALL or EXECUTE PROCEDURE statements to assign values returned by an SPL function or an external function to one or more SPL variables. You might use either of the statements in Figure 14-42 to return the full name and address from the SPL function read_address into the specified SPL variables.
|
![]() |
![]() |