22
New DB2 V11 Objects: Global variables and Archive tables Author: Venkata Rama Rajesh DB2 DBA for Z/OS Email: [email protected]

IDUG-Global variables and archive tables - Ningapi.ning.com/files/GNqxi34AHIncXuYZOlYj6IYQ3pM63O… ·  · 2017-04-18This PPT will help in understanding the concepts of global variables

Embed Size (px)

Citation preview

New DB2 V11 Objects:Global variables and Archive tables

Author:Venkata Rama Rajesh

DB2 DBA for Z/OSEmail: [email protected]

AbstractThis PPT will help in understanding the concepts of global variables and archive tables which includes its creation, maintenance and its benefits. The concepts explained here is applicable for DB2 versions 11 and 12.

Global variables - IntroductionVariables in program _______________

Variables in SQL-PL _______________

Variables in SQL ________________

1. A global variable is a named memory variable that can be accessed from applications through

SQL statements.

2. An instance of global variable will be created for each application.

3. Global variables are qualified with schema

4. This concept is available in many RDBMS systems including DB2 LUW. But introduced in

DB2 Z/OS in V11 and well enhanced in V12

Global variables - DDLThe CREATE VARIABLE statement creates a global variable. While creating a global variable, we will give its

data type and its default value as shown below.

Example to create a global variable:CREATE VARIABLE IMSREG.TODAY_DOLLER

DECIMAL(5,2)

DEFAULT 68.14;

We cannot ALTER global variable definition. We have to drop and re-create. Example for drop.DROP VARIABLE IMSREG.TODAY_DOLLER;

Global variables - DMLHow to read Global variable ?It is similar to reading DB2 special registers. We can read default value in global variable by using SYSDUMMY table as shown below. SELECT IMSREG.TODAY_DOLLER FROM SYSIBM.SYSDUMMY1; How to use Global variable ?We can use global variables in SQL similar to how we use special registers in SQL as shown below. SELECT * FROM TESTTB1 WHERE SALARY <= 10*IMSREG.TODAY_DOLLER ; How to change global variable value for current session ?We can change global variable for current session by using SET statement as shown below.SET IMSREG.TODAY_DOLLER = 58.14 ;

Global variables - DCL

We can grant or revoke the read and write privileges on global variables. Examples:GRANT READ ON VARIABLE IMSREG.TODAY_DOLLER TO IMSSXB;

REVOKE ALL PRIVILEGES ON VARIABLE IMSREG.TODAY_DOLLER FROM IMSSXB;

Global variables – Catalog informationFollowing are the catalog tables newly added in DB2 11 to provide information on global variables SYSIBM.SYSVARIABLES: Contains one row for each global variable that is created. Some of thecolumns with examples shown below.

SYSIBM.SYSVARIABLEAUTH: Contains one row for each privilege for each authorization ID

Global variables – Drop impact1. Global variable is not dropped if any of the following dependencies exist:

Procedure, function, trigger, or view is dependent on the global variable.

2. Dependent packages are marked invalid when the global variable is dropped.

3. Each and every global variable has a unique identifier called VARID. (VARID in SYSIBM.SYSVARIABLES)

.-RESTRICT-. DROP—VARIABLE—variable-name--+----------+

Recommended DBA procedure for DROP & CREATE Global variable→ Drop depended objects on global variable→ Drop global variable→ Re-create the global variable with new definition→ Re-create the dependent objects→ Rebind the dependent objects

Global variables – Built-in global variablesSchema Global variable Introduced in PurposeSYSIBMADM GET_ARCHIVE V11

Archive tables management

SYSIBMADM MOVE_TO_ARCHIVE V11

SYSIBM CLIENT_IPADDR V11 Remote connection IP address

SYSIBM CATALOG_LEVEL V12 Current DB2 catalog level

SYSIBM DEFAULT_SQLLEVEL V12 Default value of the SQLLEVEL

SYSIBM PRODUCTID_EXT V12 Database manager identifier

SYSIBM TEMPORAL_LOGICAL_TRANSACTION_TIME V12Temporal tables management

SYSIBM TEMPORAL_LOGICAL_TRANSACTIONS V12

Global variables – ApplicationsExample:1 The main purpose of global variables is to enable you to share data between different SQL statements running in the same program without any application logic (host variables, host arrays, temporary tables). Example without application logic:

SET TEMP_CHAR=(SELECT NAME

FROM DB2DBA.CUSTOMER

WHERE ID=2);

