Skip to content

Instantly share code, notes, and snippets.

@forstie
Created December 10, 2021 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forstie/16f6f3f62bcb0ca760a9b769cd764f00 to your computer and use it in GitHub Desktop.
Save forstie/16f6f3f62bcb0ca760a9b769cd764f00 to your computer and use it in GitHub Desktop.
The challenge was simple... can SQL be used to compare the contents of two spooled files? The solution follows...
-- ========================================================================
--
-- Subject: Comparing two spooled files
-- Author: Scott Forstie
-- Date : December, 2021
--
-- IBM i Services used : OUTPUT_QUEUE_ENTRIES_BASIC, SYSTOOLS.SPOOLED_FILE_DATA
-- SQL language features used: CTEs, Exception joins, Union, and row_number()
-- ========================================================================
-- ========================================================================
--
-- My two, most recent, QPRINT spooled files
--
-- ========================================================================
select job_name, spooled_file_name, file_number
from QSYS2.OUTPUT_QUEUE_ENTRIES_BASIC
where USER_NAME = Session_user and spooled_file_name = 'QPRINT'
order by CREATE_TIMESTAMP desc
limit 2;
stop;
-- ========================================================================
--
-- My two, most recent, QPRINT spooled files (CTE style)
--
-- ========================================================================
with top_two_spooled_files(rn, jn, sfn, fn) as (
select row_number() over(),job_name, spooled_file_name, file_number
from QSYS2.OUTPUT_QUEUE_ENTRIES_BASIC
where USER_NAME = Session_user and
spooled_file_name = 'QPRINT'
order by CREATE_TIMESTAMP desc
limit 2),
first_spooled_file (jn, sfn, fn) as (
select jn, sfn, fn
from top_two_spooled_files
where rn = 1),
second_spooled_file (jn, sfn, fn) as (
select jn, sfn, fn
from top_two_spooled_files
where rn = 2)
select *
from first_spooled_file
union all
select *
from second_spooled_file;
stop;
-- ========================================================================
--
-- The contents of my two, most recent, QPRINT spooled files
--
-- ========================================================================
with top_two_spooled_files(rn, jn, sfn, fn) as (
select row_number() over(),job_name, spooled_file_name, file_number
from QSYS2.OUTPUT_QUEUE_ENTRIES_BASIC
where USER_NAME = Session_user and
spooled_file_name = 'QPRINT'
order by CREATE_TIMESTAMP desc
limit 2),
first_spooled_file (jn, sfn, fn) as (
select jn, sfn, fn
from top_two_spooled_files
where rn = 1),
second_spooled_file (jn, sfn, fn) as (
select jn, sfn, fn
from top_two_spooled_files
where rn = 2)
select jn, sfn, fn, p.*
from first_spooled_file, lateral (
select *
from table (
SYSTOOLS.SPOOLED_FILE_DATA(
JOB_NAME => jn,
SPOOLED_FILE_NAME => sfn,
SPOOLED_FILE_NUMBER => fn)
)
) p
union all
select jn, sfn, fn, p.*
from second_spooled_file, lateral (
select *
from table (
SYSTOOLS.SPOOLED_FILE_DATA(
JOB_NAME => jn,
SPOOLED_FILE_NAME => sfn,
SPOOLED_FILE_NUMBER => fn)
)
) p;
stop;
-- ========================================================================
--
-- Return any differences between these two spooled files
--
-- ========================================================================
with top_two_spooled_files(rn, jn, sfn, fn) as (
select row_number() over(),job_name, spooled_file_name, file_number
from QSYS2.OUTPUT_QUEUE_ENTRIES_BASIC
where USER_NAME = Session_user and
spooled_file_name = 'QPRINT'
order by CREATE_TIMESTAMP desc
limit 2),
first_spooled_file (jn, sfn, fn) as (
select jn, sfn, fn
from top_two_spooled_files
where rn = 1),
second_spooled_file (jn, sfn, fn) as (
select jn, sfn, fn
from top_two_spooled_files
where rn = 2),
first_spooled_file_data (jn, sfn, fn, ord, data) as (
select jn, sfn, fn, p.*
from first_spooled_file, lateral (
select *
from table (
SYSTOOLS.SPOOLED_FILE_DATA(
JOB_NAME => jn, SPOOLED_FILE_NAME => sfn, SPOOLED_FILE_NUMBER => fn)
)
) p
),
second_spooled_file_data (jn, sfn, fn, ord, data) as (
select jn, sfn, fn, p.*
from second_spooled_file, lateral (
select *
from table (
SYSTOOLS.SPOOLED_FILE_DATA(
JOB_NAME => jn, SPOOLED_FILE_NAME => sfn, SPOOLED_FILE_NUMBER => fn)
)
) p
)
select a.jn, a.sfn, a.fn, a.ord, a.data
from first_spooled_file_data a
left exception join second_spooled_file_data b
on a.ord is not distinct from b.ord and
a.data is not distinct from b.data
union all
select b.jn, b.sfn, b.fn, b.ord, b.data
from first_spooled_file_data a
right exception join second_spooled_file_data b
on a.ord is not distinct from b.ord and
a.data is not distinct from b.data
order by ord;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment