-- ---------------------------- -- 合同表结构更新脚本 (Oracle) -- 说明:此脚本用于将旧的合同表结构更新为新的结构 -- 请根据实际情况谨慎执行,建议先备份数据 -- ---------------------------- -- ---------------------------- -- 第一步:备份原有数据(可选,建议执行) -- ---------------------------- -- create table biz_contract_backup as select * from biz_contract; -- ---------------------------- -- 第二步:添加新字段 -- Oracle 不支持 if not exists,需要先检查列是否存在 -- ---------------------------- declare v_count number; begin select count(*) into v_count from user_tab_columns where table_name = 'BIZ_CONTRACT' and column_name = 'PARTY_A_NAME'; if v_count = 0 then execute immediate 'alter table BIZ_CONTRACT add PARTY_A_NAME varchar2(200)'; end if; end; / declare v_count number; begin select count(*) into v_count from user_tab_columns where table_name = 'BIZ_CONTRACT' and column_name = 'PARTY_B_NAME'; if v_count = 0 then execute immediate 'alter table BIZ_CONTRACT add PARTY_B_NAME varchar2(200)'; end if; end; / -- ---------------------------- -- 第三步:迁移数据(如果需要) -- 如果需要从旧字段迁移数据到新字段 -- ---------------------------- -- update BIZ_CONTRACT set PARTY_A_NAME = CUSTOMER_NAME where PARTY_A_NAME is null; -- update BIZ_CONTRACT set PARTY_B_NAME = SIGNING_ENTITY where PARTY_B_NAME is null; -- commit; -- ---------------------------- -- 第四步:设置新字段为非空 -- ---------------------------- -- alter table BIZ_CONTRACT modify PARTY_A_NAME varchar2(200) not null; -- alter table BIZ_CONTRACT modify PARTY_B_NAME varchar2(200) not null; -- ---------------------------- -- 第五步:删除旧字段 -- ---------------------------- declare v_count number; begin select count(*) into v_count from user_tab_columns where table_name = 'BIZ_CONTRACT' and column_name = 'DELIVERY_DEPT_ID'; if v_count > 0 then execute immediate 'alter table BIZ_CONTRACT drop column DELIVERY_DEPT_ID'; end if; end; / declare v_count number; begin select count(*) into v_count from user_tab_columns where table_name = 'BIZ_CONTRACT' and column_name = 'SALES_USER_ID'; if v_count > 0 then execute immediate 'alter table BIZ_CONTRACT drop column SALES_USER_ID'; end if; end; / declare v_count number; begin select count(*) into v_count from user_tab_columns where table_name = 'BIZ_CONTRACT' and column_name = 'ACCOUNTING_TYPE'; if v_count > 0 then execute immediate 'alter table BIZ_CONTRACT drop column ACCOUNTING_TYPE'; end if; end; / declare v_count number; begin select count(*) into v_count from user_tab_columns where table_name = 'BIZ_CONTRACT' and column_name = 'CUSTOMER_NAME'; if v_count > 0 then execute immediate 'alter table BIZ_CONTRACT drop column CUSTOMER_NAME'; end if; end; / declare v_count number; begin select count(*) into v_count from user_tab_columns where table_name = 'BIZ_CONTRACT' and column_name = 'SIGNING_ENTITY'; if v_count > 0 then execute immediate 'alter table BIZ_CONTRACT drop column SIGNING_ENTITY'; end if; end; / declare v_count number; begin select count(*) into v_count from user_tab_columns where table_name = 'BIZ_CONTRACT' and column_name = 'TARGET_PROFIT_RATE'; if v_count > 0 then execute immediate 'alter table BIZ_CONTRACT drop column TARGET_PROFIT_RATE'; end if; end; / declare v_count number; begin select count(*) into v_count from user_tab_columns where table_name = 'BIZ_CONTRACT' and column_name = 'BUSINESS_TAGS'; if v_count > 0 then execute immediate 'alter table BIZ_CONTRACT drop column BUSINESS_TAGS'; end if; end; / declare v_count number; begin select count(*) into v_count from user_tab_columns where table_name = 'BIZ_CONTRACT' and column_name = 'PAID_AMOUNT'; if v_count > 0 then execute immediate 'alter table BIZ_CONTRACT drop column PAID_AMOUNT'; end if; end; / declare v_count number; begin select count(*) into v_count from user_tab_columns where table_name = 'BIZ_CONTRACT' and column_name = 'UNPAID_AMOUNT'; if v_count > 0 then execute immediate 'alter table BIZ_CONTRACT drop column UNPAID_AMOUNT'; end if; end; / declare v_count number; begin select count(*) into v_count from user_tab_columns where table_name = 'BIZ_CONTRACT' and column_name = 'REMARK'; if v_count > 0 then execute immediate 'alter table BIZ_CONTRACT drop column REMARK'; end if; end; / -- ---------------------------- -- 第六步:添加转包相关字段 -- ---------------------------- declare v_count number; begin select count(*) into v_count from user_tab_columns where table_name = 'BIZ_CONTRACT' and column_name = 'IS_SUBCONTRACT'; if v_count = 0 then execute immediate 'alter table BIZ_CONTRACT add IS_SUBCONTRACT varchar2(1) default ''0'''; execute immediate 'comment on column BIZ_CONTRACT.IS_SUBCONTRACT is ''是否转包(0否 1是)'''; end if; end; / declare v_count number; begin select count(*) into v_count from user_tab_columns where table_name = 'BIZ_CONTRACT' and column_name = 'SUBCONTRACT_FROM_ID'; if v_count = 0 then execute immediate 'alter table BIZ_CONTRACT add SUBCONTRACT_FROM_ID number(20)'; execute immediate 'comment on column BIZ_CONTRACT.SUBCONTRACT_FROM_ID is ''转包来源合同ID'''; end if; end; / -- 添加外键约束(可选) -- declare -- v_count number; -- begin -- select count(*) into v_count from user_constraints where table_name = 'BIZ_CONTRACT' and constraint_name = 'FK_CONTRACT_SUBCONTRACT_FROM'; -- if v_count = 0 then -- execute immediate 'alter table BIZ_CONTRACT add constraint FK_CONTRACT_SUBCONTRACT_FROM foreign key (SUBCONTRACT_FROM_ID) references BIZ_CONTRACT(CONTRACT_ID)'; -- end if; -- end; -- /