0% found this document useful (0 votes)
10 views7 pages

Advanced SQL I.4 Bridges - SQL

The document describes SQL code for bridging or synchronizing data between input tables and permanent tables. It contains code for bridging items, item sets, LPN details, LPN locks, and pallets. The code uses MERGE statements to insert new records, update existing records, and delete records as needed to synchronize the data between the input and permanent tables.

Uploaded by

Olivier Comte
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Advanced SQL I.4 Bridges - SQL

The document describes SQL code for bridging or synchronizing data between input tables and permanent tables. It contains code for bridging items, item sets, LPN details, LPN locks, and pallets. The code uses MERGE statements to insert new records, update existing records, and delete records as needed to synchronize the data between the input and permanent tables.

Uploaded by

Olivier Comte
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

-- Advanced SQL Training

------------------
-- Bridge items --
------------------
select 'Items' as BRIDGE from dual

lock table Z_INPT_ITEM in share row exclusive mode;

merge into Z_ITEM dst


using
(select *
from Z_INPT_ITEM i1
where not exists(select 1
from Z_INPT_ITEM i2
where i1.REFERENCE = i2.REFERENCE
and i1.INPT_ID < i2.INPT_ID
)
) src on (src.REFERENCE = dst.REFERENCE)
when matched
then
update
set DESCRIPTION = src.DESCRIPTION
, LAST_UPDATE_DTTM = CURRENT_TIMESTAMP
, USER_ID = coalesce(src.USER_ID, 'BRIDGE ITEM')
delete where src.DESCRIPTION is null
when not matched
then
insert (REFERENCE, DESCRIPTION, USER_ID)
values (src.REFERENCE, src.DESCRIPTION, coalesce(src.USER_ID,
'BRIDGE ITEM'))
where src.DESCRIPTION is not null
log errors into Z_ERR_ITEM ('BRIDGE ITEM') reject limit unlimited;

truncate table Z_INPT_ITEM;


-- Implicit commit since TRUNCATE is a DDL statement!

----------------------
-- Bridge item sets --
----------------------
select 'Item sets' as BRIDGE from dual

lock table Z_INPT_ITEM_SET_DETAIL in share row exclusive mode;

merge into Z_ITEM_SET_DETAIL dst


using
(select i1.*, r.ITEM_ID, d.ITEM_ID as DETAIL_ITEM_ID
from Z_INPT_ITEM_SET_DETAIL i1
left outer join Z_ITEM r
on r.REFERENCE = i1.REFERENCE
left outer join Z_ITEM d
on d.REFERENCE = i1.DETAIL_REFERENCE
where not exists(select 1
from Z_INPT_ITEM_SET_DETAIL i2
where i1.REFERENCE = i2.REFERENCE
and i1.DETAIL_REFERENCE =
i2.DETAIL_REFERENCE
and i1.INPT_ID < i2.INPT_ID
)
) src on ( src.ITEM_ID = dst.ITEM_ID
and src.DETAIL_ITEM_ID = dst.DETAIL_ITEM_ID
)
when matched
then
update
set QUANTITY = coalesce(nullif(src.QUANTITY, 0), 666) -- 666
is a dummy value that respect the check condition on QUANTITY > 0
, LAST_UPDATE_DTTM = CURRENT_TIMESTAMP
, USER_ID = coalesce(src.USER_ID, 'BRIDGE ITEM_SET')
delete where nullif(src.QUANTITY, 0) is null -- 666
is deleted here anyway
when not matched
then
insert (ITEM_ID, DETAIL_ITEM_ID, QUANTITY, USER_ID)
values (src.ITEM_ID, src.DETAIL_ITEM_ID, src.QUANTITY,
coalesce(src.USER_ID, 'BRIDGE ITEM'))
where nullif(src.QUANTITY, 0) is not null
log errors into Z_ERR_ITEM_SET_DETAIL ('BRIDGE ITEM_SET') reject limit
unlimited;

truncate table Z_INPT_ITEM_SET_DETAIL;


-- Implicit commit since TRUNCATE is a DDL statement!

------------------------
-- Bridge LPN details --
------------------------
select 'LPN details' as BRIDGE from dual

lock table Z_INPT_LPN_DETAIL in share row exclusive mode;

insert into Z_LPN(LPN_NUMBER, USER_ID)


select src.LPN_NUMBER, coalesce(max(src.USER_ID), 'BRIDGE LPN_DETAIL') as
USER_ID
from Z_INPT_LPN_DETAIL src
where not exists(select 1
from Z_LPN r
where r.LPN_NUMBER = src.LPN_NUMBER
)
and not exists(select 1
from Z_INPT_LPN_DETAIL i2
where src.LPN_NUMBER = i2.LPN_NUMBER
and src.ITEM_REFERENCE =
i2.ITEM_REFERENCE
and src.INPT_ID < i2.INPT_ID
)
group by src.LPN_NUMBER
log errors into Z_ERR_LPN ('BRIDGE LPN_DETAIL')
reject limit unlimited;

merge into Z_LPN_DETAIL dst


using
(select i1.*, l.LPN_ID, r.ITEM_ID
from Z_INPT_LPN_DETAIL i1
left outer join Z_LPN l
on l.LPN_NUMBER = i1.LPN_NUMBER
left outer join Z_ITEM r
on r.REFERENCE = i1.ITEM_REFERENCE
where not exists(select 1
from Z_INPT_LPN_DETAIL i2
where i1.LPN_NUMBER = i2.LPN_NUMBER
and i1.ITEM_REFERENCE =
i2.ITEM_REFERENCE
and i1.INPT_ID < i2.INPT_ID
)
) src on ( src.LPN_ID = dst.LPN_ID
and src.ITEM_ID = dst.ITEM_ID
)
when matched
then
update
set QUANTITY = coalesce(nullif(src.QUANTITY, 0), 666) -- 666
is a dummy value that respects the check condition QUANTITY > 0
, LAST_UPDATE_DTTM = CURRENT_TIMESTAMP
, USER_ID = coalesce(src.USER_ID, 'BRIDGE LPN_DETAIL')
delete where nullif(src.QUANTITY, 0) is null -- 666
is deleted here anyway
when not matched
then
insert (LPN_ID, ITEM_ID, QUANTITY, USER_ID)
values (src.LPN_ID, src.ITEM_ID, src.QUANTITY,
coalesce(src.USER_ID, 'BRIDGE ITEM'))
where nullif(src.QUANTITY, 0) is not null
log errors into Z_ERR_LPN_DETAIL ('BRIDGE LPN_DETAIL') reject limit
unlimited;

truncate table Z_INPT_LPN_DETAIL;


-- Implicit commit since TRUNCATE is a DDL statement!

----------------------
-- Bridge LPN locks --
----------------------
select 'LPN locks' as BRIDGE from dual

lock table Z_INPT_LPN_LOCK in share row exclusive mode;

insert into Z_LPN(LPN_NUMBER, USER_ID)


select src.LPN_NUMBER, coalesce(src.USER_ID, 'BRIDGE LPN_LOCK') as USER_ID
from Z_INPT_LPN_LOCK src
where not exists(select 1
from Z_LPN r
where r.LPN_NUMBER = src.LPN_NUMBER
)
and not exists(select 1
from Z_INPT_LPN_LOCK s2
where src.LPN_NUMBER = s2.LPN_NUMBER
and src.INPT_ID < s2.INPT_ID
)
and LOCK_CODE is not null -- no need to create the LPN if there is
no lock code to add.
log errors into Z_ERR_LPN ('BRIDGE LPN_LOCK') reject limit unlimited;

merge into Z_LPN_LOCK dst


