IBM Books

Call Level Interface Guide and Reference


Casting User Defined Types (UDTs)

If a parameter marker is used in a predicate of a query statement, and the parameter is a user defined type, the statement must use a CAST function to cast either the parameter marker or the UDT.

For example, if the following type and table is defined:

 CREATE DISTINCT TYPE CNUM AS INTEGER WITH COMPARISONS
 
 CREATE TABLE CUSTOMER (
         Cust_Num     CNUM NOT NULL,
         First_Name   CHAR(30) NOT NULL,
         Last_Name    CHAR(30) NOT NULL,
         Phone_Num    CHAR(20) WITH DEFAULT,
         PRIMARY KEY  (Cust_Num) )

This statement would fail since the parameter marker cannot be of type CNUM and thus the comparison fails due to incompatible types: SELECT first_name, last_name, phone_num FROM customer where cust_num = ?

Casting the column to integer (its base SQL type), allows the comparison to work since a parameter can be provided for type integer:

  SELECT  first_name, last_name, phone_num from customer
  where cast( cust_num as integer ) = ?

Alternatively the parameter marker can be cast to INTEGER and the server can then apply the INTEGER to CNUM conversion:

  SELECT  first_name, last_name, phone_num FROM customer
  where cust_num = cast( ? as integer )

Refer to the custrep.c sample file for a full working example.

Refer to the SQL Reference for more information about:


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]

[ DB2 List of Books | Search the DB2 Books ]