14
Thu, Nov
5 New Articles

TechTip: Using SQL Client Special Registers in DB2 Web Query

SQL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Leverage DB2 Web Query and SQL client special registers.

 

In V6R1 of the IBM i operating system, support was added for the specification (and subsequent retrieval) of several new special registers that applications can use to identify what jobs, users, programs, and applications are issuing SQL statements to the DB2 for i engine. Currently, there are five such registers you can use:

 

  • CLIENT_APPLNAME—Client Application Name
  • CLIENT_ACCTNG—Client accounting
  • CLIENT_PROGRAMID—Client program ID
  • CLIENT_USERID—Client User ID
  • CLIENT_WRKSTNNAME—Client Workstation

The good news is that you can use these special registers for your own applications. The better news for customers using IBM DB2 Web Query for i is that this product now uses these registers to identify all SQL requests it submits to the database engine. The values specified by DB2 Web Query for each of these special registers is provided in the table below.

 

SQL Special Register Name

DB2 Web Query Value

CLIENT_USERID

User profile logged into DB2 Web Query that ran the report. For example: COBBG

CLIENT_WRKSTNNAME

Job name, user profile, and job number that handled the DB2 Web Query request.

 

For example: 678182/QWEBQRYADM/TSCOM3

 

CLIENT_APPLNAME

DB2 WebQuery

CLIENT_ACCTNG

User profile logged into DB2 Web Query that ran the report. For example: COBBBG

CLIENT_PROGRAMID

DB2WBQRY

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

If you are using DB2 Web Query today, you may be thinking, "OK, sounds great. But how can I use this feature?" A couple of ideas jump to the forefront:

  • Selective preprocessing of DB2 Web Query requests using QIBM_QSQ_CLI_CONNECT, the CLI connect exit point
  • Database analysis of requests coming from DB2 Web Query

Selective Preprocessing of DB2 Web Query Requests

Once the registers are specified by DB2 Web Query, this information can be retrieved in your own programs to determine where the request came from. If you've read my previous articles, you may remember a couple that discuss the use of the QIBM_QSQ_CLI_CONNECT, the CLI connect exit point:

 

"Making Better Use of DB2 Web Query with *LIBL"

"Maximize SQL Query Engine (SQE) Usage of Your DB2 Web Query Reports"

 

Registration of this exit point means that you can call a user-written program whenever a CLI connect event occurs in server mode (which happens to be every time DB2 Web Query submits an SQL request to DB2). This preprocessing gives you the ability to do some useful things before the report runs, such as dynamically changing the library list or adding a row to an audit table. One of the drawbacks of this exit point is that you can register only one program to it. This means that if you have multiple CLI applications, the exit point will be activated for all SQL requests coming from CLI applications running in server mode (not just DB2 Web Query). Consequently, it can be quite difficult to determine where each request is originating from. If you want to change the library list only for DB2 Web Query requests (not for an order entry application using CLI), how can you do this? Well, now you can use the information in the special registers! Here's an example snippet from an RPGLE program that uses embedded SQL to make this determination:

 


// First make sure that this is a Web DB2 Query request. If not, exit program exec sql
SELECT CURRENT CLIENT_PROGRAMID INTO :pgmID FROM sysibm/sysdummy1;
if (pgmID <> 'DB2WBQRY');
return;

endif;


In this particular example, if the request did not originate from DB2 Web Query, the program ends and no further processing occurs. When retrieving the value of the registers, it does not matter what table you query; therefore, it is recommended that you specify one with a single row, such as the SYSIBM/SYSDUMMY1 table (which should exist on all IBM i systems). Simply add this type of conditional logic at the beginning of the exit point program and take the appropriate action: exit or branch off to the necessary program, subprocedure, subroutine, or section of code.

Analyzing Requests from DB2 Web Query

If you want to determine what applications are making SQL requests or even isolate those requests coming from DB2 Web Query to a gauge and/or analyze their impact on database resources, these special registers make it a breeze! You have a couple of options here:

  • Plan Cache Snapshot—If you're confident that your requests are being processed by the SQL Query Engine (SQE)—not the Classic Query Engine (CQE)—simply create an SQL plan cache snapshot from System i Navigator. This action will materialize all of the database request information currently in the plan cache and place it into a DB2 table. Once it's in a table, you can query it!
  • SQL Performance Monitor—Alternatively, start an SQL performance monitor trace (prior to the execution of the DB2 Web Query reports). This will capture all requests—SQE and CQE alike. So if you are not sure if all your requests are being processed by SQE, the performance monitor would be all encompassing and the safer way to go. Just make sure you turn it off once you have completed your analysis as it can be resource-intensive. While it's running, the monitor will dump all of the collected information into a DB2 table, so just as with the plan cache snapshot,
    you query the table.

