|
@@ -0,0 +1,180 @@
|
|
|
|
|
+-- 沙溪正式业务 Schema C##DC 发布脚本。
|
|
|
|
|
+-- 目的:为集体宿舍违纪增加成员表,并保存违纪发生时班级快照。
|
|
|
|
|
+whenever sqlerror exit sql.sqlcode rollback
|
|
|
|
|
+set serveroutput on
|
|
|
|
|
+alter session set container=ORCLPDB;
|
|
|
|
|
+alter session set current_schema=C##DC;
|
|
|
|
|
+
|
|
|
|
|
+declare
|
|
|
|
|
+ v_count number;
|
|
|
|
|
+ v_start number;
|
|
|
|
|
+ v_schema varchar2(30) := sys_context('USERENV', 'CURRENT_SCHEMA');
|
|
|
|
|
+
|
|
|
|
|
+ procedure create_table_if_missing(
|
|
|
|
|
+ p_name varchar2,
|
|
|
|
|
+ p_ddl varchar2
|
|
|
|
|
+ ) is
|
|
|
|
|
+ begin
|
|
|
|
|
+ select count(*) into v_count
|
|
|
|
|
+ from all_tables
|
|
|
|
|
+ where owner = v_schema
|
|
|
|
|
+ and table_name = p_name;
|
|
|
|
|
+ if v_count = 0 then
|
|
|
|
|
+ execute immediate p_ddl;
|
|
|
|
|
+ end if;
|
|
|
|
|
+ end;
|
|
|
|
|
+
|
|
|
|
|
+ procedure create_sequence_if_missing(
|
|
|
|
|
+ p_name varchar2,
|
|
|
|
|
+ p_ddl varchar2
|
|
|
|
|
+ ) is
|
|
|
|
|
+ begin
|
|
|
|
|
+ select count(*) into v_count
|
|
|
|
|
+ from all_sequences
|
|
|
|
|
+ where sequence_owner = v_schema
|
|
|
|
|
+ and sequence_name = p_name;
|
|
|
|
|
+ if v_count = 0 then
|
|
|
|
|
+ execute immediate p_ddl;
|
|
|
|
|
+ end if;
|
|
|
|
|
+ end;
|
|
|
|
|
+
|
|
|
|
|
+ procedure create_index_if_missing(
|
|
|
|
|
+ p_name varchar2,
|
|
|
|
|
+ p_ddl varchar2
|
|
|
|
|
+ ) is
|
|
|
|
|
+ begin
|
|
|
|
|
+ select count(*) into v_count
|
|
|
|
|
+ from all_indexes
|
|
|
|
|
+ where owner = v_schema
|
|
|
|
|
+ and index_name = p_name;
|
|
|
|
|
+ if v_count = 0 then
|
|
|
|
|
+ execute immediate p_ddl;
|
|
|
|
|
+ end if;
|
|
|
|
|
+ end;
|
|
|
|
|
+
|
|
|
|
|
+begin
|
|
|
|
|
+ create_table_if_missing('WJLCY', 'create table WJLCY (
|
|
|
|
|
+ WJLCYID number(16) not null,
|
|
|
|
|
+ WJJLID number(16) not null,
|
|
|
|
|
+ RYID number(16) not null,
|
|
|
|
|
+ BJID number(16),
|
|
|
|
|
+ GZLID number(16),
|
|
|
|
|
+ GZLHJBL number(3,2) default 0 not null,
|
|
|
|
|
+ JLZTM number(9) default 51 not null,
|
|
|
|
|
+ CJRYID number(16),
|
|
|
|
|
+ GLRYID number(16),
|
|
|
|
|
+ GLBMID number(16),
|
|
|
|
|
+ constraint PK_WJLCY primary key (WJLCYID),
|
|
|
|
|
+ constraint FK_WJJL_WJLCY foreign key (WJJLID)
|
|
|
|
|
+ references WJJL(WJJLID),
|
|
|
|
|
+ constraint FK_RY_WJLCY foreign key (RYID) references RY(RYID),
|
|
|
|
|
+ constraint FK_BJ_WJLCY foreign key (BJID) references BJ(BJID),
|
|
|
|
|
+ constraint FK_JLZT_WJLCY_JLZTM foreign key (JLZTM)
|
|
|
|
|
+ references JLZT(JLZTM)
|
|
|
|
|
+ )');
|
|
|
|
|
+
|
|
|
|
|
+ execute immediate
|
|
|
|
|
+ 'select nvl(max(WJLCYID), 0) + 1 from WJLCY'
|
|
|
|
|
+ into v_start;
|
|
|
|
|
+ create_sequence_if_missing(
|
|
|
|
|
+ 'WJLCYID',
|
|
|
|
|
+ 'create sequence WJLCYID start with ' || v_start
|
|
|
|
|
+ || ' increment by 1 nocache'
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ create_index_if_missing(
|
|
|
|
|
+ 'UK_WJLCY_WJJL_RY',
|
|
|
|
|
+ 'create unique index UK_WJLCY_WJJL_RY on WJLCY(WJJLID, RYID)'
|
|
|
|
|
+ );
|
|
|
|
|
+ create_index_if_missing(
|
|
|
|
|
+ 'IDX_WJLCY_WJJL_BJ',
|
|
|
|
|
+ 'create index IDX_WJLCY_WJJL_BJ on WJLCY(WJJLID, BJID, JLZTM)'
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ execute immediate q'[
|
|
|
|
|
+ create or replace trigger TRG_WJLCY_ID
|
|
|
|
|
+ before insert on WJLCY
|
|
|
|
|
+ for each row
|
|
|
|
|
+ begin
|
|
|
|
|
+ if :new.WJLCYID is null then
|
|
|
|
|
+ select WJLCYID.nextval into :new.WJLCYID from dual;
|
|
|
|
|
+ end if;
|
|
|
|
|
+ end;
|
|
|
|
|
+ ]';
|
|
|
|
|
+
|
|
|
|
|
+ execute immediate q'[
|
|
|
|
|
+ create or replace trigger TRG_WJLCY_BJID
|
|
|
|
|
+ before insert or update of RYID, BJID on WJLCY
|
|
|
|
|
+ for each row
|
|
|
|
|
+ declare
|
|
|
|
|
+ v_cnt number;
|
|
|
|
|
+ v_bjid number(16);
|
|
|
|
|
+ begin
|
|
|
|
|
+ if :new.BJID is null and :new.RYID is not null then
|
|
|
|
|
+ select count(*), min(BJID)
|
|
|
|
|
+ into v_cnt, v_bjid
|
|
|
|
|
+ from BJCY
|
|
|
|
|
+ where RYID = :new.RYID
|
|
|
|
|
+ and JLZTM in (22, 25, 51)
|
|
|
|
|
+ and nvl(SFBY, 0) = 0;
|
|
|
|
|
+ if v_cnt = 1 then
|
|
|
|
|
+ :new.BJID := v_bjid;
|
|
|
|
|
+ end if;
|
|
|
|
|
+ end if;
|
|
|
|
|
+ end;
|
|
|
|
|
+ ]';
|
|
|
|
|
+
|
|
|
|
|
+ commit;
|
|
|
|
|
+ dbms_output.put_line('WJLCY_SCHEMA_OK');
|
|
|
|
|
+end;
|
|
|
|
|
+/
|
|
|
|
|
+
|
|
|
|
|
+-- 将旧的“按学生逐条生成 WJJL”的集体违纪回填到成员表。
|
|
|
|
|
+-- 主负责人保留在 WJJL;WJLCY 只记录同一事件的其他人员,避免重复。
|
|
|
|
|
+insert into WJLCY (
|
|
|
|
|
+ WJLCYID, WJJLID, RYID, BJID, GZLID, GZLHJBL,
|
|
|
|
|
+ JLZTM, CJRYID, GLRYID, GLBMID
|
|
|
|
|
+)
|
|
|
|
|
+select
|
|
|
|
|
+ WJLCYID.nextval,
|
|
|
|
|
+ r.main_wjjlid,
|
|
|
|
|
+ r.ryid,
|
|
|
|
|
+ r.bjid,
|
|
|
|
|
+ null,
|
|
|
|
|
+ 0,
|
|
|
|
|
+ 51,
|
|
|
|
|
+ r.cjryid,
|
|
|
|
|
+ r.glryid,
|
|
|
|
|
+ r.glbmId
|
|
|
|
|
+from (
|
|
|
|
|
+ select
|
|
|
|
|
+ min(w.WJJLID) over (
|
|
|
|
|
+ partition by w.WJLXM, w.WJLBM, w.WJSJ, w.JZWID,
|
|
|
|
|
+ w.KFID, nvl(w.MS, ' '), nvl(w.FJID, 0)
|
|
|
|
|
+ ) main_wjjlid,
|
|
|
|
|
+ row_number() over (
|
|
|
|
|
+ partition by w.WJLXM, w.WJLBM, w.WJSJ, w.JZWID,
|
|
|
|
|
+ w.KFID, nvl(w.MS, ' '), nvl(w.FJID, 0)
|
|
|
|
|
+ order by w.WJJLID
|
|
|
|
|
+ ) rn,
|
|
|
|
|
+ w.RYID ryid,
|
|
|
|
|
+ w.BJID bjid,
|
|
|
|
|
+ w.CJRYID cjryid,
|
|
|
|
|
+ w.GLRYID glryid,
|
|
|
|
|
+ w.GLBMID glbmid
|
|
|
|
|
+ from WJJL w
|
|
|
|
|
+ where w.WJLXM = 5
|
|
|
|
|
+ and w.JLZTM = 51
|
|
|
|
|
+ and w.RYID is not null
|
|
|
|
|
+ and w.BJID is not null
|
|
|
|
|
+) r
|
|
|
|
|
+where r.rn > 1
|
|
|
|
|
+ and not exists (
|
|
|
|
|
+ select 1
|
|
|
|
|
+ from WJLCY c
|
|
|
|
|
+ where c.WJJLID = r.main_wjjlid
|
|
|
|
|
+ and c.RYID = r.ryid
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+commit;
|
|
|
|
|
+exit
|