You are on page 1of 5

-- Advanced SQL Training

create or replace procedure Z_ADD_ID_TRIGGER(p_TABLE_NAME varchar, p_COLUMN_NAME


varchar, p_SEQUENCE_NAME varchar)
is
begin
execute immediate '
create or replace trigger ' || substr(p_TABLE_NAME, 1, 24) || '_ID_BI
before insert
on ' || p_TABLE_NAME || '
for each row
when (new.' || p_COLUMN_NAME || ' is null)
begin
select ' || p_SEQUENCE_NAME || '.nextval into :new.' || p_COLUMN_NAME || ' from
dual;
end;';
end;
/

-- Database structure

-- Drop all tables and sequences beginning with Z_


begin
for t in (select TABLE_NAME from USER_TABLES where TABLE_NAME like 'Z\_%'
escape '\')
loop
execute immediate 'drop table ' || t.TABLE_NAME || ' cascade
constraints purge' ;
end loop;
for t in (select SEQUENCE_NAME from USER_SEQUENCES where SEQUENCE_NAME like
'Z\_%' escape '\')
loop
execute immediate 'drop sequence ' || t.SEQUENCE_NAME;
end loop;
end;
/

--Location
create sequence Z_LOCATION_ID_SEQ;

create table Z_LOCATION


( LOCATION_ID integer /*default
Z_LOCATION_ID_SEQ.nextval*/ primary key
, CLASS char(1)
not null
, BARCODE varchar2(20)
not null
, DESCRIPTION varchar2(20)
);

execute Z_ADD_ID_TRIGGER('Z_LOCATION', 'LOCATION_ID', 'Z_LOCATION_ID_SEQ');

comment on column Z_LOCATION.LOCATION_ID is 'Internal Location ID Primary Key';


comment on column Z_LOCATION.CLASS is 'Location Class: A, R or Q';
comment on column Z_LOCATION.BARCODE is 'External Location ID. Barcode
value';
comment on column Z_LOCATION.DESCRIPTION is 'External description of the
location';
--Item
create sequence Z_ITEM_ID_SEQ;

create table Z_ITEM


( ITEM_ID integer /*default
Z_ITEM_ID_SEQ.nextval*/ primary key
, REFERENCE varchar2(20)
not null
, DESCRIPTION varchar2(50)
);

comment on column Z_ITEM.ITEM_ID is 'Internal Item ID Primary Key';


comment on column Z_ITEM.REFERENCE is 'External reference of the item';
comment on column Z_ITEM.DESCRIPTION is 'External description of the item';

execute Z_ADD_ID_TRIGGER('Z_ITEM', 'ITEM_ID', 'Z_ITEM_ID_SEQ');

--LPN
create sequence Z_LPN_ID_SEQ;

create table Z_LPN


( LPN_ID integer /*default Z_LPN_ID_SEQ.nextval*/
primary key
, LPN_NUMBER varchar2(20) not null
, LOCATION_ID integer
, LPN_STATUS number(2) default 10 not null
, PLT_ID integer
);

comment on column Z_LPN.LPN_ID is 'Internal LPN ID Primary Key';


comment on column Z_LPN.LPN_NUMBER is 'External reference of the LPN';
comment on column Z_LPN.LOCATION_ID is 'Internal Location ID';
comment on column Z_LPN.LPN_STATUS is 'LPN status';
comment on column Z_LPN.PLT_ID is 'Internal ID of the container LPN';

execute Z_ADD_ID_TRIGGER('Z_LPN', 'LPN_ID', 'Z_LPN_ID_SEQ');

create table Z_LPN_DETAIL


( LPN_ID integer not null
, ITEM_ID integer not null
, QUANTITY number not null
);

comment on column Z_LPN_DETAIL.LPN_ID is 'Internal ID of the LPN';


comment on column Z_LPN_DETAIL.ITEM_ID is 'Internal ID of the item';
comment on column Z_LPN_DETAIL.QUANTITY is 'Quantity of this items in the LPN';

--LPN Lock
create table Z_LOCK
( LOCK_CODE char(2) primary key
, PRIORITY number(2) not null
);

comment on column Z_LOCK.LOCK_CODE is 'Two chars lock code';


comment on column Z_LOCK.PRIORITY is 'Priority of this kind of lock for external
reports';

create table Z_LPN_LOCK


( LPN_ID integer not null
, LOCK_CODE char(2) not null
);

comment on column Z_LPN_LOCK.LPN_ID is 'Internal ID of the LPN';


comment on column Z_LPN_LOCK.LOCK_CODE is 'Two chars lock code';

--Item set
create table Z_ITEM_SET_DETAIL
( ITEM_ID integer not null
, DETAIL_ITEM_ID integer not null
, QUANTITY number default 1 not null check(QUANTITY >
0)
);

comment on column Z_ITEM_SET_DETAIL.ITEM_ID is 'Internal ID of the


container item';
comment on column Z_ITEM_SET_DETAIL.DETAIL_ITEM_ID is 'Internal ID of the
contained item';
comment on column Z_ITEM_SET_DETAIL.QUANTITY is 'Quantity of this
contained item in the container item';

------------------
-- Input tables --
------------------
create sequence Z_INPT_ID_SEQ;

--Item
create table Z_INPT_ITEM
( INPT_ID integer /*default
Z_INPT_ID_SEQ.nextval*/ primary key
, REFERENCE varchar2(20)
not null
, DESCRIPTION varchar2(50) -- null means deletion
of the item
);

comment on column Z_INPT_ITEM.INPT_ID is 'Internal sequential


Input ID. Primary Key';
comment on column Z_INPT_ITEM.REFERENCE is 'External reference
of the item';
comment on column Z_INPT_ITEM.DESCRIPTION is 'External description of
the item';

execute Z_ADD_ID_TRIGGER('Z_INPT_ITEM', 'INPT_ID', 'Z_INPT_ID_SEQ');

--LPN
create table Z_INPT_LPN_DETAIL
( INPT_ID integer /*default Z_INPT_ID_SEQ.nextval*/
primary key
, LPN_NUMBER varchar2(20)
not null
, ITEM_REFERENCE varchar2(20)
not null
, QUANTITY number
);

comment on column Z_INPT_LPN_DETAIL.INPT_ID is 'Internal sequential


Input ID. Primary Key';
comment on column Z_INPT_LPN_DETAIL.LPN_NUMBER is 'External reference of the
LPN';
comment on column Z_INPT_LPN_DETAIL.ITEM_REFERENCE is 'External reference of the
item';
comment on column Z_INPT_LPN_DETAIL.QUANTITY is 'Quantity of this items in
the LPN. null or 0 values removes the detail';

execute Z_ADD_ID_TRIGGER('Z_INPT_LPN_DETAIL', 'INPT_ID', 'Z_INPT_ID_SEQ');

create table Z_INPT_LPN


( INPT_ID integer /*default Z_INPT_ID_SEQ.nextval*/
primary key
, LPN_NUMBER varchar2(20)
not null
, LOCATION_BARCODE varchar2(20)
, LPN_STATUS number(2)
not null
);

comment on column Z_INPT_LPN.INPT_ID is 'Internal sequential Input


ID. Primary Key';
comment on column Z_INPT_LPN.LPN_NUMBER is 'External reference of the
LPN';
comment on column Z_INPT_LPN.LOCATION_BARCODE is 'External Location ID. Barcode
value';
comment on column Z_INPT_LPN.LPN_STATUS is 'LPN status. 99 to remove
the LPN';

execute Z_ADD_ID_TRIGGER('Z_INPT_LPN', 'INPT_ID', 'Z_INPT_ID_SEQ');

--Pallet
create table Z_INPT_PLT
( INPT_ID integer /*default Z_INPT_ID_SEQ.nextval*/
primary key
, PLT_NUMBER varchar2(20)
, LPN_NUMBER varchar2(20)
not null
);

comment on column Z_INPT_PLT.INPT_ID is 'Internal sequential Input ID. Primary


Key';
comment on column Z_INPT_PLT.PLT_NUMBER is 'External reference of the container
LPN. null if not palletized';
comment on column Z_INPT_PLT.LPN_NUMBER is 'External reference of the contained
LPN';

execute Z_ADD_ID_TRIGGER('Z_INPT_PLT', 'INPT_ID', 'Z_INPT_ID_SEQ');

--Item set
create table Z_INPT_ITEM_SET_DETAIL
( INPT_ID integer /*default
Z_INPT_ID_SEQ.nextval*/ primary key
, REFERENCE varchar2(20)
not null
, DETAIL_REFERENCE varchar2(20)
not null
, QUANTITY number
);

comment on column Z_INPT_ITEM_SET_DETAIL.INPT_ID is 'Internal


sequential Input ID. Primary Key';
comment on column Z_INPT_ITEM_SET_DETAIL.REFERENCE is 'External
reference of the item';
comment on column Z_INPT_ITEM_SET_DETAIL.DETAIL_REFERENCE is 'External reference
of the contained item';
comment on column Z_INPT_ITEM_SET_DETAIL.QUANTITY is 'Quantity of
this contained item in the container item. null or 0 values removes the set
detail';

execute Z_ADD_ID_TRIGGER('Z_INPT_ITEM_SET_DETAIL', 'INPT_ID', 'Z_INPT_ID_SEQ');

-- LPN lock
create table Z_INPT_LPN_LOCK
( INPT_ID integer /*default Z_INPT_ID_SEQ.nextval*/
primary key
, LPN_NUMBER varchar2(20)
not null
, LOCK_CODE char(2)
);

comment on column Z_INPT_LPN_LOCK.INPT_ID is 'Internal sequential Input ID.


Primary Key';
comment on column Z_INPT_LPN_LOCK.LPN_NUMBER is 'External reference of the LPN';
comment on column Z_INPT_LPN_LOCK.LOCK_CODE is 'Two chars lock code. null
means remove all locks';

execute Z_ADD_ID_TRIGGER('Z_INPT_LPN_LOCK', 'INPT_ID', 'Z_INPT_ID_SEQ');

-- Add CREATE_DTTM, LAST_UPDATE_DTTM and USER_ID to all tables beginning with Z_


begin
for t in (select TABLE_NAME from USER_TABLES where TABLE_NAME like 'Z\_%'
escape '\' and TABLE_NAME not like 'Z\_ERR\_%' escape '\')
loop
execute immediate 'alter table ' || t.TABLE_NAME || ' add CREATE_DTTM
timestamp default CURRENT_TIMESTAMP not null';
execute immediate 'alter table ' || t.TABLE_NAME || ' add
LAST_UPDATE_DTTM timestamp default CURRENT_TIMESTAMP not null';
execute immediate 'alter table ' || t.TABLE_NAME || ' add USER_ID
varchar2(20)';
end loop;
end;
/

-- Also create bridge error logging tables for each active table (tables Z_* ->
Z_ERR_*)
begin
for t in (select TABLE_NAME from USER_TABLES where TABLE_NAME like 'Z\_%'
escape '\' and TABLE_NAME not like 'Z\_ERR\_%' escape '\' and TABLE_NAME not like
'Z\_INPT\_%' escape '\')
loop
DBMS_ERRLOG.CREATE_ERROR_LOG(t.TABLE_NAME, 'Z_ERR_' ||
substr(t.TABLE_NAME, 3, 24));
end loop;
end;
/

You might also like