SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION CISTOOLS.GET_DESCRIPTOR ( 
	SQLSTMT VARCHAR(32000) ) 
	RETURNS TABLE ( 
	COLUMNCARDINALITY BIGINT , 
	DATETIMECODE INTEGER , 
	BASEDBNAME VARCHAR(128) , 
	BASECOLUMNNAME VARCHAR(128) , 
	BASELIBRARYNAME VARCHAR(128) , 
	BASETABLENAME VARCHAR(128) , 
	COLUMNCCSID INTEGER , 
	COLUMNDBNAME VARCHAR(128) , 
	COLUMNGENERATED INTEGER , 
	COLUMNGENERATIONTYPE INTEGER , 
	COLUMNHIDDEN INTEGER , 
	COLUMNNAME VARCHAR(128) , 
	COLUMNROWCHANGE INTEGER , 
	COLUMNLIBNAME VARCHAR(128) , 
	COLUMNTBLNAME VARCHAR(128) , 
	COLUMNALLOWUPDATE INTEGER , 
	CORRELATIONNAME VARCHAR(128) , 
	CURSORNAME VARCHAR(128) , 
	COLUMNLABEL VARCHAR(60) , 
	PARAMNAME VARCHAR(128) , 
	RESULTSETROW BIGINT , 
	SYSTEMCOLUMNNAME CHAR(10) , 
	KEYMEMBER INTEGER , 
	COLUMNLENGTH INTEGER , 
	COLUMNLEVEL INTEGER , 
	OBJECTNAME VARCHAR(128) , 
	ISNULLABLE INTEGER , 
	OCTETLENGTH INTEGER , 
	PARAMMODE INTEGER , 
	PARAMORDPOS INTEGER , 
	ORDINALPOS INTEGER , 
	PARAMSPECIFICDB VARCHAR(128) , 
	PARAMSPECIFICTABLE VARCHAR(128) , 
	PARAMSPECIFICLIB VARCHAR(128) , 
	PRECISION INTEGER , 
	RETURNARRAYSIZE INTEGER , 
	RETURNLENGTH INTEGER , 
	RETURNOCTETLENGTH INTEGER , 
	SCALE INTEGER , 
	DATATYPE INTEGER , 
	SQLTYPE VARCHAR(128) , 
	ISUNNAMED INTEGER , 
	UDTCATALOG VARCHAR(128) , 
	UDTNAME VARCHAR(128) , 
	UDTSCHEMA VARCHAR(128) , 
	UDTCODE VARCHAR(128) )   
	LANGUAGE SQL 
	SPECIFIC CISTOOLS.GET_DESCRIPTOR 
	NOT DETERMINISTIC 
	READS SQL DATA 
	CALLED ON NULL INPUT 
	NO EXTERNAL ACTION 
	CONCURRENT ACCESS RESOLUTION DEFAULT 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DBGVIEW = *SOURCE , 
	DECRESULT = (31, 31, 00) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *OWNER , 
	OPTLOB = *YES , 
	SQLCURRULE = *STD , 
	SRTSEQ = *HEX   
	BEGIN 
DECLARE STMTHANDLE BIGINT ; 
DECLARE DESCHANDLE VARCHAR ( 128 ) ; 
DECLARE VCARDINALITY BIGINT DEFAULT 0 ; 
DECLARE VDATETIMECODE INTEGER DEFAULT 0 ; 
DECLARE VBASEDBNAME VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VBASECOLUMNNAME VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VBASELIBRARYNAME VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VBASETABLENAME VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VCCSID INTEGER DEFAULT 0 ; 
DECLARE VCOLUMNDBNAME VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VCOLUMNGENERATED INTEGER DEFAULT 0 ; 
DECLARE VCOLUMNGENERATIONTYPE INTEGER DEFAULT 0 ; 
DECLARE VCOLUMNHIDDEN INTEGER DEFAULT 0 ; 
DECLARE VCOLUMNNAME VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VCOLUMNROWCHANGE INTEGER DEFAULT 0 ; 
DECLARE VCOLUMNLIBNAME VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VCOLUMNTBLNAME VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VCOLUMNALLOWUPDATE INTEGER DEFAULT 0 ; 
DECLARE VCORRELATIONNAME VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VCURSORNAME VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VLABEL VARCHAR ( 60 ) DEFAULT ' ' ; 
DECLARE VPARAMNAME VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VRESULTSETROW BIGINT DEFAULT 0 ; 
DECLARE VSYSTEMCOLUMNNAME CHAR ( 10 ) DEFAULT ' ' ; 
DECLARE VKEYMEMBER INTEGER DEFAULT 0 ; 
DECLARE VLENGTH INTEGER DEFAULT 0 ; 
DECLARE VLEVEL INTEGER DEFAULT 0 ; 
DECLARE VOBJECTNAME VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VISNULLABLE INTEGER DEFAULT 0 ; 
DECLARE VOCTETLENGTH INTEGER DEFAULT 0 ; 
DECLARE VPARAMMODE INTEGER DEFAULT 0 ; 
DECLARE VPARAMORDPOS INTEGER DEFAULT 0 ; 
DECLARE VORDINALPOS INTEGER DEFAULT 0 ; 
DECLARE VPARAMSPECIFICDB VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VPARAMSPECIFICTABLE VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VPARAMSPECIFICLIB VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VPRECISION INTEGER DEFAULT 0 ; 
DECLARE VRETURNARRAYSIZE INTEGER DEFAULT 0 ; 
DECLARE VRETURNLENGTH INTEGER DEFAULT 0 ; 
DECLARE VRETURNOCTETLENGTH INTEGER DEFAULT 0 ; 
DECLARE VSCALE INTEGER DEFAULT 0 ; 
DECLARE VDATATYPE INTEGER DEFAULT 0 ; 
DECLARE VSQLTYPE VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VISUNNAMED INTEGER DEFAULT 0 ; 
DECLARE VUDTCATALOG VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VUDTNAME VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VUDTSCHEMA VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE VUDTCODE VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE I INTEGER DEFAULT 1 ; 
DECLARE TOKEN VARCHAR ( 128 ) DEFAULT ' ' ; 
DECLARE RTNSQLCODE INT ; 
DECLARE COLCOUNT INTEGER ; 
DECLARE SQLCODE INTEGER DEFAULT 0 ; 
DECLARE SQLSTATE CHAR ( 5 ) DEFAULT '00000' ; 
DECLARE CONTINUE HANDLER FOR SQLSTATE '22501' , SQLSTATE '33000' 
BEGIN 
DEALLOCATE DESCRIPTOR LOCAL DESCHANDLE ; 
GET DIAGNOSTICS CONDITION 1 TOKEN = DB2_ORDINAL_TOKEN_2 ; 
SET COLCOUNT = TOKEN ; 
ALLOCATE DESCRIPTOR LOCAL DESCHANDLE WITH MAX COLCOUNT ; 
DESCRIBE STMTHANDLE USING DESCRIPTOR LOCAL DESCHANDLE ; 
SET I = 1 ; 
END ; 
SET DESCHANDLE = 'Descr' || HEX ( GENERATE_UNIQUE ( ) ) ; 
PREPARE STMTHANDLE FROM SQLSTMT ; 
ALLOCATE DESCRIPTOR LOCAL DESCHANDLE WITH MAX 1024 ; 
DESCRIBE STMTHANDLE USING DESCRIPTOR LOCAL DESCHANDLE ; 
GET DESCRIPTOR LOCAL DESCHANDLE COLCOUNT = COUNT ; 
WHILE I <= COLCOUNT DO 
GET DESCRIPTOR LOCAL DESCHANDLE 
VALUE I 
VCARDINALITY = CARDINALITY , 
VDATETIMECODE = DATETIME_INTERVAL_CODE , 
VBASEDBNAME = DB2_BASE_CATALOG_NAME , 
VBASECOLUMNNAME = DB2_BASE_COLUMN_NAME , 
VBASELIBRARYNAME = DB2_BASE_SCHEMA_NAME , 
VBASETABLENAME = DB2_BASE_TABLE_NAME , VCCSID = DB2_CCSID , 
VCOLUMNDBNAME = DB2_COLUMN_CATALOG_NAME , 
VCOLUMNGENERATED = DB2_COLUMN_GENERATED , 
VCOLUMNGENERATIONTYPE = DB2_COLUMN_GENERATION_TYPE , 
VCOLUMNHIDDEN = DB2_COLUMN_HIDDEN , 
VCOLUMNNAME = DB2_COLUMN_NAME , 
VCOLUMNROWCHANGE = DB2_COLUMN_ROW_CHANGE , 
VCOLUMNLIBNAME = DB2_COLUMN_SCHEMA_NAME , 
VCOLUMNTBLNAME = DB2_COLUMN_TABLE_NAME , 
VCOLUMNALLOWUPDATE = DB2_COLUMN_UPDATABILITY , 
VCORRELATIONNAME = DB2_CORRELATION_NAME , 
VCURSORNAME = DB2_CURSOR_NAME , VLABEL = DB2_LABEL , 
VPARAMNAME = DB2_PARAMETER_NAME , 
VRESULTSETROW = DB2_RESULT_SET_ROWS , 
VSYSTEMCOLUMNNAME = DB2_SYSTEM_COLUMN_NAME , 
VKEYMEMBER = KEY_MEMBER , VLENGTH = LENGTH , VLEVEL = LEVEL , 
VOBJECTNAME = NAME , VISNULLABLE = NULLABLE , 
VOCTETLENGTH = OCTET_LENGTH , VPARAMMODE = PARAMETER_MODE , 
VPARAMORDPOS = PARAMETER_ORDINAL_POSITION , 
VPARAMSPECIFICDB = PARAMETER_SPECIFIC_CATALOG , 
VPARAMSPECIFICTABLE = PARAMETER_SPECIFIC_NAME , 
VPARAMSPECIFICLIB = PARAMETER_SPECIFIC_SCHEMA , 
VPRECISION = PRECISION , 
VRETURNARRAYSIZE = RETURNED_CARDINALITY , 
VRETURNLENGTH = RETURNED_LENGTH , 
VRETURNOCTETLENGTH = RETURNED_OCTET_LENGTH , VSCALE = SCALE , 
VDATATYPE = TYPE , VISUNNAMED = UNNAMED , 
VUDTCATALOG = USER_DEFINED_TYPE_CATALOG , 
VUDTNAME = USER_DEFINED_TYPE_NAME , 
VUDTSCHEMA = USER_DEFINED_TYPE_SCHEMA , 
VUDTCODE = USER_DEFINED_TYPE_CODE ; 
SET VLABEL = CISTOOLS . STRIP_SPACES ( VLABEL ) ; 
SET VORDINALPOS = I ; 
-- This code block maps data types to their equivalent SQL types 
-- Standardize variable names 
IF VDATATYPE = 5 THEN 
SET VSQLTYPE = 'SQL_SMALLINT' ; 
SET VLENGTH = 2 ; 
SET VOCTETLENGTH = 2 ; 
ELSEIF VDATATYPE = 4 THEN 
SET VSQLTYPE = 'SQL_INTEGER' ; 
SET VLENGTH = 4 ; 
SET VOCTETLENGTH = 4 ; 
ELSEIF VDATATYPE = 25 THEN 
SET VSQLTYPE = 'SQL_BIGINT' ; 
SET VLENGTH = 8 ; 
SET VOCTETLENGTH = 8 ; 
ELSEIF VDATATYPE = 3 THEN 
SET VSQLTYPE = 'SQL_DECIMAL' ; 
SET VLENGTH = ( VPRECISION / 2 ) + 1 ; 
SET VOCTETLENGTH = ( VPRECISION / 2 ) + 1 ; 
ELSEIF VDATATYPE = 2 THEN 
SET VSQLTYPE = 'SQL_NUMERIC' ; 
ELSEIF VDATATYPE = 7 THEN 
SET VSQLTYPE = 'SQL_REAL' ; 
SET VLENGTH = 4 ; 
SET VOCTETLENGTH = 4 ; 
ELSEIF VDATATYPE = 8 THEN 
SET VSQLTYPE = 'SQL_DECFLOAT' ; 
SET VLENGTH = 8 ; 
SET VOCTETLENGTH = 8 ; 
ELSEIF VDATATYPE = - 360 THEN 
SET VSQLTYPE = 'SQL_DECFLOAT' ; 
ELSEIF VDATATYPE = 1 THEN 
SET VSQLTYPE = 'SQL_CHARACTER' ; 
ELSEIF VDATATYPE = 12 THEN 
SET VSQLTYPE = 'SQL_VARCHAR' ; 
ELSEIF VDATATYPE = 40 THEN 
SET VSQLTYPE = 'SQL_CLOB' ; 
ELSEIF VDATATYPE = - 95 THEN 
SET VSQLTYPE = 'SQL_GRAPHIC' ; 
SET VOCTETLENGTH = VLENGTH * 2 ; 
ELSEIF VDATATYPE = - 96 THEN 
SET VSQLTYPE = 'SQL_VARGRAPHIC' ; 
SET VOCTETLENGTH = VLENGTH * 2 ; 
ELSEIF VDATATYPE = - 350 THEN 
SET VSQLTYPE = 'SQL_DBCLOB' ; 
SET VOCTETLENGTH = VLENGTH * 2 ; 
ELSEIF VDATATYPE = - 2 THEN 
SET VSQLTYPE = 'SQL_BINARY' ; 
ELSEIF VDATATYPE = - 3 THEN 
SET VSQLTYPE = 'SQL_VARBINARY' ; 
ELSEIF VDATATYPE = 30 THEN 
SET VSQLTYPE = 'SQL_BLOB' ; 
ELSEIF VDATATYPE = 9 THEN 
IF VDATETIMECODE = 1 THEN 
SET VSQLTYPE = 'SQL_DATE' ; 
SET VLENGTH = 10 ; 
SET VOCTETLENGTH = 10 ; 
ELSEIF VDATETIMECODE = 2 THEN 
SET VSQLTYPE = 'SQL_TIME' ; 
SET VLENGTH = 8 ; 
SET VOCTETLENGTH = 8 ; 
ELSEIF VDATETIMECODE = 3 THEN 
SET VSQLTYPE = 'SQL_TIMESTAMP' ; 
SET VLENGTH = 26 ; 
SET VOCTETLENGTH = 26 ; 
END IF ; 
ELSEIF VDATATYPE = 70 THEN 
SET VSQLTYPE = 'SQL_DATALINK' ; 
ELSEIF VDATATYPE = - 904 THEN 
SET VSQLTYPE = 'SQL_ROWID' ; 
ELSEIF VDATATYPE = 137 THEN 
SET VSQLTYPE = 'SQL_XML' ; 
ELSEIF VDATATYPE = - 400 THEN 
SET VSQLTYPE = 'SQL_GRAPHIC' ; 
SET VOCTETLENGTH = VLENGTH * 2 ; 
ELSEIF VDATATYPE = - 916 THEN 
SET VSQL_TYPE = 'SQL_BLOB' ; 
SET VLENGTH = 267 ; 
SET VOCTETLENGTH = 267 ; 
ELSEIF VDATATYPE = - 920 THEN 
SET VSQL_TYPE = 'SQL_CLOB' ; 
SET VLENGTH = 267 ; 
SET VOCTETLENGTH = 267 ; 
ELSEIF VDATATYPE = - 924 THEN 
SET VSQL_TYPE = 'SQL_DBCLOB' ; 
SET VLENGTH = 267 ; 
SET VOCTETLENGTH = 267 ; 
ELSEIF VDATATYPE = - 972 THEN 
SET VSQL_TYPE = 'SQL_RESULT_SET_LOCATOR' ; 
SET VLENGTH = 8 ; 
SET VOCTETLENGTH = 8 ; 
ELSE 
SET VSQLTYPE = 'UNKNOWN TYPE' ; 
END IF ; 
PIPE ( 
VCARDINALITY , 
VDATETIMECODE , VBASEDBNAME , VBASECOLUMNNAME , VBASELIBRARYNAME , 
VBASETABLENAME , VCCSID , VCOLUMNDBNAME , VCOLUMNGENERATED , 
VCOLUMNGENERATIONTYPE , VCOLUMNHIDDEN , VCOLUMNNAME , 
VCOLUMNROWCHANGE , VCOLUMNLIBNAME , VCOLUMNTBLNAME , 
VCOLUMNALLOWUPDATE , VCORRELATIONNAME , VCURSORNAME , VLABEL , 
VPARAMNAME , VRESULTSETROW , VSYSTEMCOLUMNNAME , VKEYMEMBER , 
VLENGTH , VLEVEL , VOBJECTNAME , VISNULLABLE , VOCTETLENGTH , 
VPARAMMODE , VPARAMORDPOS , VORDINALPOS , VPARAMSPECIFICDB , 
VPARAMSPECIFICTABLE , VPARAMSPECIFICLIB , VPRECISION , 
VRETURNARRAYSIZE , VRETURNLENGTH , VRETURNOCTETLENGTH , VSCALE , 
VDATATYPE , VSQLTYPE , VISUNNAMED , VUDTCATALOG , VUDTNAME , 
VUDTSCHEMA , VUDTCODE ) ; 
SET I = I + 1 ; 
END WHILE ; 
DEALLOCATE DESCRIPTOR LOCAL DESCHANDLE ; 
RETURN ; 
END  ; 

COMMENT ON PARAMETER SPECIFIC FUNCTION CISTOOLS.GET_DESCRIPTOR 
( SQLSTMT IS 'SQL Statement' ) ; 

LABEL ON SPECIFIC FUNCTION CISTOOLS.GET_DESCRIPTOR 
	IS 'Get Descriptor' ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.GET_DESCRIPTOR 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.GET_DESCRIPTOR 
TO PUBLIC ; 

