Modified table structure used to store update procedure when released mysql sql add fault determination

1. if not exists to justify the increased field is present

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_surgery_info' AND COLUMN_NAME='dept_name') THEN
alter table t_trans_surgery_info 
        add column dept_name varchar(255) DEFAULT NULL COMMENT '科室名称';
end if;

Paste a version of the update sql

Table drop IF EXISTS t_cst_use_recorde; 
the CREATE TABLE t_cst_use_recorde` `( 
  ` id` VARCHAR ( 32 ) the COMMENT the NOT NULL ' master key ' , 
  `his_patient_id` VARCHAR ( 32 ) the COMMENT the DEFAULT NULL ' hospital patient Id ' , 
  ` patient_name` VARCHAR ( 32 ) the DEFAULT the COMMENT NULL ' patient name ' , 
  `case_no` VARCHAR ( 32 ) the COMMENT the DEFAULT NULL ' medical record number ' , 
  ` gender` VARCHAR ( 2 ) the COMMENT the DEFAULT NULL ' sex ' ,
  dept_id` VARCHAR `( 32 ) the COMMENT the DEFAULT NULL ' sections coding ' , 
  ` dept_name` VARCHAR ( 32 ) the COMMENT the DEFAULT NULL ' department name ' , 
  `his_schedule_id` VARCHAR ( 32 ) the COMMENT the DEFAULT NULL ' hospital operating Id ' , 
  ` VARCHAR his_doctor_id` ( 32 ) the COMMENT the DEFAULT NULL ' surgeon Id ' , 
  `doctor_name` VARCHAR ( 32 ) the COMMENT the DEFAULT NULL ' surgeon's name ' , 
  ` surgery_name` VARCHAR ( 32 ) the COMMENT the DEFAULT NULL 'Operation name ' , 
  `epc` VARCHAR ( 32 ) the COMMENT the DEFAULT NULL ' supplies epc coding ' , 
  ` inventory_id` VARCHAR ( 32 ) the COMMENT the DEFAULT NULL ' Stock primary key table ID ' , 
  `operator_name` VARCHAR ( 32 ) the COMMENT the DEFAULT NULL ' operator name ' , 
  `operation_time` the COMMENT datetime the DEFAULT NULL ' operating time ' , 
  ` cst_code` VARCHAR ( 255 ) the COMMENT the DEFAULT NULL ' supplies coding ' , 
  `cst_model` VARCHAR ( 64 ) the COMMENT the DEFAULT NULL 'Supplies model ' , 
  `cst_name` VARCHAR ( 255 ) the COMMENT the DEFAULT NULL ' supplies name ' , 
  ` cst_spec` VARCHAR ( 255 ) the COMMENT the DEFAULT NULL ' supplies specifications ' , 
  `manu_name` VARCHAR ( 64 ) NULL the DEFAULT the COMMENT " manufacturer name " , 
  sth_id` VARCHAR `( 64 ) the COMMENT the DEFAULT NULL ' warehouse ID ' , 
  ` sth_name` VARCHAR ( 64 ) the COMMENT the DEFAULT NULL ' warehouse name ' , 
  `vendor_name` VARCHAR ( 64) The COMMENT the DEFAULT NULL ' vendor name ' , 
  a PRIMARY KEY ( `id`) 
) ENGINE = the InnoDB the DEFAULT the CHARSET = UTF8 the COMMENT = ' patients record table ' ; 


