Skip to content

Instantly share code, notes, and snippets.

View dancarlosgabriel's full-sized avatar

Reynaldo Dandreb M. Medilla dancarlosgabriel

  • Moorepay/Zellis, Ceridian/Ascender, Frontier Communications/Trbhi, NGA/Weserve Fujitsu,Cargill,BDO,Honda
  • Philippines
View GitHub Profile
@forstie
forstie / Collection Services made easy with SQL.sql
Last active October 4, 2023 15:01
The Collection Services (CS) config and CS data hold a goldmine of operational insight about the IBM i. This Gist shows how a little bit of SQL can open the door to gaining insight and value from this data.
--
-- =======================================================================
-- Subject: See Collection Services (CS) config and query CS data with SQL
-- =======================================================================
--
-- Author: Scott Forstie
-- Date : September, 2023
-- Email : forstie@us.ibm.com
--
-- This Gist is a two-fer:
@forstie
forstie / Reactive index strategy.sql
Last active August 26, 2023 01:40
Database and SQL performance tuning is a persistent focus, which many times leads into the indexing strategy. In this gist, I show how some of the existing tools can be tied together to achieve an automated "DBE in a box".
-- Subject: DBE in a box
-- Author: Scott Forstie
-- Date : August, 2023
-- Features Used : This Gist uses SQL PL, qsys2.SYSTEM_STATUS_INFO_basic, systools.lprintf, qsys2.sysixadv, qsys2.sysindexes, QSYS2.SYSINDEXSTAT, SYSTOOLS.ACT_ON_INDEX_ADVICE, QSYS2.RESET_TABLE_INDEX_STATISTICS and SYSTOOLS.REMOVE_INDEXES
--
-- Note:
-- 1) Indexes are not free, and care should be taken when deciding whether an index is worth creating
-- 2) Indexes are not free, and care should be taken to understand whether adequate system resources are available before creating additional indexes
-- 3) Indexes are not free, and care should be taken to establish a well trained Database Engineer (DBE) responsible for your IBM i partitions
--
@BirgittaHauser
BirgittaHauser / BLOBPDF.SQLRPGLE
Last active August 30, 2023 10:08
Access a PDF document (with RPG) and pass the PDF document as Parameter to another Program/Procedure
//*********************************************************************************************
// I recently get asked how to read a small PDF document into a BLOB and pass it as parameter
// to another function or procedure
// 1. When working with embedded SQL character host variables can only be used up to 32k
// Larger data (up to 16 MB - RPG restriction)
// can be read into a LOB-Variable defined with the SQLTYPE keyword
// 2. Unfortunately the SQLTYPE Keyword cannot be used in a Prototype or Procedure Interface
// 3. But the SQL precompiler generates for the LOB variable a data structure with an
// UNS(4) Subfield _LEN and
// CHAR(xxx) Subfield _DATA
@forstie
forstie / Using LICOPT and initAuto.sql
Last active July 2, 2023 05:48
The inspiration for this Gist came from a client. A piece of code had a long-standing problem where a variable was not initialized. Given the unpredictable nature of uninitialized made the topic hard to approach. This Gist shows how the IBM i Optimizing Translator can be used to find such problems within a dev or test environment.
-- Subject: Using initAuto to find uninitialized variable problems in ILE code
-- Author: Scott Forstie
-- Date : June, 2023
-- Features Used : This Gist uses SQL PL, INCLUDE, Change Program (CHGPGM) CL command, qsys2.joblog_info, qsys2.program_info, qsys2.qcmdexc, and QSYS2.BOUND_MODULE_INFO
--
-- A) Programmers use declared variables and structures to facilitate program logic.
-- B) Programmers should always initialize or assign declared variables and structures to contain known and expected values.
-- C) Programmers sometime miss step B.
--
-- This Gist shows how the IBM i Optimizing Translator can help.
@forstie
forstie / db2_to_json.sql
Last active May 2, 2024 07:42
The request... Is it possible to extract data from IBM i into JSON format with a Db2 service?
--
-- Subject: The request... return SQL services detail using JSON.
-- Author: Scott Forstie
-- Date : April, 2023
-- Features Used : This Gist uses qsys2.syscolumns2, listagg(), rtrim(), dynamic SQL, SQL PL, PIPE
--
-- Note:
-- When someone asks you to return Db2 for i data "as JSON", they probably want you
-- to publish a JSON document, which contains good key names and of course, the data.
--
@forstie
forstie / Audit Journal Management.sql
Last active May 2, 2024 07:53
I've been getting asked lots of good questions about how to configure and monitor the Audit Journal. Guess what? #SQLcandoit
--
-- Subject: Audit Journal Management
-- Author: Scott Forstie
-- Date : March, 2023
-- Features Used : This Gist uses qsys2.security_info, qsys2.journal_info, qsys2.object_statistics, qsys2.qcmdexc, CTEs, sysibmadm.env_sys_info, and SYSTOOLS.split
--
-- Notes:
-- ===============================================
-- 1) There are many configuration options to consider using when establishing the Audit Journal, this Gist uses SQL to examine some of the most important choices.
-- 2) Its important to have a retention strategy for audit journal - journal receivers
@BirgittaHauser
BirgittaHauser / ReplaceCharactersInIFSFile
Created December 21, 2022 12:54
Replace Characters in an IFS file
-- Retrieve the IFS file with GET_CLOB_FROM_FILE
-- Replace & with &
-- Write a new IFS file with the same name as the original file and replace the existing IFS file
Call Qsys2.Ifs_Write_Utf8(Path_Name => '/Home/Dir1/Dir2/YourIfsFile.csv',
Line => Replace(Get_Clob_From_File('/Home/Dir1/Dir2/YourIfsFile.csv'),
'&', '&'),
Overwrite => 'REPLACE',
End_Of_Line => 'NONE');
-- Question in an Forum: I have a table/file which has duplicate records (Lets say there are 10 fields in a record).
-- Out of which, if 5 fields put together are same across more than 1 record, then it is considered as a duplicate).
-- I need to delete those duplicate records alone using a SQL statement embedded in a SQLRPGLE program.
-- 1. Solution with a Cursor in embedded SQL (using a Common Table Expression for determining the duplicate rows!)
**Free
//---------------------------------------------------------------------------------------------
CTl-Opt DatFmt(*ISO) Option(*NoDebugIO);
//---------------------------------------------------------------------------------------------
DCL-S GblRRN Int(20);
@BirgittaHauser
BirgittaHauser / UDF - Check eMail Address with SQL.sql
Created September 1, 2022 07:07
Check eMail Address with SQL
-- UDF - Check eMail Address:
-- Parameters: ParEmail => EMail Address
-- Returns: 0 = Invalid eMail Address / 1 = valid eMail Address
-- In Release 7.5 the new Boolean Datatype could be used for the Return Value
-- Attention: There might be better regular expression patterns for checking eMail addresses
Create or Replace Function YourSchema.CheckEmail (ParEMail Varchar(256))
Returns Integer
Language SQL
Modifies SQL Data
@forstie
forstie / ddm server.sql
Created June 4, 2022 15:23
The request... use SQL to determine if the DDM/DRDA server was active, and if not, start it.
--
-- Subject: The request... use SQL to determine if the DDM/DRDA server was active, and if not, start it.
-- Author: Scott Forstie
-- Date : June, 2022
-- Features Used : This Gist uses QSYS2.ACTIVE_JOB_INFO, BOOLEAN, QSYS2.QCMDEXC scalar function, CTE, case expression
--
--
-- Is the DDM/DRDA listener active? (If at IBM i 7.4 or earlier)
--
select count(*) as DDM_DRDA_Listener_Active