This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
zAnalytics DB2 Update Day 2015 – March 23-27, 2015
5
Benefits of Using Templates
� They engrain good programming practices� You can leverage existing programming standards
� Begin all parameters with their own same naming convention e.g. P_, IN_, P_IN_
� Begin all variables with their own same naming convention e.g. V_, x
� Initialize all variables and output parameters at the beginning of the procedure
� They increase the readability and maintainability of the code� Use comments – lots of comments!� Use indentation consistently� Label all blocks – compound blocks as well as IF, WHILE, etc.
�Qualify, qualify, qualify!�Qualify column names with table names �Qualify SQL parameters by procedure name�Qualify SQL variables by label of the compound statement
� If no qualification or ambiguous qualification exists:� The name is checked to see if it is the name of a column of any
existing table or a view that is specified in the SQL routine body at the current server
� If the referenced tables or views do not exist at the current server, the name will be checked first as an SQL variable name in the compound statement and then as an SQL parameter name.
-- SQL Procedure Statements ----------------------------------------------------------------------------------------- construct an array with new SKU values SET MYSKUARRAY = ARRAY['sku99','sku01','sku20','sku04'];
-- construct a 2nd array with new PRICE values SET MYPRICEARRAY = ARRAY[99.00, 1.99, 20.98, 4.96];
-- populate table with values from arrays INSERT INTO PRODUCT (SKU, PRICE) SELECT * FROM UNNEST(MYSKUARRAY,MYPRICEARRAY) AS T(NEW_SKU,NEW_PRICE) ;
zAnalytics DB2 Update Day 2015 – March 23-27, 2015
20
Appending elements to an Array – Solution 1CREATE PROCEDURE NEW_PRODUCT_INFO ( )….LANGUAGE SQLMAIN_LOGIC: BEGIN…--------------------------------------------------------------------------------------- SQL Procedure Statements ----------------------------------------------------------------------------------------- populate 1st array with NAMEsSET MYVCHARRAY1 = (SELECT ARRAY_AGG (NAME) FROM FRIENDS); -- populate 2nd array with TELEsSET MYVCHARRAY2 = (SELECT ARRAY_AGG (TELE) FROM FRIENDS); -- determine number of elements in 1st arraySET IDX = CARDINALITY (MYVCHARRAY1);
-- loop over all elements in 2nd arrayWHILE LCV <= CARDINALITY (MYVCHARRAY2) DO -- assign elements from 2nd array into 1st array SET MYVCHARRAY1[IDX + LCV] = MYVCHARRAY2[LCV]; SET LCV = LCV + 1;
END WHILE;
-- verify results INSERT INTO RESULT_TB(INFO) SELECT * FROM UNNEST(MYVCHARRAY1) AS T(VALUE);
FROM TABLE (SELECT T.VALUE AS RESULT FROM UNNEST(MYVCHARRAY1) AS T(VALUE) UNION ALL SELECT T.VALUE AS RESULT FROM UNNEST(MYVCHARRAY2) AS T(VALUE)) AS VAL);
-- populate result table INSERT INTO RESULT_TB(INFO) SELECT * FROM UNNEST(MYVCHARRAY1) AS T(VALUE);
zAnalytics DB2 Update Day 2015 – March 23-27, 2015
22
Appending elements to an Array – Solution 3CREATE FUNCTION APPEND_ARRAY(ARR1 INTARRAYTYPE, ARR2 INTARRAYTYPE)RETURNS INTARRAYTYPEBEGINDECLARE COUNTER INT DEFAULT 1;
IF ARR2 IS NULL OR CARDINALITY(ARR2) = 0 THENRETURN ARR1;
END IF;
SET ARR1 = (SELECT ARRAY_AGG(TX.C1 ORDER BY TX.ID) FROM (SELECT U.C1,U.ID FROM UNNEST(ARR1) WITH ORDINALITY AS U(C1,ID) UNION ALLSELECT V.C1,V.ID + CARDINALITY(ARR1) FROM UNNEST(ARR2) WITH ORDINALITY AS V(C1,ID) ) TX(C1,ID)) ;
END IF; RETURN ARR1;
END#
CREATE PROCEDURE PRCAP()DYNAMIC RESULT SETS 1… MAIN_LOGIC:
BEGIN…DECLARE ARR1,ARR2 INTARRAYTYPE; …DECLARE C1 CURSOR WITH RETURN FORSELECT U.C1 FROM UNNEST(ARR1) AS U(C1);
SET ARR1=(SELECT ARRAY_AGG(INT(SALARY)) FROM EMP);SET ARR2=(SELECT ARRAY_AGG(INT(SALARY)) FROM EMP);
zAnalytics DB2 Update Day 2015 – March 23-27, 2015
23
Passing sets of values – Comma Delimited Concatenated StringsCREATE PROCEDURE NONARRAY1IN1OUT (IN IN_STRING VARCHAR(1000),
OUT OUT_STRING VARCHAR(1000))MAIN_LOGIC: BEGIN…-- declare a cursor that invokes a UDF that parses the comma delimited input stringDECLARE ARRAYINCUR CURSOR FORSELECT I_PRICE FROM ITEM, TABLE(String2Set(IN_STRING,',')) AS T(DAT) WHERE I_ID = T.DATFOR FETCH ONLY;
OPEN ARRAYINCUR ;
WHILE (MAIN_LOGIC.EOD = 0 ) DO-- assign local variable to Nth value in comma delimited input stringFETCH FROM ARRAYINCUR INTO MAIN_LOGIC.PRICE;
zAnalytics DB2 Update Day 2015 – March 23-27, 2015
24
Passing sets of values – Arrays
CREATE PROCEDURE ARRAY1IN1OUT (IN IN_IDARRAY IDARRAY_TYPE,OUT OUT_PRICEARRAY PRICEARRAY_TYPE)
LANGUAGE SQL
MAIN_LOGIC: BEGIN…-- build output array by selecting values directly from table based on input array valuesSELECT ARRAY_AGG (I_PRICE) INTO OUT_PRICEARRAYFROM (SELECT I_PRICE
FROM ITEM, (SELECT ID FROM UNNEST(IN_IDARRAY) AS ITEMID (ID)) AS ITEMID
WHERE I_ID = ITEMID.ID) AS TB FETCH FIRST 1 ROW ONLY;
END MAIN_LOGIC;
Accepts an Array as InputReturns an Array as Output
Returns data based on Input Array Values
Treat an Array like a Table i.e. 1 element = 1 row
zAnalytics DB2 Update Day 2015 – March 23-27, 2015
26
Global Variables
�Benefits�Allows users to share information across SQL statements �Allows capability to set once, use everywhere �Provides additional security via DB2 GRANT and REVOKE
�Characteristics�Similar to special registers� Their definitions are global and shared across different
connections� Their contents are only shared within the same connection
� Each connection maintains its own instantiation� Their contents are NOT affected by COMMIT nor ROLLBACK�Use only the UNICODE encoding scheme
zAnalytics DB2 Update Day 2015 – March 23-27, 2015
39
Acknowledgements and Disclaimers
Availability . References in this presentation to IBM products, programs, or services do not imply that they will be available in all countries in which IBM operates.
The workshops, sessions and materials have been prepared by IBM or the session speakers and reflect their own views. They are provided for informational purposes only, and are neither intended to, nor shall have the effect of being, legal or other guidance or advice to any participant. While efforts were made to verify the completeness and accuracy of the information contained in this presentation, it is provided AS-IS without warranty of any kind, express or implied. IBM shall not be responsible for any damages arising out of the use of, or otherwise related to, this presentation or any other materials. Nothing contained in this presentation is intended to, nor shall have the effect of, creating any warranties or representations from IBM or its suppliers or licensors, or altering the terms and conditions of the applicable license agreement governing the use of IBM software.
All customer examples described are presented as illustrations of how those customers have used IBM products and the results they may have achieved. Actual environmental costs and performance characteristics may vary by customer. Nothing contained in these materials is intended to, nor shall have the effect of, stating or implying that any activities undertaken by you will result in any specific sales, revenue growth or other results.
— U.S. Government Users Restricted Rights – Use, dupl ication or disclosure restricted by GSA ADP Schedul e Contract with IBM Corp.
IBM, the IBM logo, ibm.com, and DB2 are trademarks or registered trademarks of International Business Machines Corporation in the United States, other countries, or both. If these and other IBM trademarked terms are marked on their first occurrence in this information with a trademark symbol (® or TM), these symbols indicate U.S. registered or common law trademarks owned by IBM at the time this information was published. Such trademarks may also be registered or common law trademarks in other countries. A current list of IBM trademarks is available on the Web at
�“Copyright and trademark information” at www.ibm.com/legal/copytrade.shtml
�Other company, product, or service names may be trademarks or service marks of others.