1.使用 if not exists判断增加字段是否存在
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;
贴上一个版本的更新sql
drop table if EXISTS t_cst_use_recorde; CREATE TABLE `t_cst_use_recorde` ( `id` varchar(32) NOT NULL COMMENT '主键', `his_patient_id` varchar(32) DEFAULT NULL COMMENT '患者医院Id', `patient_name` varchar(32) DEFAULT NULL COMMENT '患者名字', `case_no` varchar(32) DEFAULT NULL COMMENT '病案号', `gender` varchar(2) DEFAULT NULL COMMENT '性别', `dept_id` varchar(32) DEFAULT NULL COMMENT '科室编码', `dept_name` varchar(32) DEFAULT NULL COMMENT '科室名称', `his_schedule_id` varchar(32) DEFAULT NULL COMMENT '医院手术Id', `his_doctor_id` varchar(32) DEFAULT NULL COMMENT '手术医生Id', `doctor_name` varchar(32) DEFAULT NULL COMMENT '手术医生姓名', `surgery_name` varchar(32) DEFAULT NULL COMMENT '手术名称', `epc` varchar(32) DEFAULT NULL COMMENT '耗材epc编码', `inventory_id` varchar(32) DEFAULT NULL COMMENT '库存表主键id', `operator_name` varchar(32) DEFAULT NULL COMMENT '操作人名称', `operation_time` datetime DEFAULT NULL COMMENT '操作时间', `cst_code` varchar(255) DEFAULT NULL COMMENT '耗材编码', `cst_model` varchar(64) DEFAULT NULL COMMENT '耗材型号', `cst_name` varchar(255) DEFAULT NULL COMMENT '耗材名称', `cst_spec` varchar(255) DEFAULT NULL COMMENT '耗材规格', `manu_name` varchar(64) DEFAULT NULL COMMENT '生产厂家名称', `sth_id` varchar(64) DEFAULT NULL COMMENT '库房id', `sth_name` varchar(64) DEFAULT NULL COMMENT '库房名称', `vendor_name` varchar(64) DEFAULT NULL COMMENT '供应商名', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='患者使用记录表'; drop table if EXISTS t_sys_log_configure; CREATE TABLE `t_sys_log_configure` ( `log_id` char(32) NOT NULL, `log_title` varchar(30) DEFAULT NULL, `log_method` varchar(50) DEFAULT NULL, `log_args` varchar(300) DEFAULT NULL, `log_type` varchar(10) DEFAULT NULL COMMENT '日志类型,1新增/修改,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;