Helpful hint: To reduce the strain on system resources, specify a pre-filter for the performance monitor so that it only collects information from a specific program ID. Depending on the IBM i OS level, use one of these commands to specify a pre-filter and only collect DB2 Web Query SQL requests.


For V6R1:
STRDBMON OUTFILE(QGPL/DB2WQFILE) OUTMBR(*FIRST *REPLACE)
JOB(*ALL/*ALL/*ALL) TYPE(*DETAIL)
COMMENT('FTRCLTPGM(DB2WBQRY)')

 

For V7R1:
STRDBMON OUTFILE(QGPL/DB2WQFILE) OUTMBR(*FIRST *REPLACE)
JOB(*ALL/*ALL/*ALL) TYPE(*DETAIL)FTRCLTPGM(DB2WBQRY)

 

Regardless of the method chosen, the SQL statement to query the results is basically the same. The following shows each register and corresponding column name of the table containing the collected data.

 

SQL Special Register Name

Database Monitor/Snapshot
Column Name

CLIENT_USERID

QVC3002

CLIENT_WRKSTNNAME

QVC3003

CLIENT_APPLNAME

QVC3001

CLIENT_ACCTNG

QVC3005

CLIENT_PROGRAMID

QVC3006

 

 

 

 

 

 

 

 

Use the following statement and specify the name of the table that contains the collected data:


SELECT

qqtime AS time ,

qvc3002 AS client_userid ,

qvc3003 AS client_wkstnname ,

qvc3001 AS client_applname ,

qvc3005 AS client_acctng,

qvc3006 AS client_programid,

qq1000 AS statement

 

FROM db2wqfile

WHERE qqrid = 1000

AND qvc3006 = 'DB2WBQRY'

ORDER BY qqtime;

 

Note: At the risk of stating the obvious, if you started an SQL performance monitor using the pre-filter described previously, it would not be necessary to include this portion in the WHERE clause:


AND qvc3006 = 'DB2WBQRY'

This is because the pre-filter would have only collected information from that program ID.

 

Using the above statement, here is an example result set:

 

040811CobbFigure1

Figure 1: This is the type of result set you can expect. (Click images to enlarge.)

 

If you really wanted to analyze a particular query by using the Visual Explain tool, simply show the statements in the snapshot or the SQL performance monitor, find the statement, and select Visual Explain. This process is shown in the examples below.

 

040811CobbFigure2

Figure 2: System i Navigator's Visual Explain tool helps you analyze queries.

 

040811CobbFigure3

Figure 3: You can drill down for details.

 

One last idea for querying this data: use DB2 Web Query and create a Web Query report of Web Query requests! An example report is shown below.

 

040811CobbFigure4

Figure 4: Get reports about queries that have been made.

Prerequisites

As mentioned previously, support for the SQL special registers started with V6R1 of the IBM i operating system. The DB2 Web Query use of these special registers is supported only for version 1.1.2 of 5733QU2 and you must have level 2 or higher of the DB2 Web Query group PTF installed. The group PTFs are different for each IBM i operating system release. The specific information for which PTF you'll need is provided below.

 

  • V6R1 - SF99636
  • V7R1 - SF99637

Register Your Own Applications!

Keep in mind that there is no magic going on here: the use of these special registers is not exclusive to DB2 Web Query, IBM i products, or internal processes! If your applications use SQL to access the data and are running on V6R1 of the IBM i operating system, they too can use these special registers to identify where the SQL requests originated from! All you have to do is call the WLM_SET_CLIENT_INFO stored procedure (specifying the values of the registers as input parameters) in your application prior to the execution of your SQL statements (within the same job, of course). Simply follow the format provided in the following example:

CALL SYSPROC.WLM_SET_CLIENT_INFO (

your_client_userid,

your_client_wrkstnname,

your_client_applname,

your_client_acctng,

your_client_programid);

 

Note: When using the WLM_SET_CLIENT_INFO stored procedure, you can allow any of the client registers to remain unchanged by specifying NULL for the appropriate parameter.

 

If you would like to learn more about SQL special registers, refer to these links:

 

http://www.ibmsystemsmag.com/ibmi/april09/technicalcorner/24804p1.aspx

 

http://ibmsystemsmag.blogs.com/i_can/database/

as/400, os/400, iseries, system i, i5/os, ibm i, power systems, 6.1, 7.1, V7, V6R1

Gene Cobb

Gene Cobb is a DB2 for i5/OS Technology Specialist in IBM's ISV Business Strategy & Enablement for System i group. He has worked on IBM midrange systems since 1988, including over 10 years in the IBM Client Technology Center (now known as IBM Systems and Technology Group Lab Services). While in Lab Services, he assisted customers with application design and development using RPG, DB2 for i5/OS, CallPath/400, and Lotus Domino. His current responsibilities include providing consulting services to System i developers, with a special emphasis in application and database modernization. He can be reached at This email address is being protected from spambots. You need JavaScript enabled to view it..

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: