分享最近写的 两条sql语句

时间:2023-03-09 07:10:20
分享最近写的 两条sql语句

1. 搭建基本环境

插入测试数据

insert into jgdm (jgdm,jgmc)  values('12300000000','河南省');
insert into jgdm (jgdm,jgmc) values('12300000005','河南省郑州市');
commit;

insert into jgdm(jgdm,jgmc) values(00300000000,'重庆市');
insert into jgdm(jgdm,jgmc) values(00300000001,'重庆市石柱县');
insert into jgdm(jgdm,jgmc) values(00300000004,'重庆市綦江区');
commit;

2. 分情况 更新 表

declare
 begin
for v_jgmc in  (select jgmc  from jgdm )  loop      
dbms_output.put_line('更新省的相关信息');
 if  instr(v_jgmc.jgmc,'省')<> 0 and  ( instr(v_jgmc.jgmc,'县')=0  or  instr(v_jgmc.jgmc,'区')=0    and  instr(v_jgmc.jgmc,'市')<>0  )  then  
       
 --1. 更新 机构级次代码
--把 '''||v_jgmc.jgmc||''' 中 有 ’省‘ 并且没有 '市'的 jgjcdm 更新为 00
execute immediate  'update jgdm set jgjcdm=''00'' where instr('''||v_jgmc.jgmc||''',''省'')<>0 and   instr('''||v_jgmc.jgmc||''',''市'')=0';
--把 '''||v_jgmc.jgmc||''' 中 有 ’省‘ 并且有 '市'的 jgjcdm 更新为 01
execute immediate  'update jgdm set jgjcdm=''01'' where instr('''||v_jgmc.jgmc||''',''省'')<>0 and   instr('''||v_jgmc.jgmc||''',''市'')<>0';
--2. 更新 上级机构代码
--把 '''||v_jgmc.jgmc||''' 中 有 ’省‘ 并且没有 '市'的 sjjgdm 更新为 自己的 jgdm
execute immediate  'update jgdm set sjjgdm=(select jgdm from jgdm where  instr('''||v_jgmc.jgmc||''',''省'')<>0 and   instr('''||v_jgmc.jgmc||''',''市'')=0 and instr('''||v_jgmc.jgmc||''',jgjc)<>0) ';
--把 '''||v_jgmc.jgmc||''' 中 有 ’省‘ 并且有 '市'的 sjjgdm 更新为 自己的 jgdm
--execute immediate  'update jgdm set sjjgdm=(select jgdm from jgdm where  instr('''||v_jgmc.jgmc||''',''省'')<>0 and   instr('''||v_jgmc.jgmc||''',''市'')<>0 )';
--3. 更新 机构简称
--把 '''||v_jgmc.jgmc||''' 中 有 ’省‘ 并且没有 '市'的 jgjc 更新为 自己的'''||v_jgmc.jgmc||'''
execute immediate  'update jgdm set jgjc='''||v_jgmc.jgmc||''' where instr('''||v_jgmc.jgmc||''',''省'')<>0 and   instr('''||v_jgmc.jgmc||''',''市'')=0';
--把 '''||v_jgmc.jgmc||''' 中 有 ’省‘ 并且有 '市'的 jgjc 更新为 自己所属的市
execute immediate  'update jgdm set jgjc= substr('''||v_jgmc.jgmc||''',instr('''||v_jgmc.jgmc||''',''省'')+1,length('''||v_jgmc.jgmc||''')-instr('''||v_jgmc.jgmc||''',''省'')) where instr('''||v_jgmc.jgmc||''',''省'')<>0 and   instr('''||v_jgmc.jgmc||''',''市'')<>0';
commit;
end if;     
       
dbms_output.put_line('更新直辖市的相关信息');       
 if  instr('''||v_jgmc.jgmc||''','省')=0 and  ( instr('''||v_jgmc.jgmc||''','县')<>0  or  instr('''||v_jgmc.jgmc||''','区')<>0    and  instr('''||v_jgmc.jgmc||''','市')<>0  )  then
    --   dbms_output.put_line(v_'''||v_jgmc.jgmc||'''.'''||v_jgmc.jgmc||''');       
--1. 更新 机构级次代码
--把 '''||v_jgmc.jgmc||''' 中 没有 ’省、县、区‘ 并且有 '市'的 jgjcdm 更新为 00
execute immediate  'update jgdm set jgjcdm=''00'' where instr('''||v_jgmc.jgmc||''',''省'')=0 and   instr('''||v_jgmc.jgmc||''',''县'')=0  and  instr('''||v_jgmc.jgmc||''',''区'')=0    and  instr('''||v_jgmc.jgmc||''',''市'')<>0 ';
--把 '''||v_jgmc.jgmc||''' 中 有 ’市、县、区‘  并且没有 '省' 的 jgjcdm 更新为 01
execute immediate  'update jgdm set jgjcdm=''01'' where   ( instr('''||v_jgmc.jgmc||''',''县'')<>0  or  instr('''||v_jgmc.jgmc||''',''区'')<>0 )   and  instr('''||v_jgmc.jgmc||''',''市'')<>0 ';
--2. 更新 上级机构代码
--把 '''||v_jgmc.jgmc||''' 中 有 ’市‘ 并且没有 ’省、县、区' 的 sjjgdm 更新为 自己的 jgdm
execute immediate  'update jgdm t2 set sjjgdm=(select jgdm from jgdm t1 where  (instr('''||v_jgmc.jgmc||''',''省'')=0 and   instr('''||v_jgmc.jgmc||''',''县'')=0  and  instr('''||v_jgmc.jgmc||''',''区'')=0)    and  instr('''||v_jgmc.jgmc||''',''市'')<>0  ) ';
--把 '''||v_jgmc.jgmc||''' 中 有 ’省‘ 并且有 '市'的 jgjc 更新为 自己所属的市
execute immediate  'update jgdm set jgjc= substr('''||v_jgmc.jgmc||''',instr('''||v_jgmc.jgmc||''',''市'')+1,length('''||v_jgmc.jgmc||''')-instr('''||v_jgmc.jgmc||''',''市'')) where instr('''||v_jgmc.jgmc||''',''省'')=0 and   instr('''||v_jgmc.jgmc||''',''市'')<>0 ';
--3. 更新 机构简称
--把 '''||v_jgmc.jgmc||''' 中  有 '市' 并且没有 '县','区',‘省‘ 并且的 jgjc 更新为 自己的'''||v_jgmc.jgmc||'''
execute immediate  'update jgdm set jgjc='''||v_jgmc.jgmc||''' where instr('''||v_jgmc.jgmc||''',''市'')<>0 and  (  instr('''||v_jgmc.jgmc||''',''县'')=0  and  instr('''||v_jgmc.jgmc||''',''区'')=0   ) ';
--把 '''||v_jgmc.jgmc||''' 中 有 ’市‘ 并且有  '县' '区'的 jgjc 更新为 自己所属的市
execute immediate  'update jgdm set jgjc= substr('''||v_jgmc.jgmc||''',instr('''||v_jgmc.jgmc||''',''市'')+1,length('''||v_jgmc.jgmc||''')-instr('''||v_jgmc.jgmc||''',''市'')) where instr('''||v_jgmc.jgmc||''',''市'')<>0 and   (instr('''||v_jgmc.jgmc||''',''县'')<>0 or  instr('''||v_jgmc.jgmc||''',''区'')<>0 ) ';
 commit;
 
 end if;
 end loop;
 end;

写的不好,望见谅