数据库升级脚本-最终版哦

spool D:\工作文档\总参\邵工\1222\spbm1222.log

--table 表1.商品编码表
create table DM_GY_SPBM
(
spbm VARCHAR2(19) not null,
spmc VARCHAR2(200),
sm VARCHAR2(4000),
zzssl VARCHAR2(200),
gjz VARCHAR2(4000),
hzx CHAR(1),
bbh VARCHAR2(20),
kyzt CHAR(1),
zzstsgl VARCHAR2(500),
zzszcyj VARCHAR2(1000),
zzstsnrdm VARCHAR2(100),
xfsgl VARCHAR2(500),
xfszcyj VARCHAR2(1000),
xfstsnrdm VARCHAR2(100),
tjjbm VARCHAR2(4000),
sppm clob,
fjbm VARCHAR2(19),
qysj DATE,
gdqjzsj DATE,
rksj DATE default sysdate
)
tablespace LVHYDK_I_DJ;


-- Create/Recreate primary, unique and foreign key constraints
alter table DM_GY_SPBM
add constraint DM_GY_SPBM_PK primary key (SPBM)
using index
tablespace LVHYDK_I_DJ;

 


--table 表2.商品编码版本表
create table DM_GY_SPBM_BB
(
bbh VARCHAR2(20),
bbsm NVARCHAR2(500),
fbsj DATE,
fbrdm VARCHAR2(20),
fbrxm VARCHAR2(50),
qysj DATE,
gdqjzsj DATE
)
tablespace LVHYDK_I_DJ;


alter table DM_GY_SPBM_BB
add constraint PK_BBH primary key (BBH)
using index
tablespace LVHYDK_I_DJ;


--table 表3.商品编码版本子表
create table DM_GY_SPBM_BB_ZB
(
jlxh VARCHAR2(20),
spbm VARCHAR2(19),
spmc VARCHAR2(200),
sm VARCHAR2(4000),
zzssl VARCHAR2(200),
gjz VARCHAR2(4000),
hzx CHAR(1),
bbh VARCHAR2(20),
kyzt CHAR(1),
zzstsgl VARCHAR2(500),
zzszcyj VARCHAR2(1000),
zzstsnrdm VARCHAR2(100),
xfsgl VARCHAR2(500),
xfszcyj VARCHAR2(1000),
xfstsnrdm VARCHAR2(100),
tjjbm VARCHAR2(4000),
sppm clob,
qysj DATE,
rksj DATE,
gdqjzsj DATE,
sftb CHAR(1),
zjzzstsgl VARCHAR2(500),
zjzzszcyj VARCHAR2(1000),
zjzzstsnrdm VARCHAR2(100),
zjxfsgl VARCHAR2(500),
zjxfszcyj VARCHAR2(1000),
zjxfstsnrdm VARCHAR2(100),
istj NUMBER(4)
)
tablespace LVHYDK_I_DJ;


alter table DM_GY_SPBM_BB_ZB
add constraint FK_ZB_BBH foreign key (BBH)
references DM_GY_SPBM_BB (BBH);

 

--table 表4.商品编码优惠政策表
create table DM_GY_SPBM_YHZC
(
jlxh VARCHAR2(20),
bbh VARCHAR2(20),
spbm VARCHAR2(19),
yhzcwhf VARCHAR2(10),
fbsj DATE,
fbrdm VARCHAR2(20),
fbrxm VARCHAR2(50),
zzszcyj VARCHAR2(1000),
xfsgl VARCHAR2(500),
xfszcyj VARCHAR2(1000),
xfstsnrdm VARCHAR2(100),
rksj DATE,
zzstsgl VARCHAR2(500),
zzstsnrdm VARCHAR2(100),
yhzcdm VARCHAR2(500),
yhzcnr VARCHAR2(500)
)
tablespace LVHYDK_I_DJ;


--table 表5.商品编码缓存表
create table DM_GY_SPBMHC
(
spbm VARCHAR2(19),
spmc VARCHAR2(200),
sm VARCHAR2(4000),
zzssl VARCHAR2(200),
gjz VARCHAR2(4000),
hzx CHAR(1),
zzstsgl VARCHAR2(500),
zzszcyj VARCHAR2(1000),
zzstsnrdm VARCHAR2(100),
xfsgl VARCHAR2(500),
xfszcyj VARCHAR2(1000),
xfstsnrdm VARCHAR2(100),
tjjbm VARCHAR2(4000),
sppm clob,
fjbm VARCHAR2(19),
rksj DATE,
fbrdm VARCHAR2(20),
fbrxm VARCHAR2(50)
)
tablespace LVHYDK_I_DJ;

--table表6.增值税特殊管理表
create table DM_GY_ZZSTSGL
(
zzstsglmc VARCHAR2(500) not null,
sm VARCHAR2(500),
sl VARCHAR2(200),
sybz CHAR(1)
)
tablespace LVHYDK_I_DJ;


-- Create/Recreate primary, unique and foreign key constraints
alter table DM_GY_ZZSTSGL
add constraint PK_ZZSMC primary key (ZZSTSGLMC)
using index
tablespace LVHYDK_I_DJ;


--初始化数据
insert into DM_GY_ZZSTSGL (zzstsglmc, SL) values ('不征税', '0%');
insert into DM_GY_ZZSTSGL (zzstsglmc, SL) values ('免税', '0%');
insert into DM_GY_ZZSTSGL (zzstsglmc, SL) values ('先征后退', null);
insert into DM_GY_ZZSTSGL (zzstsglmc, SL) values ('100%先征后退', null);
insert into DM_GY_ZZSTSGL (zzstsglmc, SL) values ('50%先征后退', null);
insert into DM_GY_ZZSTSGL (zzstsglmc, SL) values ('简易征收', '5%、4%、3%');
insert into DM_GY_ZZSTSGL (zzstsglmc, SL) values ('即征即退30%', null);
insert into DM_GY_ZZSTSGL (zzstsglmc, SL) values ('即征即退50%', null);
insert into DM_GY_ZZSTSGL (zzstsglmc, SL) values ('即征即退70%', null);
insert into DM_GY_ZZSTSGL (zzstsglmc, SL) values ('即征即退100%', null);
insert into DM_GY_ZZSTSGL (zzstsglmc, SL) values ('超税负3%即征即退', null);
insert into DM_GY_ZZSTSGL (zzstsglmc, SL) values ('稀土产品', null);
commit;