using
(with src as
(select i.INPT_ID, i.LPN_NUMBER, coalesce(i.LOCK_CODE, ll.LOCK_CODE) as
LOCK_CODE, case when i.LOCK_CODE is null then 1 end as IS_REMOVE, i.USER_ID
from Z_INPT_LPN_LOCK i
left outer join Z_LOCK ll
on i.LOCK_CODE is null -- Cartesian product when source
lock code is null
)
select s1.*, lpn.LPN_ID
from src s1
left outer join Z_LPN lpn
on lpn.LPN_NUMBER = s1.LPN_NUMBER
where not exists(select 1
from src s2
where s1.LPN_NUMBER = s2.LPN_NUMBER
and s1.LOCK_CODE = s2.LOCK_CODE
and s1.INPT_ID < s2.INPT_ID
)
) src on ( src.LPN_ID = dst.LPN_ID
and src.LOCK_CODE = dst.LOCK_CODE
)
when matched
then
update set USER_ID = dst.USER_ID where IS_REMOVE is not null --
dummy update because the syntax of merge doesn't allow to delete a row that has not
been updated
delete where IS_REMOVE is not null -- where
clause is also mandatory here
when not matched
then
insert (LPN_ID, LOCK_CODE, USER_ID)
values (src.LPN_ID, src.LOCK_CODE, coalesce(src.USER_ID, 'BRIDGE
LPN_LOCK'))
where IS_REMOVE is null
log errors into Z_ERR_LPN_LOCK ('BRIDGE LPN_LOCK') reject limit unlimited;

truncate table Z_INPT_LPN_LOCK;

-------------------
-- Bridge pallet --
-------------------
select 'Pallets' as BRIDGE from dual

lock table Z_INPT_PLT in share row exclusive mode;

insert into Z_LPN(LPN_NUMBER, USER_ID)


select src.PLT_NUMBER as LPN_NUMBER, coalesce(max(src.USER_ID), 'BRIDGE
LPN_PLT') as USER_ID
from Z_INPT_PLT src
where src.PLT_NUMBER is not null
and not exists(select 1
from Z_INPT_PLT i2
where i2.LPN_NUMBER = src.LPN_NUMBER
and i2.INPT_ID > src.INPT_ID
)
and not exists(select 1
from Z_LPN r
where r.LPN_NUMBER = src.PLT_NUMBER
)
group by src.PLT_NUMBER
log errors into Z_ERR_LPN ('BRIDGE LPN_PLT') reject limit unlimited;

merge into Z_LPN dst


using
(select i1.*, l.LPN_ID, p.LPN_ID as PLT_ID
from Z_INPT_PLT i1
left outer join Z_LPN l
on l.LPN_NUMBER = i1.LPN_NUMBER
left outer join Z_LPN p
on p.LPN_NUMBER = i1.PLT_NUMBER
where not exists(select 1
from Z_INPT_PLT i2
where i2.LPN_NUMBER = i1.LPN_NUMBER
and i2.INPT_ID > i1.INPT_ID
)
) src
on (src.LPN_ID = dst.LPN_ID)
when matched
then
update
set PLT_ID = src.PLT_ID
, LAST_UPDATE_DTTM = CURRENT_TIMESTAMP
, USER_ID = coalesce(src.USER_ID, 'BRIDGE LPN_PLT')
when not matched
then
insert (LPN_ID, PLT_ID, USER_ID)
values (src.LPN_ID, src.PLT_ID, coalesce(src.USER_ID, 'BRIDGE
LPN_PLT'))
log errors into Z_ERR_LPN ('BRIDGE LPN_DETAIL') reject limit unlimited;

-- Update of childs LPN status according to their container status.


merge into Z_LPN dst
using
(with lpn(LPN_ID, LOCATION_ID, LPN_STATUS, new_LOCATION_ID, new_LPN_STATUS,
new_USER_ID)
as (select LPN_ID, LOCATION_ID, LPN_STATUS, LOCATION_ID, LPN_STATUS, USER_ID
from Z_LPN
where PLT_ID is null
union all select e.LPN_ID, e.LOCATION_ID, e.LPN_STATUS,
m.new_LOCATION_ID, m.new_LPN_STATUS, m.new_USER_ID
from Z_LPN e
inner join lpn m
on m.LPN_ID = e.PLT_ID
)
select *
from lpn
where SYS_OP_MAP_NONNULL(LOCATION_ID) <>
SYS_OP_MAP_NONNULL(new_LOCATION_ID)
or SYS_OP_MAP_NONNULL(LPN_STATUS) <>
SYS_OP_MAP_NONNULL(new_LPN_STATUS)
) src
on (src.LPN_ID = dst.LPN_ID)
when matched
then
update
set LOCATION_ID = new_LOCATION_ID
, LPN_STATUS = new_LPN_STATUS
, LAST_UPDATE_DTTM = CURRENT_TIMESTAMP
, USER_ID = coalesce(new_USER_ID, 'BRIDGE LPN');

truncate table Z_INPT_PLT;

----------------
-- Bridge LPN --
----------------
select 'LPNs' as BRIDGE from dual

lock table Z_INPT_LPN in share row exclusive mode;

insert into Z_LPN(LPN_NUMBER, USER_ID)


select src.LPN_NUMBER, coalesce(max(src.USER_ID), 'BRIDGE LPN') as USER_ID
from Z_INPT_LPN src
where not exists(select 1
from Z_LPN r
where r.LPN_NUMBER = src.LPN_NUMBER
)
and not exists(select 1
from Z_INPT_LPN r
where r.LPN_NUMBER = src.LPN_NUMBER
and r.INPT_ID >= src.INPT_ID
)
group by src.LPN_NUMBER
log errors into Z_ERR_LPN ('BRIDGE LPN') reject limit unlimited;

merge into Z_LPN dest


using
(select i1.*
, case when i1.LOCATION_BARCODE is not null and LPN_STATUS <> 99
then coalesce(loc.LOCATION_ID, -666) end as LOCATION_ID -- -666 as LOCATION_ID
will trigger an error since it doesn't satisfy the FK (null is allowed)
from Z_INPT_LPN i1
left outer join Z_LOCATION loc
on loc.BARCODE = i1.LOCATION_BARCODE
where not exists(select 1
from Z_INPT_LPN r
where r.LPN_NUMBER = i1.LPN_NUMBER
and r.INPT_ID > i1.INPT_ID
)
) src
-- without updating child LPN status and location according to their containers:
-- on (dest.LPN_NUMBER = src.LPN_NUMBER)
-- with update of child LPN status and location according to their containers:
on (exists(with lpn(LPN_ID, LPN_NUMBER)
as (select LPN_ID, LPN_NUMBER
from Z_LPN
where PLT_ID is null
union all select e.LPN_ID, m.LPN_NUMBER
from Z_LPN e
inner join lpn m
on m.LPN_ID =
e.PLT_ID
)
select 1
from lpn
where lpn.LPN_NUMBER = src.LPN_NUMBER
and lpn.LPN_ID = dest.LPN_ID
)
)
when matched
then
update
set LOCATION_ID = src.LOCATION_ID
, LPN_STATUS = src.LPN_STATUS
, LAST_UPDATE_DTTM = CURRENT_TIMESTAMP
, USER_ID = coalesce(src.USER_ID, 'BRIDGE LPN')
delete where src.LPN_STATUS = 99
when not matched
then
insert (LPN_NUMBER, LOCATION_ID, LPN_STATUS, USER_ID)
values(src.LPN_NUMBER, src.LOCATION_ID, src.LPN_STATUS,
coalesce(src.USER_ID, 'BRIDGE LPN'))
where src.LPN_STATUS <> 99
log errors into Z_ERR_LPN ('BRIDGE LPN') reject limit unlimited;

truncate table Z_INPT_LPN;

-- Useless
commit;

You might also like