drop Table IF EXISTS t_sys_log_configure; 
the CREATE TABLE t_sys_log_configure`` ( 
  `log_id` char ( 32 ) the NOT NULL, 
  `log_title` VARCHAR ( 30 ) the DEFAULT NULL, 
  ` log_method` VARCHAR ( 50 ) the DEFAULT NULL, 
  `log_args` VARCHAR ( 300 ) the DEFAULT NULL, 
  ` log_type` VARCHAR ( 10 ) the COMMENT the DEFAULT NULL ' log type, 1 Add / modify, delete 3',
  `trans_code` char(4) DEFAULT NULL,
  `tags` varchar(100) DEFAULT NULL,
  `log_insert_date` date DEFAULT NULL,
  `log_update_date` date DEFAULT NULL,
  PRIMARY KEY (`log_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 插入手术和临时患者表字段
DROP PROCEDURE IF EXISTS insert_columns;
CREATE PROCEDURE insert_columns()
BEGIN

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_surgery_info' AND COLUMN_NAME='dept_name') THEN
alter table t_trans_surgery_info 
        add column dept_name varchar(255) DEFAULT NULL COMMENT '科室名称';
end if;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_surgery_info' AND COLUMN_NAME='dept_pym') THEN
alter table t_trans_surgery_info 
        add column dept_pym varchar(255) DEFAULT NULL COMMENT '科室拼音码';
end if;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_surgery_info' AND COLUMN_NAME='his_patient_id') THEN
alter table t_trans_surgery_info 
        add column his_patient_id varchar(255) DEFAULT NULL COMMENT 'HIS患者ID';
end if;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_surgery_info' AND COLUMN_NAME='opt_room_name') THEN
alter table t_trans_surgery_info 
        add column opt_room_name varchar(255) DEFAULT NULL COMMENT '手术间名称';
end if;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_surgery_info' AND COLUMN_NAME='opt_room_no') THEN
alter table t_trans_surgery_info 
        add column opt_room_no varchar(255) DEFAULT NULL COMMENT '手术间编码';
end if;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_surgery_info' AND COLUMN_NAME='opt_room_pym') THEN
alter table t_trans_surgery_info 
        add column opt_room_pym varchar(255) DEFAULT NULL COMMENT '手术间拼音码';
end if;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_surgery_info' AND COLUMN_NAME='patient_id') THEN
alter table t_trans_surgery_info 
        add column patient_id varchar(255) DEFAULT NULL COMMENT '患者ID';
end if;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_surgery_info' AND COLUMN_NAME='patient_name') THEN
alter table t_trans_surgery_info 
        add column patient_name varchar(255) DEFAULT NULL COMMENT '患者名称';
end if;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_surgery_info' AND COLUMN_NAME='patient_pym') THEN
alter table t_trans_surgery_info 
        add column patient_pym varchar(255) DEFAULT NULL COMMENT '患者拼音码';
end if;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_surgery_info' AND COLUMN_NAME='patient_type') THEN
alter table t_trans_surgery_info 
        add column patient_type varchar(255) DEFAULT NULL COMMENT '患者类型';
end if;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_surgery_info' AND COLUMN_NAME='ward_code') THEN
alter table t_trans_surgery_info 
        add column ward_code varchar(32) DEFAULT NULL COMMENT '所在病区代码';
end if;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_surgery_info' AND COLUMN_NAME='ward_name') THEN
alter table t_trans_surgery_info 
        add column ward_name varchar(32) DEFAULT NULL COMMENT '所在病区名称';
end if;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_temp_patient' AND COLUMN_NAME='dept_name') THEN
alter table t_trans_temp_patient 
        add column dept_name varchar(255) DEFAULT NULL COMMENT '科室名称';
end if;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_temp_patient' AND COLUMN_NAME='dept_pym') THEN
alter table t_trans_temp_patient 
        add column dept_pym varchar(255) DEFAULT NULL COMMENT '科室拼音码';
end if;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_temp_patient' AND COLUMN_NAME='opt_room_name') THEN
alter table t_trans_temp_patient 
        add column opt_room_name varchar(255) DEFAULT NULL COMMENT '手术间名称';
end if;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_temp_patient' AND COLUMN_NAME='opt_room_no') THEN
alter table t_trans_temp_patient 
        add column opt_room_no varchar(255) DEFAULT NULL COMMENT '手术间编码';
end if;

IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_trans_temp_patient' AND COLUMN_NAME='opt_room_pym') THEN
alter table t_trans_temp_patient 
        add column opt_room_pym varchar(255) DEFAULT NULL COMMENT '手术间拼音码';
end if;

end ;
call insert_columns;


DROP TRIGGER
IF EXISTS auto_set_value;

CREATE TRIGGER auto_set_value BEFORE INSERT ON t_trans_surgery_info FOR EACH ROW
BEGIN

SET new.patient_id = (
    SELECT
        p.patient_id
    FROM
        t_trans_patient p
    LEFT JOIN t_trans_seek_medical m ON p.patient_id = m.patient_id
    WHERE
        m.medical_id = new.medical_id
);


SET new.his_patient_id = (
    SELECT
        p.his_patient_id
    FROM
        t_trans_patient p
    LEFT JOIN t_trans_seek_medical m ON p.patient_id = m.patient_id
    WHERE
        m.medical_id = new.medical_id
);


SET new.patient_name = (
    SELECT
        p.patient_name
    FROM
        t_trans_patient p
    LEFT JOIN t_trans_seek_medical m ON p.patient_id = m.patient_id
    WHERE
        m.medical_id = new.medical_id
);


SET new.patient_pym = (
    SELECT
        p.pym
    FROM
        t_trans_patient p
    LEFT JOIN t_trans_seek_medical m ON p.patient_id = m.patient_id
    WHERE
        m.medical_id = new.medical_id
);

SET new.patient_type = (
    SELECT
        m.patient_type
    FROM
        t_trans_seek_medical m
    WHERE
        m.medical_id = new.medical_id
);


SET new.opt_room_name = (
    SELECT
        r.room_name
    FROM
        t_base_operation_room r
    WHERE
        r.opt_room_id = new.opt_room_id
);


SET new.opt_room_pym = (
    SELECT
        r.pym
    FROM
        t_base_operation_room r
    WHERE
        r.opt_room_id = new.opt_room_id
);


SET new.opt_room_no = (
    SELECT
        r.room_no
    FROM
        t_base_operation_room r
    WHERE
        r.opt_room_id = new.opt_room_id
);


SET new.dept_pym = (
    SELECT
        d.pym
    FROM
        t_base_dept d
    WHERE
        d.dept_id = new.dept_id
);


SET new.dept_name = (
    SELECT
        d.dept_name
    FROM
        t_base_dept d
    WHERE
        d.dept_id = new.dept_id
);

END;


DROP TRIGGER
IF EXISTS auto_set_temp_patient_value;

CREATE TRIGGER auto_set_temp_patient_value BEFORE INSERT ON t_trans_temp_patient FOR EACH ROW
BEGIN

SET new.opt_room_name = (
    SELECT
        r.room_name
    FROM
        t_base_operation_room r
    WHERE
        r.opt_room_id = new.room_id
);


SET new.opt_room_pym = (
    SELECT
        r.pym
    FROM
        t_base_operation_room r
    WHERE
        r.opt_room_id = new.room_id
);


SET new.opt_room_no = (
    SELECT
        r.room_no
    FROM
        t_base_operation_room r
    WHERE
        r.opt_room_id = new.room_id
);


SET new.dept_pym = (
    SELECT
        d.pym
    FROM
        t_base_dept d
    WHERE
        d.dept_id = new.dept_id
);


SET new.dept_name = (
    SELECT
        d.dept_name
    FROM
        t_base_dept d
    WHERE
        d.dept_id = new.dept_id
);

END;

drop TRIGGER if EXISTS auto_inventory_value;

CREATE TRIGGER auto_inventory_value BEFORE update ON t_cst_inventory FOR EACH ROW
begin
SET new.dept_id = (
    SELECT
        s.dept_id
    FROM
        t_base_dept_storehouse s
    WHERE
        s.sth_id = new.sth_id limit 1
);
end;




DROP PROCEDURE IF EXISTS insert_columns;
CREATE PROCEDURE insert_columns()
BEGIN
IF NOT EXISTS(SELECT 1 FROM  information_schema.COLUMNS WHERE TABLE_SCHEMA=(SELECT database()) AND table_name='t_cst_inventory_journal' AND COLUMN_NAME='thing_id') THEN
alter table t_cst_inventory_journal 
        add column thing_id varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '设备ID';
end if;
end ;
call insert_columns;

Guess you like

Origin www.cnblogs.com/guanxiaohe/p/12027437.html