Skip to content

Instantly share code, notes, and snippets.

View kskuhlman's full-sized avatar

k2uhl kskuhlman

View GitHub Profile
@forstie
forstie / Temporalize a library.sql
Created November 8, 2019 13:59
System period temporal tables were added as a feature built into Db2 for i with IBM i 7.3. This example shows how Temporal could be established for all database files within a specific library.
--
--
-- description: find database files and deploy Temporal over them
-- note: The history table will be named <existing-table-name>_HISTORY
-- note: Uncomment the LPRINTF's if you've built this procedure or have it from Db2 for i
-- minvrm: V7R3M0
--
CREATE OR REPLACE PROCEDURE coolstuff.deploy_temporal(target_library varchar(10))
BEGIN
@NielsLiisberg
NielsLiisberg / ifs_write_json.sql
Last active November 5, 2019 17:17
SQL compund statement using FOR loop and writing JSON to the IFS
begin
declare cmd varchar(4096);
declare comma varchar(1) default '';
call qusrsys.ifs_write('/tmp/test.json' , '[');
for vl as c1 cursor for select cusnum , lstnam from qiws.qcustcdt a do
call qusrsys.ifs_append('/tmp/test.json' ,
comma concat
json_object (
'customerNumber' : CUSNUM,
'name' : LSTNAM
@forstie
forstie / SQL generated table comparison query
Last active April 16, 2021 19:00
SQL, LISTAGG(), QSYS2.SYSCOLUMNS2, and IS NOT DISTINCT all team up here to generate a table level comparision query.
--
-- Imagine that you have 2 versions of the same table.
-- The tables have the same format.
-- This example provides an SQL function that generates a table compare query.
--
create schema coolstuff;
create table coolstuff.table_master as (select * from qsys2.syslimtbl limit 100) with data;
create table coolstuff.table_secondary as (select * from qsys2.syslimtbl limit 100) with data;
select * from coolstuff.table_secondary;
update coolstuff.table_secondary set limit_category = 555 limit 10;
@forstie
forstie / Numbify Packed Decimals using SQL
Last active November 5, 2019 12:39
SQL scalar functions can transform data into information.
--
--
-- Description: Convert packed decimal numbers into decimals
--
--
cl:addlible qsysinc;
cl:clrlib qtemp;
cl:crtsrcpf qtemp/qcsrc;
cl:addpfm file(qtemp/qcsrc) mbr(NIB);
insert into qtemp.qcsrc values
@forstie
forstie / Optimize journaled database files
Created October 12, 2019 22:24
Subtle options in how objects are journaled can have a big impact on performance.
--
--
-- Find journaled database files that can be adjusted for improved performance
--
-- Resources:
-- https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_74/rzajq/rzajqudfobjectstat.htm
-- http://www.redbooks.ibm.com/redbooks/pdfs/sg246286.pdf
--
select 'TOYSTORE', objname as file, omit_journal_entry, journal_images, objtype, objowner,
objdefiner, objcreated, objsize, objtext, objlongname, last_used_timestamp, journaled,
--
-- Search for journals that can be easily improved
--
-- Resources:
-- https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_74/rzajq/rzajqviewjournalinfo.htm
-- http://www.redbooks.ibm.com/redbooks/pdfs/sg246286.pdf
--
select journal_library, journal_name, receiver_maximum_size, remove_internal_entries, asp_number,
journal_aspgrp, attached_journal_receiver_name, attached_journal_receiver_library,
message_queue, message_queue_library, delete_receiver_option, delete_receiver_delay,
@forstie
forstie / SQL Environmental Limits
Created October 12, 2019 19:37
Review the top consumers of SQL resources since the last IPL.
-- Resource:
-- https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_74/rzajq/rzajqhealthenvlimits.htm
--
DECLARE GLOBAL TEMPORARY TABLE Health_Environmental_Limits
LIKE QSYS2.QSQHENVLIM
WITH REPLACE
NOT LOGGED ON ROLLBACK PRESERVE ROWS
ON COMMIT PRESERVE ROWS;
@forstie
forstie / Display Software Resources
Created September 28, 2019 14:47
The Display Software Resources (DSPSFWRSC) command allows you to show, print, or write to an output file the list of installed software resources. This SQL example shows how to externalize the same detail by extracting message text and transforming numerics into integer form.
--
-- category: Software Resources
-- description: DSPSFWRSC for SQL users
--
create or replace function coolstuff.whatsinstalled ()
returns table (
product varchar(7) ccsid 37, load integer, option integer,
software_text varchar(132) ccsid 37
)
external action
@forstie
forstie / ZDA mystery solved
Last active August 18, 2023 17:37
This example shows several things worthy of attention. System managers can utilize exit program to establish improved auditing, understanding, and real time business rules using SQL. For QZDASOINIT jobs, it can be easily considered an unsolvable mystery. With the help of Db2 for i Client Special Registers, we can understand a great deal about ZD…
-- =============================================================
-- Author: Scott Forstie
-- Date : September 8, 2019
-- Revised: August 28, 2020
--
-- Description: Have you ever wondered what's driving all
-- those QZDASxINIT jobs?
-- This example shows how to establish an
-- exit program to capture client special register
-- and other detail for ZDA connections.
@forstie
forstie / dynamic VALUES INTO
Created August 29, 2019 16:02
This example shows how to use dynamic SQL (PREPARE and EXECUTE) to implement a VALUES INTO statement.
create procedure qgpl.values_into (out pout integer)
begin
declare values_into_stmt varchar(1000) ccsid 37;
set values_into_stmt = 'values 1+2+3 into ?';
prepare values_into_query from values_into_stmt;
execute values_into_query using pout;
end;
call qgpl.values_into(?);
-- Note that 6 is returned...