-- 编码表 添加 外键约束(字段 bbh 指向 版本表 的bbh)
ALTER TABLE DM_GY_SPBM ADD CONSTRAINT FK_BBH FOREIGN KEY (BBH) REFERENCES DM_GY_SPBM_BB(BBH);


-- 7. 创建序列
create sequence SPBM_YHZC_SEQ01
minvalue 1
maxvalue 9999999999999999999999999999
start with 11
increment by 1
cache 10;


create sequence SPBM_SEQ01
minvalue 1
maxvalue 9999999999999999999999999999
start with 11381
increment by 1
cache 20;


create sequence SEQ_YHZC
minvalue 1
maxvalue 9999999999999999999999999999
start with 422
increment by 1
cache 20;


--8 以下为修改的表

--货物运输业增值税专用发票明细表

alter table JS_SKSKJDK_HYZZSFPMX add BMB_BBH varchar2(20);


--货物运输业增值税专用发票明细子表
alter table JS_SKSKJDK_HYZZSFPMX_FYXM add SPBM Varchar2(20);
alter table JS_SKSKJDK_HYZZSFPMX_FYXM add ZXBM varchar2(20);
alter table JS_SKSKJDK_HYZZSFPMX_FYXM add YHZCBS Char(1);
alter table JS_SKSKJDK_HYZZSFPMX_FYXM add ZZSTSGL varchar2(100);
alter table JS_SKSKJDK_HYZZSFPMX_FYXM add LSLBS Char(1);


--机动车销售统一发票明细表
alter table JS_SKSKJDK_JDCFPMX add BMB_BBH Varchar2(20);
alter table JS_SKSKJDK_JDCFPMX add SPBM Varchar2(20);
alter table JS_SKSKJDK_JDCFPMX add ZXBM varchar2(20);
alter table JS_SKSKJDK_JDCFPMX add YHZCBS Char(1);
alter table JS_SKSKJDK_JDCFPMX add ZZSTSGL varchar2(100);
alter table JS_SKSKJDK_JDCFPMX add LSLBS Char(1);

 

--红字信息表子表
alter table TZD_SKSKJDK_HZFP add BMB_BBH varchar2(20);


--红字信息表子表子表
alter table TZD_SKSKJDK_HZFP_ZB add SPBM Varchar2(20);
alter table TZD_SKSKJDK_HZFP_ZB add ZXBM varchar2(20);
alter table TZD_SKSKJDK_HZFP_ZB add YHZCBS Char(1);
alter table TZD_SKSKJDK_HZFP_ZB add ZZSTSGL varchar2(100);
alter table TZD_SKSKJDK_HZFP_ZB add LSLBS Char(1);

 

--9 创建类型

drop type tbspbmtable force;
drop type tbspbmobjec force;

create or replace type tbspbmobjec as object(hbbm nvarchar2(19),lwmc nvarchar2(200),sm nvarchar2(4000),zzssl nvarchar2(100),zzstsgl nvarchar2(500),zzszcyj nvarchar2(1000),zzstsnrdm nvarchar2(100),xfsgl nvarchar2(500),xfszcyj nvarchar2(1000),xfstsnrdm nvarchar2(100),gjz nvarchar2(4000),sfgb nvarchar2(1),hydm nvarchar2(4000),sppm clob,fjbm nvarchar2(19),bbh nvarchar2(19),sysj nvarchar2(19),qysj nvarchar2(19),kyzt nvarchar2(10));
/
create or replace type tbspbmtable as table of tbspbmobjec;
/

-- 10 创建类型
drop type spbmtable force;
drop type spbmobjec force;

create or replace type spbmobjec as object(hbbm nvarchar2(19),lwmc nvarchar2(200),sm nvarchar2(4000),zzssl nvarchar2(100),zzstsgl nvarchar2(500),zzszcyj nvarchar2(1000),zzstsnrdm nvarchar2(100),
xfsgl nvarchar2(500),xfszcyj nvarchar2(1000),xfstsnrdm nvarchar2(100),gjz nvarchar2(4000),sfgb nvarchar2(1),hydm nvarchar2(4000),sppm clob,fjbm nvarchar2(19),isyzfj nvarchar2(1));
/
create or replace type spbmtable as table of spbmobjec;
/

--11 创建存储过程 分页存储过程
CREATE OR REPLACE Procedure Soa_Getpager(p_Pagesize Int, --每页记录数
p_Pageno Int, --当前页码,从 1 开始
p_Sqlcols Varchar2, --选择列
p_Sqlfrom Varchar2, --表名OR连接查询
p_Sqlwhere nVarchar2, --查询子句
p_Sqlorderby Varchar2, --排序
p_Outrecordcount Out Int, --返回总记录数
p_Outpagecount Out Int, --返回总页数
p_counts Out Sys_Refcursor) As
v_Sql Varchar2(3000);
p_Sqlselect Varchar2(3000);
v_Count Int;
v_Heirownum Int;
v_Lowrownum Int;
Begin

----拼接SQL查询语句
p_Sqlselect := 'select ' || p_Sqlcols || ' from ' || p_Sqlfrom || ' ' || p_Sqlwhere ;
----取记录总数
v_Sql := 'select count(1) from ' || p_Sqlfrom || ' ' || p_Sqlwhere ;
Execute Immediate v_Sql Into v_Count;
p_Outrecordcount := v_Count;
----取总页数
p_Outpagecount := p_Outrecordcount / p_Pagesize;
If p_Outpagecount = 0 Then
p_Outpagecount := 1;
End If;

p_Sqlselect :=p_Sqlselect || ' order by '||p_Sqlorderby;
----执行分页查询
v_Heirownum := p_Pageno * p_Pagesize;
v_Lowrownum := v_Heirownum - p_Pagesize + 1;

v_Sql := 'SELECT * FROM (
SELECT A.*,rownum rn
FROM ( ' || p_Sqlselect || ') A
WHERE rownum <= ' || To_Char(v_Heirownum) || '
) B
WHERE rn >= ' || To_Char(v_Lowrownum);
--注意对rownum别名的使用,第一次直接用rownum,第二次一定要用别名rn

Dbms_Output.Put_Line(v_Sql);

Open p_counts For v_Sql;

exception when others then p_Outpagecount:=-1;

End Soa_Getpager;
/

--12 创建存储过程 获取商品编码子集
create or replace procedure spbm_get_jb(spbm in varchar2,backdata out Sys_Refcursor) is
cxsql varchar2(4000);
begin

if spbm is null or spbm='' then

cxsql:=' select spbm,spmc,zzssl,(select max(spbm)||''|''||count(1) from DM_GY_SPBM z where z.fjbm=t.spbm) cc,kyzt,fjbm,zzstsgl from DM_GY_SPBM t
where fjbm is null and t.kyzt=''Y'' order by spbm asc';

else

cxsql:= 'select spbm,spmc,zzssl,(select max(spbm)||''|''||count(1) from DM_GY_SPBM z where z.fjbm=t.spbm) cc,kyzt,fjbm,zzstsgl from DM_GY_SPBM t
where t.fjbm='''||spbm||''' and t.kyzt=''Y'' order by spbm asc ';
end if;

open backdata for cxsql;

end spbm_get_jb;


/


--13 创建存储过程 批量导入商品编码
create or replace procedure spbmproceobject(aym in spbmtable,proerr out clob,SYSJ in varchar2,v_QYSJ in varchar,
FBRDM in varchar,FBRXM in varchar,BBSM in varchar,cfbm out clob,spbbh out varchar2,isfb in number)
as
ain spbmobjec;
v_version varchar2(20);
v_spbm_counts number;
v_fjspbm_counts number;
v_lwmc number;

begin

savepoint pt;

--计算版本
select max(to_number(substr(bbh,1,instr(bbh,'.')-1)))+1 into v_version from dm_gy_spbm_bb;
if(v_version is null or v_version ='') then
v_version:='1.0';
end if;
if instr(v_version,'.')=0 then
v_version:=v_version||'.0';
end if;

insert into dm_gy_spbm_bb values(v_version,BBSM,sysdate,FBRDM,FBRXM,to_date(SYSJ,'yyyy/mm/dd'),to_date(v_QYSJ,'yyyy/mm/dd'));

 

for ay in aym.first()..aym.last()
loop
ain:=aym(ay);
v_fjspbm_counts:=1;
if ain.isyzfj='1' then
select count(1) into v_fjspbm_counts from dm_gy_spbm where spbm=ain.fjbm ;
end if;

v_spbm_counts:=0;
if v_fjspbm_counts>0 then
select count(1) into v_spbm_counts from dm_gy_spbm where spbm=ain.hbbm ;
end if;

v_lwmc:=0;
if v_spbm_counts=0 and v_fjspbm_counts=1 then
select count(1) into v_lwmc from dm_gy_spbm where spmc=ain.lwmc ;
end if;

if ((v_spbm_counts=0 and isfb=0) or (isfb=1)) and v_fjspbm_counts=1 and v_lwmc=0 then

--新增
if v_spbm_counts=0 then
insert into dm_gy_spbm(spbm,spmc,SM,zzssl,hzx,kyzt,gjz,zzstsgl,ZZSZCYJ,ZZSTSNRDM,XFSGL,XFSZCYJ,XFSTSNRDM,TJJBM,SPPM,QYSJ,GDQJZSJ,FJBM,BBH,RKSJ)
values(ain.hbbm,ain.lwmc,ain.sm,ain.zzssl,ain.sfgb,'Y',ain.gjz,ain.zzstsgl,ain.zzszcyj,ain.zzstsnrdm,
ain.xfsgl,ain.xfszcyj,ain.xfstsnrdm,ain.hydm,ain.sppm,to_date(SYSJ,'yyyy/mm/dd'),to_date(v_QYSJ,'yyyy/mm/dd'),ain.fjbm,v_version,sysdate) ;
end if;
if v_spbm_counts>0 then
update dm_gy_spbm set spmc=ain.lwmc,SM=ain.sm,zzssl=ain.zzssl,hzx=ain.sfgb,gjz=ain.gjz,zzstsgl=ain.zzstsgl,
ZZSZCYJ=ain.zzszcyj,ZZSTSNRDM=ain.zzstsnrdm,XFSGL=ain.xfsgl,XFSZCYJ=ain.xfszcyj,XFSTSNRDM=ain.xfstsnrdm,
TJJBM=ain.hydm,SPPM=ain.sppm,QYSJ=to_date(SYSJ,'yyyy/mm/dd'),GDQJZSJ=to_date(v_QYSJ,'yyyy/mm/dd'),BBH=v_version
where spbm=ain.hbbm ;
end if;