INSERT INTO DB2DBA.CUSTOMER1

VALUES(3,TEMP_CHAR);

Example with application logic:ACCEPT WSTATE.

EXEC SQL

SELECT NAME

INTO :WSTATE

FROM DB2DBA.CUSTOMER

WHERE ID=2

END-EXEC.

EXEC SQL

INSERT INTO DB2DBA.CUST

VALUES (1,:WSTATE)

END-EXEC.

Global variables – Applications (Cont.)The global variables aid in executing in both batch or online without any application coding or logic.

SELECT ID, PRODUCT,

CASE STRIP(:PERIOD)

WHEN 'WAVE1' THEN 0.1*PRICE

WHEN 'WAVE2' THEN 0.1*PRICE

WHEN 'WAVE3' THEN 0.1*PRICE

END as discount

FROM SALES

SELECT ID, PRODUCT,

CASE STRIP(DB2DBA.TEMP_CHAR)

WHEN 'WAVE1' THEN 0.1*PRICE

WHEN 'WAVE2' THEN 0.1*PRICE

WHEN 'WAVE3' THEN 0.1*PRICE

END as discount

FROM SALES

Example with application logic: Example without application logic:

Global variables – Applications (Cont.)Global variables can be used as application-level switches . Using global variable, we can control the firing of the trigger.Example for global variable:CREATE VARIABLE DB2DBA.DIS_TRIGGER CHAR(1) DEFAULT 'N';

Example for trigger based on global variable. CREATE TRIGGER DB2DBA.DEPT_CHECK

NO CASCADE BEFORE

DELETE ON DB2DBA.TESTTB1

FOR EACH ROW MODE DB2SQL

WHEN (IMSREG.DIS_TRIGGER = 'N')

SIGNAL SQLSTATE '75001' ('DELETE IS NOT ALLOWED IN THIS PERIOD FOR USERS.

CONTACT MVR DB2-APPLICATION SUPPORT FOR ASSISTANCE');

Archive tables – DDL1. Archive tables are useful to store huge volumes of historical data that are not often referenced2. The original table is called an archive-enabled table. 3. DB2 can store rows that are deleted from an archive-enabled table in an associated archive table.

CREATE: Once we create a base table, We can tag archive table to it using ENABLE ARCHIVE option in ALTER table statement.We can un-tag archive table from base table using DISABLE ARCHIVE option. In ALTER table statement, no other clause is allowed.ALTER TABLE EMPL

ENABLE ARCHIVE USE EMPL_R;

Rules for archive enabled tableShould not have clone table or system-period temporal table.The table must not have a column mask or row permission defined.

Archive tables – DDL (Cont.)ALTER: Most of the common DML operations are not possible on the archive table or archive enabled table except ADD COLUMN statement. We would end up getting the sql error -20180 or -750 as shown below. If we add the column in base table then the column is also added to the associated archive table automatically.

ALTER TABLE EMPL

ALTER COLUMN NAME SET DATA TYPE CHAR(15);

+---------+---------+---------+---------+---------+---------+

DSNT408I SQLCODE = -20180, ERROR: COLUMN NAME IN TABLE DB2DBA.EMPL CAN NOT BE ALTERED AS SPECIFIED

RENAME TABLE EMPL TO EMP420

+---------+---------+---------+---------+---------+---------+

DSNT408I SQLCODE = -750, ERROR: THE SOURCE DB2DBA.EMPL CANNOT BE RENAMED OR ALTERED AS SPECIFIED

Archive tables – DDL (Cont.)DBA Procedure to perform a change on archive enabled table.1. Disable archiving for base table (archive enabled table)

ALTER TABLE EMPL

DISABLE ARCHIVE;

2. Perform change on base tableALTER TABLE EMPL

ALTER COLUMN NAME SET DATA TYPE CHAR(15);

3. Perform change on archive tableALTER TABLE EMPL_R

ALTER COLUMN NAME SET DATA TYPE CHAR(15);

4. Enable archiving for base table.ALTER TABLE EMPL

ENABLE ARCHIVE USE EMPL_R;

Archive tables – DDL (Cont.)DROP: If we drop the archive enabled table then archive table will be dropped implicitly. We cannot drop the archive

table until relation is there. If we try to drop then sql error -478 will raised as shown below.

DROP TABLE EMPL_R;

------------+------------+--------------+--------------+-------------+-----------+

DSNT408I SQLCODE = -478, ERROR: ALTER, DROP, OR REVOKE AFFECTING OBJECT TYPE TABLE CANNOT BE PROCESSED

BECAUSE OBJECT DB2DBA.EMPL OF TYPE TABLE IS DEPENDENT ON IT

Note: You can drop a database that contains an archive table only if the database also contains the associated archive-enabled table.

You can drop a database that contains an archive-enabled table when the associated archive table is contained in another database.

In this case, the action cascades to drop the archive table.

Archive tables – DML How DML works on archive enabled and archive tables will depend upon following built-in global variables

1. SYSIBMADM.MOVE_TO_ARCHIVE

(DELETE / TRUNCATE on base table)

2. SYSIBMADM.GET_ARCHIVE

(SELECT on base table)

Global variables – DML (Cont.)

Option Global variableE Archiving is enabledN Archiving is disabledY Archiving is enabled. Additionally, when the global variable is set to 'Y', an insert or

update or merge operation that specifies the archive-enabled table as the target of the statement will return an error.

SYSIBMADM.MOVE_TO_ARCHIVEThis global variable can have 3 possible values

Ex: SET SYSIBMADM.MOVE_TO_ARCHIVE = 'Y' ;

DELETE FROM EMPL WHERE ID=1 ; – – SQLCODE 0

INSERT INTO EMPL VALUES(1,'RAJESH',32.6,'DB2'); – – SQLCODE -20555

Global variables – DML (Cont.)

Option Global variableY Specifies that when a table-reference is an archive-enabled table, the table reference includes

rows in the associated archive table.

N Specifies that when a table-reference is an archive-enabled table, the table reference does not include rows in the associated archive table. This is the default value.

SYSIBMADM.GET_ARCHIVEThis global variable can have 2 possible values

SET SYSIBMADM.GET_ARCHIVE = 'N' ; SELECT * FROM EMPL; ---------+---------+---------+------+

ID NAME SALARY DEPT ---------+---------+---------+------+

2 RAJU 42.60 DBDBA DSNE610I NUMBER OF ROWS DISPLAYED IS 1

SET SYSIBMADM.GET_ARCHIVE = 'Y' ; SELECT * FROM EMPL; ---------+---------+---------+---------+

ID NAME SALARY DEPT ---------+---------+---------+---------+

2 RAJU 42.60 DBDBA3 RAMU 12.30 DB2

DSNE610I NUMBER OF ROWS DISPLAYED IS 2

Archive tables – DCL, Catalog info.DCL: If we provide access on archive-enabled table to user, that itself is enough for user to use archiving concepts.

We do not need to provide access on archive table.

Catalog info.: l TYPE= 'R' refers archive table in SYSIBM.SYSTABLESl ARCHIVING_SCHEMA and ARCHIVING_TABLE, If the table is an archive-enabled table, this columns

contains the schema name and table name of the archive table.

SELECT CREATOR, NAME, TYPE, ARCHIVING_SCHEMA ,ARCHIVING_TABLE

FROM SYSIBM.SYSTABLES WHERE NAME IN ('EMPL','EMPL_R');

---------+---------+---------+---------+---------+---------+CREATOR NAME TYPE ARCHIVING_SCHEM ARCHIVING_TABLE ---------+---------+---------+---------+---------+---------+

DB2DBA EMPL T DB2DBA EMPL_R

DB2DBA EMPL_R R DB2DBA EMPL

---------+---------+---------+---------+---------+---------+

Archive tables – ARCHIVESENSITIVE (default YES)This bind option determines whether references to archive-enabled tables SQL statements are affected by the value of the SYSIBMADM.GET_ARCHIVE global variable. If this option is enabled, then for static SQL's DB2 automatically prepares two access path strategies: one for use when archiving is enabled (GET_ARCHIVE='Y') and another for use when it is not (GET_ARCHIVE='N')

For packages ARCHIVESENSITIVE For UDFs and Sprocs ARCHIVE SENSITIVE (No space between ARCHIVE and SENSITIVE) (Space between ARCHIVE and SENSITIVE)

BIND PACKAGE CREATE FUNCTION (SQL scalar)

REBIND PACKAGE ALTER FUNCTION (SQL scalar)

CREATE TRIGGER (implicit trigger package) CREATE PROCEDURE (SQL native)

ALTER PROCEDURE (SQL native)

BibliographyIBM Knowledge center

DB2 11: The Database for Big Data and Analytics

DB2 11 for z/OS Transparent Archiving by Stan Goodwin