![]() |
Db2 log files and data on IBM COS |
Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts
Friday, January 17, 2025
Once again: Db2 External Tables
Labels:
administration,
data in action,
database,
IBM,
ibmcloud,
IT,
knowledge center,
sql,
version 12
Wednesday, August 28, 2024
A look at local external tables in Db2
![]() |
Surprising Db2 results or not? |
Labels:
administration,
data in action,
DB2,
developer,
IBM,
IT,
lakehouse,
sql,
storage,
version 11.5
Tuesday, July 16, 2024
About BIRD, SQL, IBM granite models, and your business reporting
![]() |
BIRD-SQL benchmark |
Wednesday, June 17, 2020
Getting started with Db2 on Cloud Lite
It seems to me that during the COVID-19 pandemic there are more hackathons than usual. Great to see that IBM Cloud is the platform of choice for many solutions. Db2 on Cloud is used as database, often the free Lite plan to get started. That's the reason I published a quick guide on the IBM Cloud blog on how to use Db2 on Cloud Lite plan for hackathons.
Monday, May 18, 2020
Some advanced SQL to analyze COVID-19 data
![]() |
Learn to write SQL |
Monday, January 27, 2020
25th meeting of German Db2 User Group
![]() |
Celebrating the 25th DeDUG meeting |
Labels:
cloud,
data in action,
database,
DB2,
dedug,
IBM,
idug,
IT,
performance,
sql,
user group
Tuesday, November 26, 2019
ETL in the cloud using SQL Query and Db2
![]() |
From COS to Db2 using SQL |
Wednesday, June 12, 2019
After IDUG is before IDUG: Back from Db2 conference in Charlotte, NC
Last week the IDUG NA 2019 was held in Charlotte, NC. It featured 8 parallel tracks dedicated to Db2 for z/OS and Db2 for LUW and emerging technologies. In addition, two tracks for hands-on labs and workshops were offered. Personally, I delivered three presentations, moderated and attended sessions, and was active on Twitter. So what was on? Continue after the snap of Twitter photos from last week.
![]() |
Twitter photo feed from IDUG NA 2019 |
Saturday, April 27, 2019
Db2: SQL-based explain and printed acccess plan
![]() |
Vote for this Db2 idea |
Friday, September 21, 2018
More tricks for building chatbots with IBM Watson Assistant
![]() |
Have you heard? New tips and tricks! |
Friday, April 27, 2018
Db2: CTE and CONNECT BY - two kinds of recursion
![]() |
Writing recursive SQL |
If you had to write recursive queries, e.g., to create a bill of material or to find dependencies in data, you would typically use a SELECT statement with a common table expression (CTE). CTEs are easily spotted by starting with the keyword WITH. Some form of temporary table is defined, then the initial data (seed) is specified followed by UNION ALL and the SELECT for the recursive part. A regular fullselect is then applied to the result of that computation.
The CONNECT BY provides another syntax for hiearachical and recursive queries. It was first introduced by Oracle and made its way into Db2 as part of the SQL compatibility features. It allows to write a, more or less, regular SELECT statement followed by a START WITH clause to define the initial data (see) and a CONNECT BY clause to specify the recursion. There are some additional keywords to access data from the prior step, the root ancestor or the (string) path from the root to a node. It is quite powerful and often shorter than a CTE. The only drawback is that you have to enable it in the Db2 compatibility vector.
The following two queries return the same result. It is the organizational structure of the departments in the SAMPLE database. The first query uses CONNECT BY and is directly taken out of the Db2 documentation.
SELECT LEVEL, CAST(SPACE((LEVEL - 1) * 4) || '/' || DEPTNAME AS VARCHAR(40)) AS DEPTNAME FROM DEPARTMENT START WITH DEPTNO = 'A00' CONNECT BY NOCYCLE PRIOR DEPTNO = ADMRDEPT
The second query is my version of the above with a CTE. As you can see, it is longer. Readability depends on what you are used to.
WITH tdep(level, deptname, deptno) as (
SELECT 1, CAST( DEPTNAME AS VARCHAR(40)) AS DEPTNAME, deptno
FROM department
WHERE DEPTNO = 'A00'
UNION ALL
SELECT t.LEVEL+1, CAST(SPACE(t.LEVEL * 4) || '/' || d.DEPTNAME
AS VARCHAR(40)) AS DEPTNAME, d.deptno
FROM DEPARTMENT d, tdep t
WHERE d.admrdept=t.deptno and d.deptno<>'A00')
SELECT level, deptname
FROM tdep
If you only knew one style of recursive query then it is time to try out the other. Both queries above work with the SAMPLE database. If you have feedback, suggestions, or questions about this post, please reach out to me on Twitter (@data_henrik) or LinkedIn.
Monday, September 25, 2017
2017 IDUG Db2 Tech Conference in Lisbon, Portugal
![]() |
IDUG EMEA 2017 Conference |
Tuesday, September 12, 2017
Db2 with External Tables: First Tests
![]() |
External Tables - New in Db2 |
Tuesday, January 10, 2017
DB2 Quiz for the Resource-Minded & IDUG EMEA 2017
![]() |
Did you know...? |
Speaking of learning new stuff. Do you know which DB2 function or procedure produced the following output on my system? As you may notice, I checked some DB2 system processes using SQL. The feature I am using is around since DB2 version 9.7.
If you have a guess, leave a comment or send an email.
MEMBER DB2_PROCESS_NAME DB2_PROCESS_ID CPU_USER CPU_SYSTEM
------ ----------------- -------------------- ------------ ------------
0 db2fmp 15234 6 7
0 db2vend (PD Vendo 15064 5 3
0 db2ckpwd 0 15060 0 0
0 db2ckpwd 0 15061 0 0
0 db2ckpwd 0 15062 0 0
0 db2sysc 0 15054 91 128
...
8 record(s) selected.
Thursday, October 6, 2016
Easy to identify: Does the table have a primary key?
![]() |
Primary Key |
For performance reasons almost all database systems use an unique index to implement a primary key. So the key (pun intended) is to look for such an index. Both DB2 for Linux, UNIX, and Windows (LUW) and DB2 for z/OS store information about indexes in a system table SYSIBM.SYSINDEXES. On DB2 for z/OS that table is exposed to the user and documented here. DB2 LUW has catalog views on top and the view to use is named SYSCAT.INDEXES, however querying the table still works:
SELECT COLNAMES
FROM SYSIBM.SYSINDEXES
WHERE TBNAME = 'MYTABLE'
AND UNIQUERULE = 'P'
The query returns the columns on which the primary key is defined for the table MYTABLE. As can be seen in the documentation, the UNIQUERULE provides information about whether the index is an index with duplicates, an unique index, or it is used to implement a primary key (value P). On DB2 LUW we could write the query utilizing the catalog view SYSCAT.INDEXES. The following query returns the table name and schema as well as the column names for all tables which have a primary key defined:
SELECT TABNAME, TABSCHEMA,COLNAMES
FROM SYSCAT.INDEXES
WHERE UNIQUERULE='P'
So the key to quickly working with primary keys are indexes and their metadata...
Tuesday, March 1, 2016
Mom, I joined the cloud! (or: Use old stuff with new stuff - DB2 federation)
Everybody is talking about Hybrid Clouds, combining on-premises resources like database systems and ERMs with services in the public or dedicated cloud. Today I am showing you exactly that, how I combined my on-prem DB2 with a cloud-based DB2 that I provisioned via Bluemix. The interesting thing is that really old technology can be used for that purpose: database federation. So relax, sit back, and follow my journey in joining the cloud...
For my small adventure I used a local DB2 10.5 and a Bluemix-based SQLDB service. The steps I followed are an extended version of what I wrote in 2013 about using three-part names in DB2 to easily access Oracle or DB2 databases. Smilar to the entry I started by enabling my DB2 instance for Federation (FEDERATED is the configuration parameter).
[hloeser@mymachine] db2 update dbm cfg using federated yes
DB20000I The UPDATE DATABASE MANAGER CONFIGURATION command completed
successfully.
![]() |
Database Services in the Bluemix Catalog |
For my small adventure I used a local DB2 10.5 and a Bluemix-based SQLDB service. The steps I followed are an extended version of what I wrote in 2013 about using three-part names in DB2 to easily access Oracle or DB2 databases. Smilar to the entry I started by enabling my DB2 instance for Federation (FEDERATED is the configuration parameter).
[hloeser@mymachine] db2 update dbm cfg using federated yes
DB20000I The UPDATE DATABASE MANAGER CONFIGURATION command completed
successfully.
Monday, January 25, 2016
A Cache of Identities and a Sequence of Events
![]() |
Bits and Bytes of Sequences |
Monday, December 7, 2015
MySQL-Style LIMIT and OFFSET in DB2 Queries
I was recently asked whether DB2 support MySQL-style syntax to page through query result sets. The good news is that DB2 supports LIMIT and OFFSET in addition to its own syntax. The only drawback is that the extra syntax supports needs to be enabled before it can be used. In the following I am going to show you how.
To support features offered by other database systems like Oracle, Microsoft SQL Server, Sybase, or MySQL that are incompatible with existing DB2 features, the DB2_COMPATIBILITY_VECTOR registry variable was introduced in DB2 version 9.7. The vector is used to selectively enable features and there are predefined settings named ORA (enable all Oracle features), SYB (Sybase), and MYS (MySQL). With that background knowledge we can directly get started on enabling LIMIT and OFFSET.
![]() |
MySQL syntax in DB2 |
To support features offered by other database systems like Oracle, Microsoft SQL Server, Sybase, or MySQL that are incompatible with existing DB2 features, the DB2_COMPATIBILITY_VECTOR registry variable was introduced in DB2 version 9.7. The vector is used to selectively enable features and there are predefined settings named ORA (enable all Oracle features), SYB (Sybase), and MYS (MySQL). With that background knowledge we can directly get started on enabling LIMIT and OFFSET.
Friday, October 30, 2015
DB2, Halloween, and a Time Saver
A roadblock? |
DB2 has the function TIMESTAMP_FORMAT (with synomy TO_DATE and TO_TIMESTAMP) to convert string values to timestamps or dates. The expected date and time format can be specified via the usual placeholders, e.g., MM for months or SS for seconds. However, there are some tricky parts to it and I am going to treat you with a solution (this was the "trick & treat" Halloween reference...):
db2 "values to_date('30.10.2015 00:00:00','DD.MM.YYYY HH:MI:SS')"
SQL20448N "31.10.2015 00:00:00" cannot be interpreted using format string
"DD.MM.YYYY HH:MI:SS" for the TIMESTAMP_FORMAT function. SQLSTATE=22007
db2 "values timestamp_format('30.10.2015 24:00:00','DD.MM.YYYY HH:MI:SS')"
SQL20448N "31.10.2015 24:00:00" cannot be interpreted using format string
"DD.MM.YYYY HH:MI:SS" for the TIMESTAMP_FORMAT function. SQLSTATE=22007
In both cases I am trying to convert midnight on October 31st to an internal timestamp value. Both calls return an error. The reason is that "HH" is the same as "HH12", the 12-hour format. Using "HH24" (24-hour format) everything is ok:
db2 "values timestamp_format('31.10.2015 00:00:00','DD.MM.YYYY HH24:MI:SS')"
1
--------------------------
2015-10-31-00.00.00.000000
1 record(s) selected.
db2 "values timestamp_format('31.10.2015 24:00:00','DD.MM.YYYY HH24:MI:SS')"
1
--------------------------
2015-10-31-24.00.00.000000
1 record(s) selected.
If you want to use the HH/HH12 format, you need to specify where to expect the so-called Meridian indicator (AM/PM):
db2 "values timestamp_format('31.10.2015 12:00:00am','DD.MM.YYYY HH:MI:SSAM')"
1
--------------------------
2015-10-31-00.00.00.000000
1 record(s) selected.
db2 "values timestamp_format('31.10.2015 12:00:00am','DD.MM.YYYY HH:MI:SSPM')"
1
--------------------------
2015-10-31-00.00.00.000000
1 record(s) selected.
That's all, enjoy the weekend and Halloween...
Monday, September 28, 2015
Altering tables in the SQLDB service on Bluemix
My blog has been quiet for some weeks thanks to some offline vacation and due to some business trips. Last week I first spoke about DB2 at a conference, then coached developers at a Bluemix hackathon. At that Bluemix event I was asked whether it would be possible to change some column definitions for an existing table. It was easy to say "absolutely", but it turned out to be harder than thought because at first I couldn't how to do it. So let's document it... :)
Once you have provisioned a SQLDB database service on Bluemix and launch the administration console, you are greeted by the "Getting Started" screen prominently featuring the following three choiced:
The "Run Queries" button is intended to perform SQL SELECTs, nothing else.
Once you have provisioned a SQLDB database service on Bluemix and launch the administration console, you are greeted by the "Getting Started" screen prominently featuring the following three choiced:
![]() |
IBM SQL Database: Getting Started |
The "Run Queries" button is intended to perform SQL SELECTs, nothing else.
Subscribe to:
Posts (Atom)