insert into dm_gy_spbm_bb_zb(JLXH,spbm,spmc,SM,zzssl,hzx,kyzt,gjz,zzstsgl,ZZSZCYJ,ZZSTSNRDM,XFSGL,XFSZCYJ,XFSTSNRDM,TJJBM,SPPM,QYSJ,GDQJZSJ,BBH,rksj)
values(spbm_seq01.nextval,ain.hbbm,ain.lwmc,ain.sm,ain.zzssl,ain.sfgb,'Y',ain.gjz,ain.zzstsgl,ain.zzszcyj,ain.zzstsnrdm,
ain.xfsgl,ain.xfszcyj,ain.xfstsnrdm,ain.hydm,ain.sppm,to_date(SYSJ,'yyyy/mm/dd'),to_date(v_QYSJ,'yyyy/mm/dd'),v_version,sysdate) ;
else

if v_spbm_counts>0 then
cfbm:=cfbm||to_char(ain.hbbm)||',';--编码重复
end if;

if v_fjspbm_counts=0 then
proerr:=proerr||to_char(ain.hbbm)||'bb';--没有夫级
end if;

if v_lwmc>0 then
proerr:=proerr||to_char(ain.hbbm)||'cc';--劳务名称
end if;
end if;

end loop;
if (proerr is null or proerr='') and (cfbm is null or cfbm='') then
spbbh:=v_version;
commit;
else
rollback to savepoint pt;
end if;


exception
when others then
proerr:=sqlerrm;
rollback to savepoint pt;
end spbmproceobject;

/

---14 创建存储过程 批量同步商品编码
create or replace procedure tbspbmproceobject(aym in tbspbmtable,tbresult out varchar2)
as
ain tbspbmobjec;
v_version varchar2(20);
v_version1 number(8);

b_zzstsgl varchar2(4000);
b_zzszcyj varchar2(4000);
b_zzstsnrdm varchar2(4000);
b_xfsgl varchar2(4000);
b_xfszcyj varchar2(4000);
b_xfstsnrdm varchar2(4000);
bbzj number(8);
indexsy number(12);
JLXHcount number(12);
begin

savepoint pt;

indexsy:=0;

for ay in aym.first()..aym.last()
loop
tbresult :=1;
indexsy:=indexsy+1;
ain:=aym(ay);

select count(spbm) into v_version1 from dm_gy_spbm where spbm=ain.hbbm ;

 

if v_version1 =0 then
--先往版本表里面插入值
select count(bbh) into bbzj from dm_gy_spbm_bb where bbh=ain.bbh;
if bbzj=0 then
insert into dm_gy_spbm_bb values(ain.bbh,'',sysdate,'','',to_date(ain.sysj,'yyyy/mm/dd'),to_date(ain.qysj,'yyyy/mm/dd'));
end if;
insert into dm_gy_spbm(spbm,spmc,SM,zzssl,gjz,HZX,BBH,kyzt,zzstsgl,ZZSZCYJ,ZZSTSNRDM,XFSGL,XFSZCYJ,XFSTSNRDM,TJJBM,SPPM,FJBM,QYSJ,GDQJZSJ,RKSJ)
values(ain.hbbm,ain.lwmc,ain.sm,ain.zzssl,ain.gjz,ain.sfgb,ain.bbh,ain.kyzt,ain.zzstsgl,ain.zzszcyj,ain.zzstsnrdm,
ain.xfsgl,ain.xfszcyj,ain.xfstsnrdm,ain.hydm,ain.sppm,ain.fjbm,to_date(ain.sysj,'yyyy/mm/dd'),to_date(ain.qysj,'yyyy/mm/dd'),sysdate) ;

--王版本字表中插入数据
insert into dm_gy_spbm_bb_zb(JLXH,spbm,spmc,SM,zzssl,HZX,kyzt,gjz,zzstsgl,ZZSZCYJ,ZZSTSNRDM,XFSGL,XFSZCYJ,XFSTSNRDM,TJJBM,SPPM,QYSJ,GDQJZSJ,BBH,RKSJ,SFTB,
zjzzstsgl,zjzzszcyj,zjzzstsnrdm,zjxfsgl,zjxfszcyj,zjxfstsnrdm)
values(spbm_seq01.nextval,ain.hbbm,ain.lwmc,ain.sm,ain.zzssl,ain.sfgb,ain.kyzt,ain.gjz,ain.zzstsgl,ain.zzszcyj,ain.zzstsnrdm,
ain.xfsgl,ain.xfszcyj,ain.xfstsnrdm,ain.hydm,ain.sppm,to_date(ain.sysj,'yyyy/mm/dd'),to_date(ain.qysj,'yyyy/mm/dd'),ain.bbh,sysdate,1,
ain.zzstsgl,ain.zzszcyj,ain.zzstsnrdm,ain.xfsgl,ain.xfszcyj,ain.xfstsnrdm) ;


else
select bbh into v_version from dm_gy_spbm where spbm=ain.hbbm ;

if trunc(v_version)<>trunc(ain.bbh) then

v_version := substr(ain.bbh,1,instr(ain.bbh,'.')-1)||'.'||substr(v_version,instr(v_version,'.')+1,length(v_version)- instr(v_version,'.'));

select count(bbh) into bbzj from dm_gy_spbm_bb where bbh=v_version;
if bbzj=0 then
insert into dm_gy_spbm_bb values(v_version,'',sysdate,'','',to_date(ain.sysj,'yyyy/mm/dd'),to_date(ain.qysj,'yyyy/mm/dd'));
end if;

select count(JLXH) into JLXHcount from dm_gy_spbm_bb_zb where spbm=ain.hbbm and sftb=1;
if JLXHcount>0 then
--获取总局的优惠政策数据
select zjzzstsgl,zjzzszcyj,zjzzstsnrdm,zjxfsgl,zjxfszcyj,zjxfstsnrdm into b_zzstsgl,b_zzszcyj,b_zzstsnrdm,b_xfsgl,b_xfszcyj,b_xfstsnrdm from (select zjzzstsgl,zjzzszcyj,zjzzstsnrdm,zjxfsgl,zjxfszcyj,zjxfstsnrdm from dm_gy_spbm_bb_zb where spbm=ain.hbbm and sftb=1 order by JLXH desc) where rownum=1 ;
else
b_zzstsgl:=null;
b_zzszcyj:=null;
b_zzstsnrdm:=null;
b_xfsgl:=null;
b_xfszcyj:=null;
b_xfstsnrdm:=null;
end if;
if ain.zzstsgl<>b_zzstsgl or (b_zzstsgl is null and ain.zzstsgl is not null) then
b_zzstsgl:=ain.zzstsgl;
else
b_zzstsgl:=null;
end if;
if ain.zzszcyj<>b_zzszcyj or (b_zzszcyj is null and ain.zzszcyj is not null) then
b_zzszcyj:=ain.zzszcyj;
else
b_zzszcyj:=null;
end if;
if ain.zzstsnrdm<>b_zzstsnrdm or (b_zzstsnrdm is null and ain.zzstsnrdm is not null) then
b_zzstsnrdm:=ain.zzstsnrdm;
else
b_zzstsnrdm:=null;
end if;
if ain.xfsgl<>b_xfsgl or (b_xfsgl is null and ain.xfsgl is not null) then
b_xfsgl:=ain.xfsgl;
else
b_xfsgl:=null;
end if;
if ain.xfszcyj<>b_xfszcyj or (b_xfszcyj is null and ain.xfszcyj is not null) then
b_xfszcyj:=ain.xfszcyj;
else
b_xfszcyj:=null;
end if;
if ain.xfstsnrdm<>b_xfstsnrdm or (b_xfstsnrdm is null and ain.xfstsnrdm is not null) then
b_xfstsnrdm:=ain.xfstsnrdm;
else
b_xfstsnrdm:=null;
end if;

--更新商品编码表
update dm_gy_spbm set spbm=ain.hbbm,spmc=ain.lwmc,SM=ain.sm,zzssl=ain.zzssl,HZX=ain.sfgb,kyzt=ain.kyzt,gjz=ain.gjz,
zzstsgl=(case when b_zzstsgl is null then zzstsgl else b_zzstsgl end),
ZZSZCYJ=(case when b_zzszcyj is null then ZZSZCYJ else b_zzszcyj end),
ZZSTSNRDM=(case when b_zzstsnrdm is null then ZZSTSNRDM else b_zzstsnrdm end),
XFSGL=(case when b_xfsgl is null then XFSGL else b_xfsgl end),
XFSZCYJ=(case when b_xfszcyj is null then XFSZCYJ else b_xfszcyj end),
XFSTSNRDM=(case when b_xfstsnrdm is null then XFSTSNRDM else b_xfstsnrdm end),
TJJBM=ain.hydm,SPPM=ain.sppm,GDQJZSJ=to_date(ain.qysj,'yyyy/mm/dd'),QYSJ=to_date(ain.sysj,'yyyy/mm/dd'),FJBM=ain.fjbm,BBH=v_version,RKSJ=sysdate
where spbm=ain.hbbm;

insert into dm_gy_spbm_bb_zb(JLXH,spbm,spmc,SM,zzssl,HZX,kyzt,gjz,zzstsgl,ZZSZCYJ,ZZSTSNRDM,XFSGL,XFSZCYJ,XFSTSNRDM,TJJBM,SPPM,GDQJZSJ,QYSJ,BBH,RKSJ,SFTB,
zjzzstsgl,zjzzszcyj,zjzzstsnrdm,zjxfsgl,zjxfszcyj,zjxfstsnrdm)
select spbm_seq01.nextval,spbm,spmc,SM,zzssl,HZX,kyzt,gjz,zzstsgl,ZZSZCYJ,ZZSTSNRDM,XFSGL,XFSZCYJ,XFSTSNRDM,TJJBM,SPPM,GDQJZSJ,QYSJ,BBH,sysdate,1,
ain.zzstsgl,ain.zzszcyj,ain.zzstsnrdm,ain.xfsgl,ain.xfszcyj,ain.xfstsnrdm from (
select spbm,spmc,SM,zzssl,HZX,kyzt,gjz,zzstsgl,ZZSZCYJ,ZZSTSNRDM,XFSGL,XFSZCYJ,XFSTSNRDM,TJJBM,SPPM,GDQJZSJ,QYSJ,BBH from dm_gy_spbm where spbm=ain.hbbm
);


end if;
end if;
if mod(indexsy,200)=0 then
commit;
end if;
end loop;

exception
when others then
tbresult :=sqlerrm;--失败了
dbms_output.put_line(sqlcode||sqlerrm);
rollback to savepoint pt;
commit;
end tbspbmproceobject;
/

--15 缓存商品编码
create or replace procedure hcspbm (
v_spbm in VARCHAR2,
v_spmc in varchar2,
v_zzssl in varchar2,
v_KGB in varchar2,
v_KYZT in varchar2,
v_GJZ in varchar2,
v_ZZSTSGL in varchar2,
v_ZZSZCYJ in varchar2,
v_ZZSTSNRDM in varchar2,
v_XFSGL in varchar2,
v_XFSZCYJ in varchar2,
v_XFSTSNRDM in varchar2,
v_TJJBM in varchar2,
v_SPPM in clob,
v_SM in varchar2,
v_SYSJ in date,
v_QYSJ in date,
v_FJBM in varchar2,
v_FBRDM in varchar2,
v_FBRXM in varchar2,
v_oper in varchar2,
czzt out number --该变量记录本次操作的状态。 0 成功 -1 失败
)

as
v_spbm_counts number;
v_version varchar2(20);
v_sytime date;
v_qytime date;
v_lwmc number;
begin
--首先检查商品编码是否重复。如果 重复不做修改;不重复 插入编码表
select count(spbm) into v_spbm_counts from dm_gy_spbmhc where spbm=v_spbm ;


if v_oper='2' then
if v_spbm_counts=0 then
select count(spbm) into v_spbm_counts from dm_gy_spbm where spbm=v_spbm ;
end if;

select count(spbm) into v_lwmc from dm_gy_spbmhc where spbm<>v_spbm and spmc=v_spmc ;

if v_lwmc >0 then
czzt:= -2 ;
return;
end if;
end if;
--新增商品编码验证是否重复,修改商品编码必须存在编码
if (v_spbm_counts>0 and v_oper='2') or (v_spbm_counts=0 and v_oper='3') then
czzt:= -1 ;
return;

else

-- 取出当前最大版本号,往上递加1(注意区分 总局 省局。如果总局操作那么小数点左边递增1.如果省局操作 那么小数点右边递增1 )

czzt:=0;
v_sytime:=v_SYSJ;
v_qytime:=v_QYSJ;
if v_sytime is null or v_sytime='' then
v_sytime:=sysdate;
end if;

if v_qytime is null or v_qytime='' then
v_qytime:=sysdate;
end if;

 

--新增商品商品编码
if v_oper='2' then
insert into dm_gy_spbmhc(spbm,spmc,SM,zzssl,hzx,gjz,zzstsgl,ZZSZCYJ,ZZSTSNRDM,XFSGL,XFSZCYJ,XFSTSNRDM,TJJBM,SPPM,FJBM,RKSJ,FBRDM,FBRXM)
values(v_spbm,v_spmc,v_SM,v_zzssl,v_kgb,v_gjz,v_zzstsgl,v_ZZSZCYJ,v_ZZSTSNRDM,v_XFSGL,v_XFSZCYJ,v_XFSTSNRDM,v_TJJBM,v_SPPM,v_fjbm,sysdate,v_FBRDM,v_FBRXM) ;
end if;

if v_oper='3' then
update dm_gy_spbmhc set spmc=v_spmc,SM=v_SM,zzssl=v_zzssl,hzx=v_kgb,gjz=v_gjz,zzstsgl=v_zzstsgl,ZZSZCYJ=v_ZZSZCYJ,ZZSTSNRDM=v_ZZSTSNRDM,
XFSGL=v_XFSGL,XFSZCYJ=v_XFSZCYJ,XFSTSNRDM=v_XFSTSNRDM,TJJBM=v_TJJBM,SPPM=v_SPPM
where spbm=v_spbm;
end if;

czzt:=1;
commit;

end if;

exception when others then
rollback;
dbms_output.put_line(sqlcode||' '||sqlerrm);
end hcspbm;


/

--16 创建存储过程 优惠政策维护
create or replace procedure whspbm(spbmxml in clob,v_ZZSTSGL in varchar2,v_ZZSZCYJ in varchar2,v_ZZSTSNRDM in varchar2
,v_XFSGL in varchar2,v_XFSZCYJ in varchar2,v_XFSTSNRDM in varchar2,FBRDM in varchar2,FBRXM in varchar2,BBSM in varchar2,vtype in varchar2,bbh out varchar2,czzt out number) is
v_version varchar2(200);
v_xtdate date;
gddate date;
qydate date;
begin

savepoint pt;
--计算版本
if(vtype='0') then --总局
select max(to_number(substr(bbh,1,instr(bbh,'.')-1)))+1 into v_version from dm_gy_spbm_bb;

if(v_version is null or v_version ='') then
v_version:='1.0';
end if;
if instr(v_version,'.')=0 then
v_version:=v_version||'.0';
end if;

else--省局
select max(to_number(bbh)) into v_version from dm_gy_spbm_bb;

if(v_version is null or v_version ='') then
v_version:='0.0';
end if;
if instr(v_version,'.')=0 then
v_version:=v_version||'.0';
end if;
v_version:= substr(v_version,1,instr(v_version,'.')-1)||'.';

select (v_version||(max(to_number(substr(bbh,instr(bbh,'.')+1)))+1)) into v_version from dm_gy_spbm_bb;

end if;

select GDQJZSJ,QYSJ into gddate,qydate from DM_GY_SPBM_BB where bbh=(select (max(floor(bbh))||'.'||max(to_number(replace(to_char((to_number(bbh)-floor(bbh))),'.','')))) maxbbh from dm_gy_spbm_bb where bbh>=((select floor(max(to_number(bbh))) from dm_gy_spbm_bb)));
if qydate<sysdate then
qydate:=to_date(to_char(sysdate,'YYYY/MM/DD'),'YYYY/MM/DD');
gddate:=to_date(to_char(sysdate,'YYYY/MM/DD'),'YYYY/MM/DD');
end if;

v_xtdate:=sysdate;
insert into dm_gy_spbm_bb values(v_version,BBSM,v_xtdate,FBRDM,FBRXM,qydate,gddate);

for xsk in ( SELECT
EXTRACTVALUE(VALUE(t),'/u/i') i--编码
, EXTRACTVALUE(VALUE(t),'/u/t') t-- 类型
FROM TABLE(XMLSEQUENCE(EXTRACT(
XMLTYPE(spbmxml), '/d/u')))
t )
loop

if(xsk.t='1') then
update dm_gy_spbm set ZZSTSGL=v_ZZSTSGL,ZZSZCYJ=v_ZZSZCYJ,ZZSTSNRDM=v_ZZSTSNRDM,XFSGL=v_XFSGL,XFSZCYJ=v_XFSZCYJ,XFSTSNRDM=v_XFSTSNRDM,BBH=v_version,GDQJZSJ=gddate,QYSJ=qydate where SPBM like ''||xsk.i||'%';
elsif (xsk.t='2') then
update dm_gy_spbm set ZZSTSGL=v_ZZSTSGL,ZZSZCYJ=v_ZZSZCYJ,ZZSTSNRDM=v_ZZSTSNRDM,XFSGL=v_XFSGL,XFSZCYJ=v_XFSZCYJ,XFSTSNRDM=v_XFSTSNRDM,BBH=v_version,GDQJZSJ=gddate,QYSJ=qydate where FJBM=xsk.i;
elsif (xsk.t='3') then
update dm_gy_spbm set ZZSTSGL=v_ZZSTSGL,ZZSZCYJ=v_ZZSZCYJ,ZZSTSNRDM=v_ZZSTSNRDM,XFSGL=v_XFSGL,XFSZCYJ=v_XFSZCYJ,XFSTSNRDM=v_XFSTSNRDM,BBH=v_version,GDQJZSJ=gddate,QYSJ=qydate where SPBM=xsk.i;
end if;

end loop;

insert into dm_gy_spbm_bb_zb(JLXH,spbm,spmc,SM,zzssl,hzx,kyzt,gjz,zzstsgl,ZZSZCYJ,ZZSTSNRDM,XFSGL,XFSZCYJ,XFSTSNRDM,TJJBM,SPPM,QYSJ,GDQJZSJ,BBH,rksj,istj)
select spbm_seq01.nextval,spbm,spmc,SM,zzssl,hzx,kyzt,gjz,zzstsgl,ZZSZCYJ,ZZSTSNRDM,XFSGL,XFSZCYJ,XFSTSNRDM,TJJBM,SPPM,QYSJ,GDQJZSJ,BBH,v_xtdate,1 from (
select spbm,spmc,SM,zzssl,hzx,kyzt,gjz,zzstsgl,ZZSZCYJ,ZZSTSNRDM,XFSGL,XFSZCYJ,XFSTSNRDM,TJJBM,SPPM,QYSJ,GDQJZSJ,BBH from dm_gy_spbm where BBH=v_version);
bbh:=v_version||'|'||to_char(sysdate,'yyyy-mm-dd hh24:mi:ss');
czzt:=1;
commit;

exception
when others then
bbh:=v_version||'|'||to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')||'|'||sqlerrm;
rollback to savepoint pt;

end whspbm;
/

--17 创建存储过程 更新商品编码
create or replace procedure update_spbm (
v_spbm in VARCHAR2,
v_spmc in varchar2,
v_zzssl in varchar2,
v_KGB in varchar2,
v_KYZT in varchar2,
v_GJZ in varchar2,
v_ZZSTSGL in varchar2,
v_ZZSZCYJ in varchar2,
v_ZZSTSNRDM in varchar2,
v_XFSGL in varchar2,
v_XFSZCYJ in varchar2,
v_XFSTSNRDM in varchar2,
v_TJJBM in varchar2,
v_SPPM in clob,
v_SM in varchar2,
v_SYSJ in date,
v_QYSJ in date,
v_FJBM in varchar2,
v_FBRDM in varchar2,
v_FBRXM in varchar2,
v_oper in varchar2,
czzt out number --该变量记录本次操作的状态。 0 成功 -1 失败
)

as
v_spbm_counts number;
v_version varchar2(20);
v_sytime date;
v_qytime date;
v_lwmc number;
begin
--首先检查商品编码是否重复。如果 重复不做修改;不重复 插入编码表

select count(1) into v_spbm_counts from dm_gy_spbm where spbm=v_spbm ;


select count(1) into v_lwmc from dm_gy_spbm where spbm<>v_spbm and spmc=v_spmc ;

if v_lwmc >0 then
czzt:= -2 ;
return;
end if;
--新增商品编码验证是否重复,修改商品编码必须存在编码
if (v_spbm_counts>0 and v_oper='0') or (v_spbm_counts=0 and v_oper='1') then
czzt:= -1 ;
return;

else
-- 取出当前最大版本号,往上递加1(注意区分 总局 省局。如果总局操作那么小数点左边递增1.如果省局操作 那么小数点右边递增1 )

czzt:=0;
v_sytime:=v_SYSJ;
v_qytime:=v_QYSJ;
if v_sytime is null or v_sytime='' then
v_sytime:=sysdate;
end if;

if v_qytime is null or v_qytime='' then
v_qytime:=sysdate;
end if;
--计算版本
select max(to_number(substr(bbh,1,instr(bbh,'.')-1)))+1 into v_version from dm_gy_spbm_bb;
if(v_version is null or v_version ='') then
v_version:='1.0';
end if;
if instr(v_version,'.')=0 then
v_version:=v_version||'.0';
end if;

insert into dm_gy_spbm_bb values(v_version,'',sysdate,v_FBRDM,v_FBRXM,v_sytime,v_qytime);

--新增商品商品编码
if v_oper='0' then
insert into dm_gy_spbm(spbm,spmc,SM,zzssl,HZX,kyzt,gjz,zzstsgl,ZZSZCYJ,ZZSTSNRDM,XFSGL,XFSZCYJ,XFSTSNRDM,TJJBM,SPPM,QYSJ,GDQJZSJ,FJBM,BBH,RKSJ)
values(v_spbm,v_spmc,v_SM,v_zzssl,v_kgb,v_kyzt,v_gjz,v_zzstsgl,v_ZZSZCYJ,v_ZZSTSNRDM,v_XFSGL,v_XFSZCYJ,v_XFSTSNRDM,v_TJJBM,v_SPPM,v_sytime,v_qytime,v_fjbm,v_version,sysdate) ;
end if;

