22
Sun, Dec
3 New Articles

TechTip: Unraveling SQL Error Messages Using SELF

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

The SQL Error Logging Facility (SELF) is a new tool that can help you find and understand SQL errors and warnings.

SQL has become the standard method for communication with your database. You can use SQL to create and modify your database tables, insert or update data in your table, or access data in your database. More-complex SQL features allow you to create triggers, procedures, and functions to perform more-complicated operations. Additionally, IBM i SQL services provide you with new ways of interacting with everything IBM i that isn’t the database. However, if an error occurs while using SQL to perform these tasks, it can cause significant problems in your applications. It thus becomes very important to proactively find and fix these errors before they become problems.

When you are using SQL and a problem occurs, either an SQL error message or SQL warning message is returned to your application. These errors and warnings contain an SQLSTATE, a corresponding SQLCODE, and text that describes the problem that occurred. For example, if you are attempting to insert into a table, and the table is locked by another job, a message is returned with an SQLCODE of SQL0913, SQLSTATE 57033, and a message stating, “Row or object MY_TABLE in MY_LIBRARY type *FILE in use.”

These messages can be investigated and the problem fixed; however, it can be easy to overlook errors, especially when the problem you are trying to understand is intermittent. Sometimes just finding the problem is the first hurdle to clear before you can begin to understand why the SQL error or warning occurred. We cannot fix a problem until we find it and understand it.

The SQL Error Logging Facility (SELF) is a new tool that can help you find and understand SQL errors and warnings. SELF can capture additional detail about specific SQL errors and warnings when they occur. Information that is collected when an SQL error or warning occurs includes the SQLCODE, SQLSTATE, failing statement text, information about the program that took the error, when it occurred, the job it occurred in, the current user, the stack at the time of the failure, and much, much more.

To use SELF, you first must identify which error messages and warning message you want to gather information for. Once you’ve identified the messages, determine those messages’ corresponding SQLCODEs. An error SQLCODE starts with a negative sign (-). A warning SQLCODE starts with a plus sign (+). The listing of SQL messages (https://www.ibm.com/docs/en/i/7.5?topic=codes-listing-sql-messages) can be used to identify the SQLCODE that matches a specific message. For the intermittent lock failure described earlier, search the listing of SQL messages for SQL0913. This will return a table that defines the corresponding SQLCODE as -913. SELF allows you to identify one or multiple SQLCODEs to gather information for.

Once you have identified the SQLCODEs you want to gather information about, simply configure SELF to collect information when it encounters one of those SQLCODEs. In order to do this configuration, set the SYSIBMADM.SELFCODES global variable to the SQLCODES you want to gather information about. The SELFCODES global variable can be set for a single job or for the entire system.

Let’s look at how we would set it for a single job. To enable SELF in a single job, set the SELFCODES global variable in that job. For example, if we want to monitor for the SQL0913 in our job, we would set SELFCODES in the job by running the following SQL SET statement.

SET SYSIBMADM.SELFCODES = SYSIBMADM.VALIDATE_SELF('-913');

The VALIDATE_SELF scalar function is used to ensure that the string being used to set SELFCODES is valid.

Once the value is set, you can run the application that contains the problematic SQL statements. SELF will collect information at the point where the failure occurred and log that information. After the information is collected, you can analyze it more fully by querying the QSYS2.SQL_ERROR_LOG view. This information can help you to pinpoint where and when the problem occurred. To query the SQL_ERROR_LOG, run the following query:

SELECT LOGGED_SQLCODE,
       STATEMENT_TEXT,
       LOGGED_TIME,
       JOB_NAME
FROM QSYS2.SQL_ERROR_LOG
ORDER BY LOGGED_TIME DESC;

This query returns the following:

 

LOGGED_SQLCODE

STATEMENT_TEXT

LOGGED_TIME

JOB_NAME

-913

SELECT * FROM LIBA.T1

2023-02-22 14:30:45.022000

037158/QUSER/QZDASOINIT

Many more columns can queried, including LOGGED_SQLSTATE, NUMBER_OCCURRENCES, STATEMENT_OPERATION, STATEMENT_OPERATION_DETAIL, REASON_CODE, PROGRAM_LIBRARY, PROGRAM_NAME, PROGRAM_TYPE, MODULE_NAME, ADOPTED_USER_NAME, USER_NAME, SYSTEM_USER_NAME, CLIENT_ACCTNG, CLIENT_APPLNAM, CLIENT_PROGRAMID, CLIENT_USERID, CLIENT_WRKSTNNAME, RDB_NAME, INITIAL_LOGGED_TIME, INITIAL_JOB_NAME, INITIAL_ADOPTED_USER_NAME, and INITIAL_STACK.

You can also set the SELFCODES for all user jobs. This is useful when there are errors or warnings that you always want to gather information about—for example, authorization failures. This is done by setting the default value for SELFCODES. The first time you run an SQL statement in a job, the SEFLCODES global variable in the job is set to SELFCODES default value. If you change the default value for the global variable, then all jobs that start after the change is made will use the new value. Use the CREATE OR REPLACE VARIABLE statement to change the default value for SELFCODES. If I wanted all jobs to gather information when the job encounters either an SQL0551 “Not authorized to object &1 in &2 type *&3” or an SQL0552 “Not authorized to &1”, I would run the following SQL statement:

CREATE OR REPLACE VARIABLE SYSIBMADM.SELFCODES VARCHAR(256)

DEFAULT '551, 552, -551, -552';

SELF is very easy to use and, importantly, safe to run in production. This is because SELF reacts only when it encounters an SQLCODE that you requested to monitor for. It does not have any performance impacts on SQL statements that run successfully or on statements that fail with an SQLCODE that does not match with the ones being monitored for.

For more information about SELF, visit https://www.ibm.com/docs/en/i/7.5?topic=tools-sql-error-logging-facility-self. SELF is available in IBM i 7.5 TR1 and IBM i 7.4 TR7.

 

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: