sql for the system administrator - gateway/400 group · pdf filesql for the system...
TRANSCRIPT
© 2015 IBM Corporation1 SQL for the System Admin
Power Systems 2015
SQL for the System Administrator
Scott [email protected]
DB2 for i Business Architect
For…
© 2015 IBM Corporation2 SQL for the System Admin
Commands, APIs and tools – consider the options
• IBM i has a long history of creating Commands and APIs, so much so that we frequently conclude that further investment in these interfaces is the appropriate approach to deliver new capabilities to our customers
• The subject of this presentation is to explain the role of SQL based interfaces to allow non-DB2 for i products and components to understand that an alternative / additional choice exists
• Development Cost, Value to the customer and Risk are the important factors to consider for any enhancement
© 2015 IBM Corporation3 SQL for the System Admin
Commands, APIs and tools – Why consider SQL?
• Even though IBM i customers are used to commands and APIs, they are not always the easiest interfaces for customers to utilize (especially if from a client or the web).
• In many cases, customers are required to write specialized programs to exploit a new IBM i command or API.
• Across the industry, SQL is recognized as a standardized and easy language to access the database. SQL skills are either commonplace or customers have a desire to do more with SQL.
• SQL as a language construct does not need to be limited to database topics.
• SQL can be used on IBM i to easily extract deep details on non-database topics. This presentation contains proof of this point.
© 2015 IBM Corporation4 SQL for the System Admin
Commands, APIs and tools – Why consider SQL?
• With SQL-based options, consider which interface provides the best “tool for the task”
• Customer feedback SQL based interfaces has been very positive
• Besides being easy to use, SQL queries can be used for:
– Selection (WHERE clause)
– Ordering
– Grouping
– Joining
– Top ‘n’ results
– IN, LIKE, NOT, and more…
Unleash the power of the DB2 for i query engine
© 2015 IBM Corporation5 SQL for the System Admin
Where to start? Get educated
• Understand the SQL coding environments:RUNSQL, RUNSQLSTM, Run SQL Scripts, STRSQL, CRTSQLxxxx, ODBC, JDBC, PHP, DRDA, etc…
• Understand the query engine and optimizer options:
• Understand the SQL coding options:Explicit 3-part names, Implicit 3-part names, INSERT with remote subselect, CREATE TABLE with remote subselect, etc...
• Keep the SQL Reference close by
• Like other crafts… with repeated study and practice, your skills will improve
© 2015 IBM Corporation6 SQL for the System Admin
New to SQL? Use Navigator’s Run SQL Scripts
• Powerful, graphical interface
IBM i Navigator (aka System i Navigator)
IBM i Access Windows Service Pack
7.1 – SI53809 TR9 timed enhancements
Product Download Site:www-03.ibm.com/systems/power/software/i/access/windows_sp.html
Next Planned Update…June, 2015
Right click
© 2015 IBM Corporation7 SQL for the System Admin
New to SQL? Use Navigator’s Run SQL Scripts
• Insert from Examples…
No need to start from scratch.
Save and reuse useful scripts.
© 2015 IBM Corporation8 SQL for the System Admin
SQL catalogs TCP/IP Services
TCP/IP Services
© 2015 IBM Corporation9 SQL for the System Admin
QSYS2.TCPIP_INFO – view
Client/server identification, logging and other instrumentation is possible using SQL.
The TCPIP_INFO view returns detailed information for the current connection.
SELECT * from QSYS2.TCPIP_INFO;
Columns which don’t exist on IBM i 6.1:
SERVER_IP_ADDRESS and SERVER_IP_ADDRESS_TYPE
© 2015 IBM Corporation10 SQL for the System Admin
TCPIP_INFO – example usage
• Well defined port numbers –http://www-01.ibm.com/support/knowledgecenter/ssw_ibm_i_72/rzajr/rzajrservicesandports.htm?lang=en
Now it’s possible (and easy) to deploy interface specific logic with SQL
© 2015 IBM Corporation11 SQL for the System Admin
TCPIP_INFO – Trigger example
This trigger can have conditional logic when running within a QZDASOINIT job.
Varies by the caller
© 2015 IBM Corporation12 SQL for the System Admin
SYSIBMADM.ENV_SYS_INFO – view
• SYSIBMADM/ENV_SYS_INFO catalog returns information about the current server
• This is a DB2 family compatible administrative view
© 2015 IBM Corporation13 SQL for the System Admin
SQL catalogs Program Temporary Fix (PTF)
PTF Services
© 2015 IBM Corporation14 SQL for the System Admin
Explore the catalog columns and detail…
SELECT * FROM QSYS2.GROUP_PTF_INFO;
QSYS2.GROUP_PTF_INFO – view
© 2014 IBM Corporation15 SQL for the System Admin
• Use SQL to interrogate PTF Group information• Alternative to the Work with PTF Groups (WRKPTFGRP) command• Since this command has no OUTPUT(*OUTFILE) capability, the command is not easily consumable from a program
-- What PTF Groups will change on the next IPL?SELECT * FROM QSYS2.GROUP_PTF_INFOWHERE PTF_GROUP_STATUS = ‘APPLY AT NEXT IPL’ORDER BY PTF_GROUP_LEVEL DESC;
-- What is the most recently installed CUM? SELECT MAX(PTF_GROUP_LEVEL) AS CUM_LEVEL FROM QSYS2.GROUP_PTF_INFOWHERE PTF_GROUP_NAME IN (‘SF99610’,‘SF99710’)AND PTF_GROUP_STATUS = ‘INSTALLED’;
QSYS2.GROUP_PTF_INFO – view
© 2015 IBM Corporation16 SQL for the System Admin
• Interrogate individual PTF information using SQL• Data returned is similar to the QpzListPTF() API output• Use SQL to formalize prerequisite checks within software products
-- I’m about to IPL, will the IPL impact PTF state?SELECT PTF_IDENTIFIER, PTF_IPL_ACTION, A.*FROM QSYS2.PTF_INFO AWHERE PTF_IPL_ACTION <> 'NONE‘;
QSYS2.PTF_INFO – view
© 2015 IBM Corporation17 SQL for the System Admin
-- Which PTFs are loaded, but not applied?SELECT PTF_IDENTIFIER, PTF_PRODUCT_DESCRIPTION, A.*FROM QSYS2.PTF_INFO AWHERE PTF_LOADED_STATUS = 'LOADED'ORDER BY PTF_PRODUCT_ID;
QSYS2.PTF_INFO – view
© 2015 IBM Corporation18
SYSTOOLS.GROUP_PTF_CURRENCY View
SELECT * from SYSTOOLS.GROUP_PTF_CURRENCYWHERE PTF_GROUP_RELEASE = ‘R720’ORDER BY ptf_group_level_available -ptf_group_level_installed DESC;
Current or
behind on
service?
PTF Group
Info
Level
installed
on this
partition
Level
available
from IBM
Date that
IBM last
updated
this group
© 2015 IBM Corporation19
SYSTOOLS.GROUP_PTF_CURRENCY View
http://www-912.ibm.com/s_dir/sline003.nsf/PSPbyNumL.xml?OpenView&count=500
XML
namespace
&
structure
© 2015 IBM Corporation20
SYSTOOLS.GROUP_PTF_CURRENCY View
Study the XML structure to define the data to the HTTP function.
HTTPXML document
structure
Developer
resources
TCP/IP Enablement:
‘www-912.ibm.com’ maps to 129.42.160.32
IBM i TCP/IP configuration Technote:http://www-01.ibm.com/support/docview.wss?uid=nas8N1018980
White papers:
• https://ibm.biz/XMLandDB2fori
• https://ibm.biz/HTTPandDB2fori
Enablement
© 2015 IBM Corporation21 SQL for the System Admin
SQL catalogs Journal Services
Journal Services
© 2015 IBM Corporation22 SQL for the System Admin
• Use SQL to retrieve detail for local & remote journals• Information from QjoRetrieveJournalInformation() API, RJRN0100 format.• The view returns Key 1 & 3 information, one row == information about one journal.
-- Which remote journals are the most heavily used?SELECT JOURNALED_OBJECTS, A.* FROM QSYS2.JOURNAL_INFO A WHERE NUMBER_REMOTE_JOURNALS > 0 AND JOURNALED_OBJECTS IS NOT NULL ORDER BY JOURNALED_OBJECTS DESC;-- Journals nearing the limit of journaled objects?SELECT * FROM QSYS2.JOURNAL_INFO WHERE JOURNALED_OBJECT_LIMIT = '*MAX250K' AND JOURNALED_OBJECTS > 200000;
QSYS2.JOURNAL_INFO – View
© 2015 IBM Corporation23 SQL for the System Admin
• Use this new source of information to achieve better journal management• Recognize conditions that require attention
-- Remote journals that fell behind this week?SELECT MAXIMUM_TIME_BEHIND, MAXIMUM_BEHIND_TIMESTAMP, ESTIMATED_TIME_BEHIND, TOTAL_SIZE_JOURNAL_RECEIVERS,RTRIM(ATTACHED_JOURNAL_RECEIVER_LIBRARY) CONCAT '/' CONCAT RTRIM(ATTACHED_JOURNAL_RECEIVER_NAME) AS JrnName, A.* FROM QSYS2.JOURNAL_INFO A WHEREMAXIMUM_BEHIND_TIMESTAMP > CURRENT TIMESTAMP - 7 DAYS AND MAXIMUM_TIME_BEHIND > 0 AND MAXIMUM_TIME_BEHIND IS NOT NULL ORDER BY MAXIMUM_TIME_BEHIND DESC FETCH FIRST 10 ROWS ONLY;
QSYS2.JOURNAL_INFO – View
© 2015 IBM Corporation24 SQL for the System Admin
• Use SQL to extract data from journals• Either call the UDTF directly or use IBM i Navigator
• Why use these capabilities over the green screen commands?
Isolate detail from complex data, efficiently Automate frequent audit analysis Add logic to applications Leverage the SQL Query Engine (SQE) to order, group, join, transform journal based data
QSYS.DISPLAY_JOURNAL() – UDTF
© 2015 IBM Corporation25 SQL for the System Admin
SELECT journal_code, journal_entry_type, object, object_type, X.* FROM TABLE (QSYS2.Display_Journal('PRODDATA', 'QSQJRN', -- Journal library and name'', '', -- Receiver library and nameCAST(null as TIMESTAMP), -- Starting timestampCAST(null as DECIMAL(21,0)), -- Starting sequence number'', -- Journal codes'', -- Journal entries'',‘’,'',‘', -- Object library, Object name, Object
type, Object member‘SCOTT', -- User'', -- Job'' -- Program) ) AS xORDER BY entry_timestamp DESC;
Before 7.2 (must specify 15 parameters):
With 7.2 (leverage the optional parameters):
SELECT journal_code, journal_entry_type, object, object_type, X.* FROM TABLE (QSYS2.Display_Journal('PRODDATA', 'QSQJRN', -- Journal library and name“USER” => ‘SCOTT’ -- User) ) AS xORDER BY entry_timestamp DESC;
QSYS2.Display_Journal – User Defined Table Function
© 2015 IBM Corporation26 SQL for the System Admin
-- Which rows were deleted from TOYSTORE5/SALES table this week?
select ENTRY_DATA,ENTRY_TIMESTAMP,JOURNAL_ENTRY_TYPE,COUNT_OR_RRN as ROW_DELETED,"CURRENT_USER", RTRIM(JOB_NUMBER) CONCAT '/' CONCAT RTRIM(JOB_USER) CONCAT '/' CONCAT RTRIM(JOB_NAME) AS Qualified_Job_Name from table (
QSYS2.Display_Journal(
'TOYSTORE', 'QSQJRN', -- Journal library and name
STARTING_TIMESTAMP => CURRENT TIMESTAMP - 7 DAYS,
JOURNAL_ENTRY_TYPES => 'DL',
OBJECT_LIBRARY => 'TOYSTORE',
OBJECT_NAME => 'SALES',
OBJECT_OBJTYPE => '*FILE',
OBJECT_MEMBER => 'SALES'
) ) as x
order by entry_timestamp desc ;
DISPLAY_JOURNAL() Who deleted data?
© 2015 IBM Corporation27 SQL for the System Admin
-- What data was deleted from PRODDATA/SALES table this week?
select
cast(cast(substring(entry_data,610-610+1,10) as VARCHAR(10) for bit data) as DATE) AS SALES_DATE,
cast(cast(substring(entry_data,622-610+1,15) as VARCHAR(15) for bit data) as varchar(15) ccsid 37) as SALES_PERSON,
cast(cast(substring(entry_data,638-610+1,15) as VARCHAR(15) for bit data) as varchar(15) ccsid 37) as REGION,
cast(cast(substring(entry_data,655-610+1,4) as VARCHAR(4) for bit data) as varchar(4)) as SALES
from table (
QSYS2.Display_Journal(
'TOYSTORE', 'QSQJRN', -- Journal library and name
STARTING_TIMESTAMP => CURRENT TIMESTAMP - 7 DAYS,
JOURNAL_ENTRY_TYPES => 'DL',
OBJECT_LIBRARY => 'TOYSTORE',
OBJECT_NAME => 'SALES',
OBJECT_OBJTYPE => '*FILE',
OBJECT_MEMBER => 'SALES'
) ) as x
order by entry_timestamp desc;
DISPLAY_JOURNAL() What did they delete?
© 2015 IBM Corporation28 SQL for the System Admin
Schemas Journals View Entries Enter Filter Criteria
Journal Viewer finds the file and uses the file format to decompose the data!
Navigator What data was deleted?
© 2015 IBM Corporation29 SQL for the System Admin
SQL catalogs Work Management Services
Work Management Services
© 2015 IBM Corporation30 SQL for the System Admin
QSYS2.GET_JOB_INFO() – user defined table function
• GET_JOB_INFO() accepts the jobname as input and returns a single row result
table containing detail about that job.
• Enhanced with TR8:
• Use ‘*’ to return detail on the current job
• Current (or most recent) SQL Statement text returned
-- Get job information for the current jobselect a.* from table(qsys2.get_job_info('*')) a;
-- Get job information for the current jobselect a.* from table(qsys2.get_job_info('803868/Quser/Qzdasoinit')) a;
© 2015 IBM Corporation31 SQL for the System Admin
QSYS2.SYSTEM_VALUE_INFO – view
• The new view return the same data as the Retrieve System Values (QWCRSVAL) API
• *ALLOBJ or *AUDIT special authority is required to retrieve the values for QAUDCTL,
QAUDENDACN, QAUDFRCLVL, QAUDLVL, QAUDLVL2, and QCRTOBJAUD
(‘*NOTAVL’ or -1 are returned when accessed by an unauthorized user):
Catalog definition:SYSTEM_VALUE_NAME VARCHAR(10) – Name of the system valueCURRENT_NUMERIC_VALUE BIGINT – System value when binary data is returnedCURRENT_CHARACTER_VALUE VARGRAPHIC(1280) – System value when character data is returned
-- Examine the system values related to maximums
SELECT * FROM QSYS2.SYSTEM_VALUE_INFOWHERE SYSTEM_VALUE_NAME LIKE '%MAX%'ORDER BY SYSTEM_VALUE_NAME;
© 2015 IBM Corporation32 SQL for the System Admin
QSYS2.SYSTEM_VALUE_INFO – view
DECLARE GLOBAL TEMPORARY TABLE SESSION . Remote_System_Values
( SYSTEM_VALUE_NAME,CURRENT_NUMERIC_VALUE,CURRENT_CHARACTER_VALUE )
AS (SELECT * FROM X1423P2.QSYS2.SYSTEM_VALUE_INFO) WITH DATA
WITH REPLACE;
SELECT 'LP13UT16' AS "System Name", A.SYSTEM_VALUE_NAME,A.CURRENT_NUMERIC_VALUE,A.CURRENT_CHARACTER_VALUE FROM QSYS2.SYSTEM_VALUE_INFO A
LEFT EXCEPTION JOIN SESSION.Remote_System_Values B
ON A.SYSTEM_VALUE_NAME = B.SYSTEM_VALUE_NAME AND
A.CURRENT_NUMERIC_VALUE IS NOT DISTINCT FROM B.CURRENT_NUMERIC_VALUE AND
A.CURRENT_CHARACTER_VALUE IS NOT DISTINCT FROM B.CURRENT_CHARACTER_VALUE
UNION ALL
SELECT 'X1423P2' AS "System Name", B.SYSTEM_VALUE_NAME,B.CURRENT_NUMERIC_VALUE,B.CURRENT_CHARACTER_VALUE FROM QSYS2.SYSTEM_VALUE_INFO A
RIGHT EXCEPTION JOIN SESSION.Remote_System_Values B
ON A.SYSTEM_VALUE_NAME = B.SYSTEM_VALUE_NAME AND
A.CURRENT_NUMERIC_VALUE IS NOT DISTINCT FROM B.CURRENT_NUMERIC_VALUE AND
A.CURRENT_CHARACTER_VALUE IS NOT DISTINCT FROM B.CURRENT_CHARACTER_VALUE
ORDER BY SYSTEM_VALUE_NAME;
Remote 3-part DGTT
statement
Use a query to find the SYSVAL
differences between the two systems
© 2015 IBM Corporation33 SQL for the System Admin
QSYS2.SYSTEM_VALUE_INFO – view
Example exception join output using SYSTEM_VALUE_INFO.
© 2015 IBM Corporation34 SQL for the System Admin
SQL catalogs Command Services
Librarian Services
© 2015 IBM Corporation35 SQL for the System Admin
QSYS2.LIBRARY_LIST_INFO – view
• With direct access to the library list, SQL users can tap into the library list detail to:
• Programmatically review the environment
• Know when the library list needs to be adjusted
• Derive information about the libraries
IASP Number
Schema vs Library name mapping
User vs System vs Product libraries
Above all else, order of libraries searched for unqualified objects
-- Programmatically review the library list detail for this connectionSELECT * FROM QSYS2.LIBRARY_LIST_INFO;
© 2015 IBM Corporation36 SQL for the System Admin
QSYS2.SYSCATALOGS – view
• The SYSCATALOGS catalog mirrors the Relational Database Entries
(WRKRDBDIRE)
• The catalog returns status information for Independent ASPs (iASPs)
• CATALOG_STATUS values
AVAILABLE – available for use
ACTIVE – In the process of becoming available
VARYON – About to begin the vary on process
VARYOFF – Offline, not useable
-- Query the existence & state of the local database and iASPsSELECT CATALOG_NAME, CATALOG_STATUS, CATALOG_ASPNUM FROM
QSYS2.SYSCATALOGS WHERE CATALOG_TYPE = 'LOCAL‘;
© 2015 IBM Corporation37 SQL for the System Admin
SQL catalogs Command Services
Message Handling Services
© 2015 IBM Corporation38 SQL for the System Admin
QSYS2.JOBLOG_INFO – UDTF
• Job logs contain essential information.
• To understand a failure, job log messages are reviewed
• To understand which CL commands were issued, REQUEST messages in
the job log are reviewed
• Even though trace and failure collection instrumentation exists, job logs are
frequently requested by IBM Service
• The JOBLOG_INFO() UDTF provides a new service for application developers
and system managers
• A single parameter indicates the target job.
• ‘*’ can be used to indicate use the current job as the target.
-- Find the most recently executed command in a target job
SELECT MESSAGE_TEXT FROMTABLE(QSYS2.JOBLOG_INFO('706721/SCOTTF/QPADEV0006')) AWHERE A.MESSAGE_TYPE = 'REQUEST' ORDER BY ORDINAL_POSITION DESCFETCH FIRST 1 ROW ONLY;
© 2015 IBM Corporation39 SQL for the System Admin
QSYS2.JOBLOG_INFO – UDTF
-- Automatic consumption of job log in an application
CREATE OR REPLACE PROCEDURE TOYSTORE.UPDATE_SALES
(IN P_NEW_SALES INTEGER, IN P_SALES_PERSON VARCHAR(100),
IN P_SALES_DATE DATE)
DYNAMIC RESULT SETS 1
LANGUAGE SQL MODIFIES SQL DATA SET OPTION COMMIT = *CHG
BEGIN
DECLARE C_RESULT_SET1 CURSOR WITH RETURN TO CALLER FOR SELECT MESSAGE_ID, MESSAGE_TEXT,
MESSAGE_TYPE FROM SESSION.My_Joblog;
Mainline: BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
DECLARE GLOBAL TEMPORARY TABLE My_Joblog AS
(SELECT * FROM TABLE(QSYS2.JOBLOG_INFO('*')) A)
WITH DATA WITH REPLACE;
OPEN C_RESULT_SET1;
END;
UPDATE TOYSTORE.SALES
SET SALES = SALES + P_NEW_SALES
WHERE SALES_PERSON = P_SALES_PERSON
AND SALES_DATE = P_SALES_DATE;
END Mainline;
END ;
CALL TOYSTORE.UPDATE_SALES(3, 'LUCCHESSI', '1995-12-31');
© 2015 IBM Corporation40 SQL for the System Admin
QSYS2.JOBLOG_INFO – UDTF
-- Saving job log detail to a permanent table
CREATE TABLE APPLIB.Joblog_Detail AS (SELECT QSYS2.JOB_NAME, A.* FROM TABLE(QSYS2.JOBLOG_INFO('*')) A) WITH NO DATA;
CREATE OR REPLACE PROCEDURE TOYSTORE.UPDATE_SALES(IN P_NEW_SALES INTEGER, IN P_SALES_PERSON VARCHAR(100),IN P_SALES_DATE CHAR(10))
LANGUAGE SQL MODIFIES SQL DATA
Mainline: BEGINDECLARE EXIT HANDLER FOR SQLEXCEPTIONINSERT INTO APPLIB.Joblog_Detail
SELECT QSYS2.JOB_NAME, A.* FROM TABLE(QSYS2.JOBLOG_INFO('*')) AORDER BY ORDINAL_POSITION DESCFETCH FIRST 5 ROWS ONLY;
UPDATE TOYSTORE.SALES SET SALES = SALES + P_NEW_SALESWHERE SALES_PERSON = P_SALES_PERSONAND SALES_DATE = P_SALES_DATE;
END Mainline;
© 2015 IBM Corporation41 SQL for the System Admin
QSYS2.JOBLOG_INFO – UDTF
Automatic consumption of job log in an application
call TOYSTORE.UPDATE_SALES(5, 'Frank Salesguy', '2014-1011');
SELECT A.JOB_NAME, MESSAGE_ID, MESSAGE_TEXT,MESSAGE_TOKENS,A.* from APPLIB.Joblog_Detail A ORDER BY MESSAGE_TIMESTAMP ASC;
© 2015 IBM Corporation42 SQL for the System Admin
QSYS2.REPLY_LIST_INFO – view
• One reply list handles system wide automatic response to messages
• Now, SQL can be used to:
Compare (exception join) the configuration of two machines
Determine whether a specific sequence number is already in use
Confirm whether setup is complete
• REPLY_LIST_INFO matches the Work Reply List Entry (WRKRPYLE) command
behavior of allowing *PUBLIC users to view the reply list information
-- Review reply list detail for all messages which begin with ‘CPA’
SELECT * FROM QSYS2.REPLY_LIST_INFO WHERE message_ID like 'CPA%‘;
© 2015 IBM Corporation43 SQL for the System Admin
SQL catalogs Security Services
Security Services
© 2015 IBM Corporation44 SQL for the System Admin
QSYS2.GROUP_PROFILE_ENTRIES – view
Use SQL to understand:
• What Group Profiles exist
• Which User Profiles belong to specific Group Profiles
• The text associated with the profile
Handles both:
Group profile . . . . . . . . . GRPPRF
Supplemental groups . . SUPGRPPRF
-- Examine all groups and the group membersSELECT * from qsys2.group_profile_entries;
© 2015 IBM Corporation45 SQL for the System Admin
SYSIBM.AUTHORIZATIONS – view
The AUTHORIZATIONS catalog has been extended to include a new column,
which differentiates users from groups.
The AUTHORIZATIONS view is a DB2 family compatible catalog which contains
one row for every authorization ID.
© 2015 IBM Corporation46 SQL for the System Admin
QSYS2.USER_INFO – view
• Built upon the data returned by the Retrieve User Information (QSYRUSRI) API.
• Users see the data for any *USRPRF to which they have *READ authority
• Setup details, authorizations, and more… easy access to deep details
-- Which users are having trouble signing on?
SELECT * FROM QSYS2.USER_INFO
WHERE SIGN_ON_ATTEMPTS_NOT_VALID > 0;
© 2015 IBM Corporation47 SQL for the System Admin
USER_INFO & GROUP_PROFILE_ENTRIES
• Leverage multiple services to answer more interesting questions
---- Which users have *ALLOBJ authority either directly-- or via a Group or Supplemental profile?--SELECT AUTHORIZATION_NAME, STATUS, NO_PASSWORD_INDICATOR,
PREVIOUS_SIGNON,TEXT_DESCRIPTIONFROM QSYS2.USER_INFOWHERE SPECIAL_AUTHORITIES LIKE '%*ALLOBJ%'OR AUTHORIZATION_NAME IN (
SELECT USER_PROFILE_NAMEFROM QSYS2.GROUP_PROFILE_ENTRIESWHERE GROUP_PROFILE_NAME IN (
SELECT AUTHORIZATION_NAMEFROM QSYS2.USER_INFOWHERE SPECIAL_AUTHORITIES like '%*ALLOBJ%')
)ORDER BY AUTHORIZATION_NAME;
© 2015 IBM Corporation48 SQL for the System Admin
USER_INFO & GROUP_PROFILE_ENTRIES
• Query results…
© 2015 IBM Corporation49 SQL for the System Admin
QSYS2.USER_INFO – view
• Extended to include columns that are available to users of the QSYRUSRI() API.
-- Which users are at risk of becoming disabled due to lack of use? SELECT * FROM QSYS2.USER_INFO WHERE STATUS = '*ENABLED' AND LAST_USED_TIMESTAMP IS NOT NULLORDER BY LAST_USED_TIMESTAMP ASCFETCH FIRST 20 ROWS ONLY;
New Columns
© 2015 IBM Corporation50 SQL for the System Admin
QSYS2.FUNCTION_INFO – view
• Built upon the data returned by the Retrieve Function Information
(QSYRTVFI, QsyRetrieveFunctionInformation) API
• Any user can examine the function usage identifiers.
• Only users with *SECADM user special authority can examine the function usage
configuration details returned via the QSYS2.FUNCTION_INFO catalog.
• Each row describes the function usage defined on the machine.
Example:
-- Which function usage IDs exist and what is the default configuration?
SELECT * FROM QSYS2.FUNCTION_INFO ORDER BY FUNCTION_ID;
© 2015 IBM Corporation51 SQL for the System Admin
QSYS2.FUNCTION_USAGE – view
• The detail returned corresponds to the data returned by
the Retrieve Function Usage Information
(QSYRTFUI, QsyRetrieveFunctionUsageInfo) API.
• Only users with *SECADM user special authority can examine the function usage
configuration details returned with this view. Users without *SECADM authority who attempt
to reference this view will get SQLCODE -443.
• Each row corresponds with users or groups that have function usage settings
Example:
-- What function usage has been granted or revoked?
SELECT * FROM QSYS2.FUNCTION_USAGE ORDER BY FUNCTION_ID, USER_NAME;
© 2015 IBM Corporation52 SQL for the System Admin
SQL catalogs Storage Services
Storage Services
© 2015 IBM Corporation53 SQL for the System Admin
QSYS2.USER_STORAGE – view
• Built upon the data returned by the Retrieve User Information (QSYRUSRI) API.
• Users see the data for any *USRPRF to which they have *READ authority
• User storage is broken down by SYSBAS and iASPs
-- How much storage has user SCOTTF consumed?
SELECT * FROM QSYS2.USER_STORAGE
WHERE USER_NAME = ‘SCOTTF’;
© 2015 IBM Corporation54 SQL for the System Admin
QSYS2.USER_STORAGE – view
-- Review the top 10 storage consumers
SELECT A.AUTHORIZATION_NAME, SUM(A.STORAGE_USED) AS TOTAL_STORAGE_USED, B.TEXT_DESCRIPTION, B.ACCOUNTING_CODE, B.MAXIMUM_ALLOWED_STORAGE
FROM QSYS2.USER_STORAGE A
INNER JOIN QSYS2.USER_INFO B ON B.USER_NAME = A.AUTHORIZATION_NAME
GROUP BY A.AUTHORIZATION_NAME, B.TEXT_DESCRIPTION,B.ACCOUNTING_CODE, B.MAXIMUM_ALLOWED_STORAGE
ORDER BY TOTAL_STORAGE_USED DESC FETCH FIRST 10 ROWS ONLY;
© 2015 IBM Corporation55 SQL for the System Admin
QSYS2.SYSDISKSTAT – view
The SYSDISKSTAT catalog can be used to quickly and easily understand the DISK information
using SQL.
Examples:
-- Query information for all SSD unitsSELECT * FROM QSYS2.SYSDISKSTAT WHERE UNIT_TYPE = 1;-- Query information for all DISKs, order by percentage usedSELECT PERCENT_USED, A.* FROM QSYS2.SYSDISKSTAT A
ORDER BY PERCENT_USED DESC;
© 2015 IBM Corporation56 SQL for the System Admin
• Observe System-wide Temporary storage consumption
via a new DB2 for i Service: QSYS2.SYSTMPSTG
• Read all about it in IBM Knowledge Center:www.ibm.com/support/knowledgecenter/ssw_ibm_i_72/rzajq/rzajqviewsystmpstg.htm
-- Which active jobs are the top consumers of temporary storage?
SELECT bucket_current_size, bucket_peak_size,
rtrim(job_number) concat '/' concat rtrim(job_user_name) concat '/' concat rtrim(job_name) as q_job_name
FROM QSYS2.SYSTMPSTG
WHERE job_status = '*ACTIVE'
ORDER BY bucket_current_size desc;
Improved
System Management
with DB2 for i
QSYS2.SYSTMPSTG – view
© 2015 IBM Corporation57 SQL for the System Admin
• Combine services to extract more detail
-- Which active database server connections
-- are consuming the most temporary storage
WITH TOP_TMP_STG (bucket_current_size, q_job_name) AS (
SELECT bucket_current_size, rtrim(job_number) concat '/' concatrtrim(job_user_name) concat '/' concat rtrim(job_name) as q_job_name
FROM QSYS2.SYSTMPSTG
WHERE job_status = '*ACTIVE' AND JOB_NAME IN ('QZDASOINIT', 'QZDASSINIT', 'QRWTSRVR', 'QSQSRVR')
ORDER BY bucket_current_size desc fetch first 10 rows only
) SELECT bucket_current_size, q_job_name, V_SQL_STATEMENT_TEXT, B.* FROM TOP_TMP_STG, TABLE(QSYS2.GET_JOB_INFO(q_job_name)) B;
Improved
System Management
with DB2 for i
QSYS2.SYSTMPSTG – view
© 2015 IBM Corporation58 SQL for the System Admin
SQL catalogs Command Services
Command Services
© 2015 IBM Corporation59 SQL for the System Admin
QSYS2.QCMDEXC – Procedure
The QCMDEXC procedure can be used within SQL applications to easily execute CL
Commands.
Examples:
• DECLARE V_CPYLIB_COMMAND VARCHAR(500);DECLARE V_CPYLIB_LEN DECIMAL(15, 5);SET V_CPYLIB_COMMAND = 'CPYLIB FROMLIB(' CONCAT RTRIM(V_LIBRARY) CONCAT ') TOLIB(' CONCAT RTRIM(V_NEW_LIBRARY) CONCAT ') DATA(*NO)‘;SET V_CPYLIB_LEN = CHARACTER_LENGTH(V_CPYLIB_COMMAND);CALL QSYS2.QCMDEXC(V_CPYLIB_COMMAND, V_CPYLIB_LEN);
• CALL QSYS2.QCMDEXC('ADDLIBLE PRODLIB2',17);
• Length argument no longer required:
CALL QSYS2.QCMDEXC('ADDLIBLE PRODLIB2');
© 2015 IBM Corporation60 SQL for the System Admin
DB2 for i built-in Global Variables
Built-in Global Variables
© 2015 IBM Corporation61 SQL for the System Admin
DB2 for i Built-in Global Variables
• The qualified job name of the current connection is easily accessed
• When SQL Server Mode is used, the job name of the application instance
which owns the connection is accessed through SERVER_MODE_JOB_NAME
• Use these variables to deploy advanced logic in triggers, RCAC rules, and
more
Variable name Schema Data Type Size
JOB_NAME QSYS2 VARCHAR 28
SERVER_MODE_JOB_NAME QSYS2 VARCHAR 28
CLIENT_IPADDR SYSIBM VARCHAR 128
CLIENT_HOST SYSIBM VARCHAR 255
CLIENT_PORT SYSIBM INTEGER -
PACKAGE_NAME SYSIBM VARCHAR 128
PACKAGE_SCHEMA SYSIBM VARCHAR 128
PACKAGE_VERSION SYSIBM VARCHAR 64
ROUTINE_SCHEMA SYSIBM VARCHAR 128
ROUTINE_SPECIFIC_NAME SYSIBM VARCHAR 128
ROUTINE_TYPE SYSIBM CHAR 1
New with
IBM i 7.2
SF99702
Level 3
Available
with
base
IBM i 7.2
© 2015 IBM Corporation62 SQL for the System Admin
Built-in Global Variables – Client information
SELECT SYSIBM.client_host AS CLIENT_HOST,
SYSIBM.client_ipaddr AS CLIENT_IP,
SYSIBM.client_port AS CLIENT_PORT
FROM LP92UT27.SYSIBM.SYSDUMMY1;
SELECT * FROM LP92UT27.QSYS2.TCPIP_INFO;
• Two ways to extract the detail
• Global variables fit nicely into view definitions & RCAC masks/permissions
© 2015 IBM Corporation63 SQL for the System Admin
Built-in Global Variables – Routine information
create procedure scottf.show_GV_values(
rtn_schema_p out varchar(128),
rtn_specific_name_p out varchar(128),
rtn_type_p out char(1))
language sql
specific scottf.demonstrate_the_new_DB2_for_i_GVs
begin
SET (rtn_schema_p,rtn_specific_name_p,rtn_type_p) =
(SYSIBM.ROUTINE_SCHEMA, SYSIBM.ROUTINE_SPECIFIC_NAME, SYSIBM.ROUTINE_TYPE);
End;
CALL scottf.show_GV_values(?,?,?);
Variables reflect the currently executing routine (procedure or function)
© 2015 IBM Corporation64 SQL for the System Admin
Built-in Global Variables – Routine information
create or replace function scottf.show_specific_name_function()
returns clob(1K)
language sql
deterministic
Begin
RETURN(SYSIBM.ROUTINE_SCHEMA CONCAT '.' CONCAT SYSIBM.ROUTINE_SPECIFIC_NAME CONCAT
CASE WHEN SYSIBM.ROUTINE_TYPE = 'P' THEN ' PROCEDURE'
WHEN SYSIBM.ROUTINE_TYPE = 'F' THEN ' FUNCTION'
END);
End;
values(scottf.show_specific_name_function());
Leverage this new insight within code used by procedures and functions
© 2015 IBM Corporation65 SQL for the System Admin
Built-in Global Variables – DRDA information
• When DRDA is used, the package detail is readily accessible via the new DB2
for i global variables.
• Similar to the routine variables, this can provide easy identification of remote
execution of embedded SQL programs.
SELECT SYSIBM.package_schema AS PACKAGE_SCHEMA,
SYSIBM.package_name AS PACKAGE_NAME,
SYSIBM.package_version AS PACKAGE_VERSION
FROM LP92UT27.SYSIBM.SYSDUMMY1;
© 2015 IBM Corporation66 SQL for the System Admin
SQL catalogs Landscape view
Landscape
© 2014 IBM Corporation67 SQL for the System Admin
SYSCATALOGS
INFORMATION_SCHEMA_CATALOG_NAME
Catalogs
IBM® DB2® for i Catalogs
SQLSCHEMAS
SCHEMATA
Schemas
SYSSCHEMAS
SYSCHKCST
SYSCST
SYSCSTCOL
SYSCSTDEP
SYSKEYCST
SYSREFCST
SQLFOREIGNKEYS
SQLPRIMARYKEYS
CHECK_CONSTRAINTS
REFERENTIAL_CONSTRAINTS
TABLE_CONSTRAINTS
Constraints
SYSCOLUMNS
SYSCOLUMNS2
SYSFIELDS
SYSINDEXES
SYSKEYS
SYSTABLEDEP
SYSTABLES
SYSVIEWDEP
SYSVIEWS
SQLCOLUMNS
SQLSPECIALCOLUMNS
SQLTABLES
COLUMNS
TABLES
VIEWS
Tables Views Indexes
SYSTRIGCOL
SYSTRIGDEP
SYSTRIGGERS
SYSTRIGUPD
TriggersSYSPACKAGE
SYSSEQUENCES
SYSTYPES
SYSVARIABLEDEP
SYSVARIABLES
SQLTYPEINFO
SQLUDTS
USER_DEFINED_TYPES
SEQUENCES
Miscellaneous Objects
SYSFUNCS
SYSJARCONTENTS
SYSJAROBJECTS
SYSPARMS
SYSPROCS
SYSROUTINEDEP
SYSROUTINES
SQLFUNCTIONCOLS
SQLFUNCTIONS
SQLPROCEDURECOLS
SQLPROCEDURES
PARAMETERS
ROUTINES
Routines Statistics
SYSCOLUMNSTAT
SYSINDEXSTAT
SYSMQTSTAT
SYSPACKAGESTAT
SYSPACKAGESTMTSTAT
SYSPARTITIONDISK
SYSPARTITIONINDEXES
SYSPARTITIONINDEXDISK
SYSPARTITIONINDEXSTAT
SYSPARTITIONMQTS
SYSPARTITIONSTAT
SYSPROGRAMSTAT
SYSPROGRAMSTMTSTAT
SYSTABLEINDEXSTAT
SYSTABLESTAT
SQLSTATISTICS
DB2 for i catalog views (QSYS2)
ODBC and JDBCTM
catalog views (SYSIBM)
ANS and ISO catalog views (QSYS2)
SQL_FEATURES
SQL_LANGUAGES
SQL_SIZING
CHARACTER_SETS
Database Support
XSRANNOTATIONINFO
XSROBJECTCOMPONENTS
XSROBJECTHIERARCHIES
XSROBJECTS
XML Schemas
Privileges
SQLCOLPRIVILEGES
SQLTABLEPRIVILEGES
AUTHORIZATIONS
ROUTINE_PRIVILEGES
UDT_PRIVILEGES
USAGE_PRIVILEGES
VARIABLE_PRIVILEGES
SYSCOLAUTH
SYSCONTROLS
SYSCONTROLSDEP
SYSPACKAGEAUTH
SYSROUTINEAUTH
SYSSCHEMAAUTH
SYSSEQUENCEAUTH
SYSTABAUTH
SYSUDTAUTH
SYSVARIABLEAUTH
SYSXSROBJECTAUTH
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both.
Other company, product or service names may be trademarks or service marks of others.
http://www.ibm.com/systems/i/software/db2/
© 2014 IBM Corporation68 SQL for the System Admin
SYSTOOLS.ACT_ON_INDEX_ADVICE - PROCEDURE
SYSTOOLS.HARVEST_INDEX_ADVICE - PROCEDURE
QSYS2.OVERRIDE_QAQQINI - PROCEDURE
QSYS2.RESET_TABLE_INDEX_STATISTICS - PROCEDURE
QSYS2.SYSIXADV - TABLE
SYSTOOLS.REMOVE_INDEXES - PROCEDURE
Performance Services
QSYS2.HEALTH_ACTIVITY
QSYS2.HEALTH_DATABASE_OVERVIEW
QSYS2.HEALTH_DESIGN_LIMITS
QSYS2.HEALTH_ENVIRONMENTAL_LIMITS
QSYS2.HEALTH_SIZE_LIMITS
QSYS2.RESET_ENVIRONMENTAL_LIMITS
Health Center Procedures
PTF ServicesQSYS2.PTF_INFO – VIEW
QSYS2.GROUP_PTF_INFO – VIEW
SYSTOOLS.GROUP_PTF_CURRENCY -VIEW
DB2 for i Services
IBM i Services
http://www.ibm.com/developerworks/ibmi/db2
http://www.ibm.com/developerworks/ibmi/techupdates/db2/landscape
QSYS2.CANCEL_SQL
QSYS2.DUMP_SQL_CURSORS
QSYS2.EXTRACT_STATEMENTS
QSYS2.FIND_AND_CANCEL_QSQSRVR_SQL
QSYS2.FIND_QSQSRVR_JOBS
QSYS2.GENERATE_SQL
QSYS2.RESTART_IDENTITY
SYSTOOLS.CHECK_CST
SYSTOOLS.CHECK_SYSROUTINE
Utility Procedures Security Services
QSYS2.FUNCTION_INFO – VIEW
QSYS2.FUNCTION_USAGE – VIEW
QSYS2.GROUP_PROFILE_ENTRIES – VIEW
SYSPROC.SET_COLUMN_ATTRIBUTE - PROCEDURE
QSYS2.SQL_CHECK_AUTHORITY - UDF
QSYS2.USER_INFO – VIEW
TCP/IP Services
QSYS2.TCPIP_INFO – VIEW
SYSIBMADM.ENV_SYS_INFO – VIEW
Work Management Services
QSYS2.SYSTEM_VALUE_INFO – VIEW
QSYS2.GET_JOB_INFO – UDTF
QSYS2.CHANGE_PLAN_CACHE_SIZE
QSYS2.DUMP_PLAN_CACHE
QSYS2.DUMP_PLAN_CACHE_PROPERTIES
QSYS2.DUMP_PLAN_CACHE_topN
QSYS2.DUMP_SNAP_SHOT_PROPERTIES
QSYS2.END_ALL_PLAN_CACHE_EVENT_MONITORS
QSYS2.END_PLAN_CACHE_EVENT_MONITOR
QSYS2.START_PLAN_CACHE_EVENT_MONITOR (2)
Plan Cache Procedures
System Health ServicesQSYS2.SYSLIMTBL - TABLE
QSYS2.SYSLIMITS – VIEWQSYS2.OVERRIDE_TABLE – PROCEDURE
QSYS2.DELIMIT_NAME – UDF
SYSPROC.WLM_SET_CLIENT_INFO – PROCEDURE
Application Services
Journal ServicesQSYS2.DISPLAY_JOURNAL – UDTF
QSYS2.JOURNAL_INFO – View
Storage Services
QSYS2.SYSDISKSTAT – VIEW
QSYS2.SYSTMPSTG – VIEW
QSYS2.USER_STORAGE – VIEW
Object Services
QSYS2.OBJECT_STATISTICS – UDTF
IBM® DB2® for i Services
QSYS2.QCMDEXC - PROCEDURE
Librarian Services
QSYS2.LIBRARY_LIST_INFO – View
Message Handling Services
QSYS2.JOBLOG_INFO – View
QSYS2.REPLY_LIST_INFO – View
© 2015 IBM Corporation69 SQL for the System Admin
Documentation
• With IBM i 7.2, these and other
services are documented within the
“Database Performance and
Optimization” book
• The Technology Updates wiki
includes fact pages for these
services
http://ibm.biz/DB2foriServices
© 2015 IBM Corporation70 SQL for the System Admin
This document was developed for IBM offerings in the United States as of the date of publication. IBM may not make these offerings available in
other countries, and the information is subject to change without notice. Consult your local IBM business contact for information on the IBM
offerings available in your area.
Information in this document concerning non-IBM products was obtained from the suppliers of these products or other public sources. Questions
on the capabilities of non-IBM products should be addressed to the suppliers of those products.
IBM may have patents or pending patent applications covering subject matter in this document. The furnishing of this document does not give
you any license to these patents. Send license inquires, in writing, to IBM Director of Licensing, IBM Corporation, New Castle Drive, Armonk, NY
10504-1785 USA.
All statements regarding IBM future direction and intent are subject to change or withdrawal without notice, and represent goals and objectives
only.
The information contained in this document has not been submitted to any formal IBM test and is provided "AS IS" with no warranties or
guarantees either expressed or implied.
All examples cited or described in this document are presented as illustrations of the manner in which some IBM products can be used and the
results that may be achieved. Actual environmental costs and performance characteristics will vary depending on individual client configurations
and conditions.
IBM Global Financing offerings are provided through IBM Credit Corporation in the United States and other IBM subsidiaries and divisions
worldwide to qualified commercial and government clients. Rates are based on a client's credit rating, financing terms, offering type, equipment
type and options, and may vary by country. Other restrictions may apply. Rates and offerings are subject to change, extension or withdrawal
without notice.
IBM is not responsible for printing errors in this document that result in pricing or information inaccuracies.
All prices shown are IBM's United States suggested list prices and are subject to change without notice; reseller prices may vary.
IBM hardware products are manufactured from new parts, or new and serviceable used parts. Regardless, our warranty terms apply.
Any performance data contained in this document was determined in a controlled environment. Actual results may vary significantly and are
dependent on many factors including system hardware configuration and software design and configuration. Some measurements quoted in this
document may have been made on development-level systems. There is no guarantee these measurements will be the same on generally-
available systems. Some measurements quoted in this document may have been estimated through extrapolation. Users of this document
should verify the applicable data for their specific environment.
Special notices
© 2015 IBM Corporation71 SQL for the System Admin
IBM, the IBM logo, ibm.com AIX, AIX (logo), AIX 6 (logo), AS/400, BladeCenter, Blue Gene, ClusterProven, DB2, ESCON, i5/OS, i5/OS (logo), IBM Business Partner
(logo), IntelliStation, LoadLeveler, Lotus, Lotus Notes, Notes, Operating System/400, OS/400, PartnerLink, PartnerWorld, PowerPC, pSeries, Rational, RISC
System/6000, RS/6000, THINK, Tivoli, Tivoli (logo), Tivoli Management Environment, WebSphere, xSeries, z/OS, zSeries, AIX 5L, Chiphopper, Chipkill, Cloudscape, DB2
Universal Database, DS4000, DS6000, DS8000, EnergyScale, Enterprise Workload Manager, General Purpose File System, , GPFS, HACMP, HACMP/6000, HASM, IBM
Systems Director Active Energy Manager, iSeries, Micro-Partitioning, POWER, PowerExecutive, PowerVM, PowerVM (logo), PowerHA, Power Architecture, Power
Everywhere, Power Family, POWER Hypervisor, Power Systems, Power Systems (logo), Power Systems Software, Power Systems Software (logo), POWER2,
POWER3, POWER4, POWER4+, POWER5, POWER5+, POWER6, POWER6+, System i, System p, System p5, System Storage, System z, Tivoli Enterprise, TME 10,
Workload Partitions Manager and X-Architecture 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 ™), 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
The Power Architecture and Power.org wordmarks and the Power and Power.org logos and related marks are trademarks and service marks licensed by Power.org.
UNIX is a registered trademark of The Open Group in the United States, other countries or both.
Linux is a registered trademark of Linus Torvalds in the United States, other countries or both.
Microsoft, Windows and the Windows logo are registered trademarks of Microsoft Corporation in the United States, other countries or both.
Intel, Itanium, Pentium are registered trademarks and Xeon is a trademark of Intel Corporation or its subsidiaries in the United States, other countries or both.
AMD Opteron is a trademark of Advanced Micro Devices, Inc.
Java and all Java-based trademarks and logos are trademarks of Sun Microsystems, Inc. in the United States, other countries or both.
TPC-C and TPC-H are trademarks of the Transaction Performance Processing Council (TPPC).
SPECint, SPECfp, SPECjbb, SPECweb, SPECjAppServer, SPEC OMP, SPECviewperf, SPECapc, SPEChpc, SPECjvm, SPECmail, SPECimap and SPECsfs are
trademarks of the Standard Performance Evaluation Corp (SPEC).
NetBench is a registered trademark of Ziff Davis Media in the United States, other countries or both.
AltiVec is a trademark of Freescale Semiconductor, Inc.
Cell Broadband Engine is a trademark of Sony Computer Entertainment Inc.
InfiniBand, InfiniBand Trade Association and the InfiniBand design marks are trademarks and/or service marks of the InfiniBand Trade Association.
Other company, product and service names may be trademarks or service marks of others.
Special notices (cont.)