if v_oper='1' then
update dm_gy_spbm set spmc=v_spmc,SM=v_SM,zzssl=v_zzssl,HZX=v_kgb,kyzt=v_kyzt,gjz=v_gjz,zzstsgl=v_zzstsgl,ZZSZCYJ=v_ZZSZCYJ,ZZSTSNRDM=v_ZZSTSNRDM,XFSGL=v_XFSGL,
XFSZCYJ=v_XFSZCYJ,XFSTSNRDM=v_XFSTSNRDM,TJJBM=v_TJJBM,SPPM=v_SPPM,QYSJ=v_sytime,GDQJZSJ=v_qytime,BBH=v_version
where spbm=v_spbm;
end if;

insert into dm_gy_spbm_bb_zb(JLXH,spbm,spmc,SM,zzssl,HZX,kyzt,gjz,zzstsgl,ZZSZCYJ,ZZSTSNRDM,XFSGL,XFSZCYJ,XFSTSNRDM,TJJBM,SPPM,QYSJ,GDQJZSJ,BBH,rksj)
values(spbm_seq01.nextval,v_spbm,v_spmc,v_SM,v_zzssl,v_kgb,v_kyzt,v_gjz,v_zzstsgl,v_ZZSZCYJ,v_ZZSTSNRDM,v_XFSGL,v_XFSZCYJ,v_XFSTSNRDM,v_TJJBM,v_SPPM,v_sytime,v_qytime,v_version,sysdate) ;

czzt:=1;
commit;

end if;

exception when others then
rollback;
dbms_output.put_line(sqlcode||' '||sqlerrm);
end update_spbm;
/

--18创建存储过程 监控优惠政策
create or replace procedure jkyhzc(spbmxml in clob,v_ZZSTSGL in varchar2,v_ZZSZCYJ in varchar2,v_ZZSTSNRDM in varchar2
,v_XFSGL in varchar2,v_XFSZCYJ in varchar2,v_XFSTSNRDM in varchar2,v_FBRDM in varchar2,v_FBRXM in varchar2,v_YHZCWHF in varchar2,v_FBSJ in varchar2,v_BBH in varchar2,czzt out number) is
begin

savepoint pt;


for xsk in ( SELECT
EXTRACTVALUE(VALUE(t),'/u/i') i--编码
, EXTRACTVALUE(VALUE(t),'/u/t') t-- 类型
FROM TABLE(XMLSEQUENCE(EXTRACT(
XMLTYPE(spbmxml), '/d/u')))
t)
loop

if(xsk.t='1') then
insert into dm_gy_spbm_yhzc (JLXH, BBH, SPBM,YHZCWHF,FBSJ,FBRDM,FBRXM,XFSGL,XFSZCYJ,XFSTSNRDM,ZZSTSGL,ZZSZCYJ,ZZSTSNRDM,RKSJ)
select SEQ_YHZC.NEXTVAL,v_BBH,spbm,v_YHZCWHF,to_date(v_FBSJ,'yyyy/mm/dd hh24:mi:ss'),v_FBRDM,v_FBRXM,v_XFSGL,v_XFSZCYJ,v_XFSTSNRDM,v_ZZSTSGL,v_ZZSZCYJ,v_ZZSTSNRDM,sysdate
from(select spbm from dm_gy_spbm where SPBM like ''||xsk.i||'%');
elsif (xsk.t='2') then
insert into dm_gy_spbm_yhzc (JLXH, BBH, SPBM,YHZCWHF,FBSJ,FBRDM,FBRXM,XFSGL,XFSZCYJ,XFSTSNRDM,ZZSTSGL,ZZSZCYJ,ZZSTSNRDM,RKSJ)
select SEQ_YHZC.NEXTVAL,v_BBH,spbm,v_YHZCWHF,to_date(v_FBSJ,'yyyy/mm/dd hh24:mi:ss'),v_FBRDM,v_FBRXM,v_XFSGL,v_XFSZCYJ,v_XFSTSNRDM,v_ZZSTSGL,v_ZZSZCYJ,v_ZZSTSNRDM,sysdate
from(select spbm from dm_gy_spbm where FJBM=xsk.i);
elsif (xsk.t='3') then
insert into dm_gy_spbm_yhzc (JLXH, BBH, SPBM,YHZCWHF,FBSJ,FBRDM,FBRXM,XFSGL,XFSZCYJ,XFSTSNRDM,ZZSTSGL,ZZSZCYJ,ZZSTSNRDM,RKSJ)
select SEQ_YHZC.NEXTVAL,v_BBH,spbm,v_YHZCWHF,to_date(v_FBSJ,'yyyy/mm/dd hh24:mi:ss'),v_FBRDM,v_FBRXM,v_XFSGL,v_XFSZCYJ,v_XFSTSNRDM,v_ZZSTSGL,v_ZZSZCYJ,v_ZZSTSNRDM,sysdate
from(select spbm from dm_gy_spbm where SPBM=xsk.i);
end if;

end loop;

 

czzt:=1;
commit;

exception
when others then
rollback to savepoint pt;

end jkyhzc;
/

--19创建存储过程 更新商品编码对照表
create or replace procedure spbm_zzstsgl(zzxml clob) is
begin
delete dm_gy_zzstsgl;

for xsk in (
SELECT
EXTRACTVALUE(VALUE(t),'/u/i') i--编码
, EXTRACTVALUE(VALUE(t),'/u/t') t-- 类型
, EXTRACTVALUE(VALUE(t),'/u/y') y-- 类型
, EXTRACTVALUE(VALUE(t),'/u/p') p-- 类型
FROM TABLE(XMLSEQUENCE(EXTRACT(
XMLTYPE(zzxml), '/d/u')))
t)
loop
insert into dm_gy_zzstsgl(zzstsglmc,sm,sl,sybz) values(xsk.i,xsk.t,xsk.y,xsk.p);
end loop;

commit;
end spbm_zzstsgl;

/

spool off

你可能感兴趣的:(数据库升级脚本-最终版哦)