Skip to content

Instantly share code, notes, and snippets.

View forstie's full-sized avatar

Scott Forstie forstie

  • IBM
View GitHub Profile
@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 / 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 / Glengarry Glen Ross - SQL Style
Created September 24, 2019 01:37
Ok movie buffs, here is a fictional before and after, with a happy ending. Use these techniques to bring database engineering to bear in your data center.
--
-- Existing file... not very user friendly
--
create schema gggr;
create table gggr.sales (
CL varchar(100),
PTY varchar(100),
SP varchar(30),
PG char(1)
);
@forstie
forstie / ROUND vs QUANTIZE
Created August 18, 2019 17:10
This example shows that QUANTIZE can be used along with decfloat rounding mode to achieve programmer control of rounding numeric values.
values current decfloat rounding mode;
set current decfloat rounding mode= round_half_even;
stop;
-- ===============================================================================================================
--
-- QUANTIZE uses the decfloat rounding mode.
-- This is the default: ROUND_HALF_EVEN
--
-- Round to nearest value; if values are equidistant, round so that the final digit is even.
-- If the discarded digits represent greater than half (0.5) of the value of a number in the next left position,
@forstie
forstie / User profile ownership and basic authorities.sql
Last active November 5, 2019 12:53
The first query identifies those users who are lacking authority to use their own *USRPRF. This lack of authority can cause annoying failures in software products. The other queries are used to review whether the *USRPRF ownership implementation matches the strategy.
--
-- description: Which users lack basic authority to their own user profile?
--
select *
from qsys2.object_privileges
where object_type = '*USRPRF'
and object_name = authorization_name
and (object_operational <> 'YES'
or object_management <> 'YES'
@forstie
forstie / Reuseable SQL code segments with INCLUDE
Last active November 5, 2019 12:56
Locate reusable pieces of SQL code into the IFS. Then, use INCLUDE to pull those segments into your SQL routines or triggers, and even ACS's Run SQL Scripts or RUNSQLSTM.
--
-- Use INCLUDE (SQL) to maintain reusable code segments.
-- and incorporate in scripts or SQL Procedures, Functions, and Triggers
-- Reference: http://ibm.biz/DB2fori_INCLUDE
--
--
-- description: Use the INCLUDE statement to pull
-- in and execute common pieces
-- of SQL
@forstie
forstie / Row permissions control for ZDA access.sql
Created June 24, 2019 09:36
Row permissions control for ZDA access
--
-- description: This row permission can be used to disallow specific jobnames from selecting
-- data over a specific file
--
cl:CHGFCNUSG FCNID(QIBM_DB_SECADM) USER(SCOTTF) USAGE(*ALLOWED); -- repeat this on the target (not yet mirrored)
set schema star1g ;
set path star1g ;
create table RowPermRules (
@forstie
forstie / Restoring libraries that begin with the letter E.sql
Created June 15, 2019 05:58
Restoring libraries that begin with the letter E
-- =================================================
-- author: Scott Forstie
-- date : May 29, 2019
-- email : forstie@us.ibm.com
-- disclaimer - no implied warranties, yada yada
-- =================================================
--
-- Super Fast retrieval of library and schema name
@forstie
forstie / journal receivers attached or not.sql
Created October 23, 2019 14:18
I was asked to provide a way to find journal receivers and discern whether or not they are attached or detached.
--
-- description: Which journal receivers are currently attached?
--
select attached_journal_receiver_library, attached_journal_receiver_name
from qsys2.journal_info
where journal_library = 'PRODLIB'
order by 1,2;
@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