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;

写的不好,望见谅

分享最近写的 两条sql语句的更多相关文章

  1. 题目:写出一条SQL语句,查询工资高于10000,且与他所在部门的经理年龄相同的职工姓名。

    create table Emp( eid char(20) primary key, ename char(20), age integer check (age > 0), did char ...

  2. 写出一条Sql语句:取出表A中第31到第40记录(SQLServer,以自动增长的ID作为主键,注意:ID可能不是连续的。

    答:解1:  select top 10 * from A where id not in (select top 30 id from A) 解2:  select top 10 * from A ...

  3. 曾经很长时间不会写的两个SQL语句(group by,having)

    1.统计各部门的平均工资,平均资金(要求同时显示出部门名称,部门编号,部门总人数) SQL)),) 部门平均工资,)),) 部门平均资金 FROM EMP E RIGHT JOIN DEPT D ON ...

  4. 写出一条Sql语句:取出表Customer中第31到第40记录(SQLServer,以自动增长的Id作为主键,注意:Id可能不是连续的。

    select top 10 * from (select ROW_NUMBER() over(order by Id) as rows,* from Customer) as C where C.ro ...

  5. 写出一条SQL语句:取出表A中第31到40行记录(SQLserver,以自增长的ID作为主键,注意: 一条Sql语句:取出表A中第31到第40记录

    解1: select top 10 * from A where id not in (select top 30 id from A) 解2: select top 10 * from A wher ...

  6. 使用传入的总记录数实现一条sql语句完成分页查询

    使用传入的总记录数实现一条sql语句完成分页查询     问题:在传统的分页查询的实现中不可避免的需要两条sql语句,一条用于查询数据一条用于查询总记录数.如下面的实际代码所示: Img1 当然如果使 ...

  7. 关联映射、关联查询【重点掌握一条SQL语句的那种方法】

    1 什么叫关联映射 通过数据库对象之间的关联关系(一对一.一对多.多对多),反映到实体对象上之间的引用. 举例 用户实体类(User):user_id user_name user_token 笔记本 ...

  8. CASE WHEN 及 SELECT CASE WHEN的用法(写了一坨烂代码发现两条sql就行了, 哎)

    转自:http://blog.sina.com.cn/s/blog_4c538f6c01012mzt.html Case具有两种格式.简单Case函数和Case搜索函数. 简单Case函数 CASE  ...

  9. [SQL]开启事物,当两条插入语句有出现错误的时候,没有错误的就插入到表中,错误的语句不影响到正确的插入语句

    begin transaction mustt insert into student values(,'kkk','j大洒扫','j','djhdjh') insert into student v ...

随机推荐

  1. GPL,LGPL和BSD等协议注意事项

    使用开源软件必须注意GPL,LGPL和BSD等协议 简而言之,GPL协议就是一个开放源代码协议,软件的初始开发者使用了GPL协议并公开软件的源程序后,后续使用该软件源程序开发软件者亦应当根据GPL协议 ...

  2. canvas-画图改进版

    前几天在canvas——画板中做了个很简陋的画板,只能画简单的线条,可以选择颜色和线条粗度,今天在此简陋的画板上增加了新的形状,撤销,保存,橡皮擦等功能,虽然功能还是很简单,刚接触canvas,过程中 ...

  3. Bzoj 2038: [2009国家集训队]小Z的袜子(hose) 莫队,分块,暴力

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 5763  Solved: 2660[Subm ...

  4. Eclipse中svn图标不显示

    在菜单栏中:windows ->preferences->General->Appearance->Lable Decorations 勾选其中的 SVN 项,最后应用确认之后 ...

  5. Telecasting station - SGU 114(带劝中位数)

    题目大意:在一个数轴上有N个点,每个点都有一个权值,在这个数轴上找一个点,是的每个点到这个点的距离之和乘上权值的总和最小. 分析:以前也遇到过类似的问题,不过并不知道这是带权值的中位数问题,百度百科有 ...

  6. nginx安装lua-nginx-module模块

    转载注明地址:http://www.cnblogs.com/dongxiao-yang/p/5312285.html 本文主要采用手动源码安装的方式将lua-nginx模块编译到nginx源码内部 一 ...

  7. jsp页面写入中文到mysql时出现了乱码(转)

    今天自己在用jsp把中文写入mysql的时候出现乱码,从数据库中读取出来的时候也显示为“??”,感觉应该出现了编码转换过程中的字符信息丢失.然后在mysql中直接执行该命令,发现中文是正常的,所有认为 ...

  8. delphi TColorDialog

    TColorDialog 预览          实现过程 动态创建和使用颜色对话框 function ShowColorDlg:TColor;begin  with TColorDialog.Cre ...

  9. java websocket工具

    https://github.com/dzharvis/servlet-websocket-server http://redstarofsleep.iteye.com/blog/1488639 ht ...

  10. Objective-C--@property,@synthesize关键字介绍

    Objective-C–@property,@synthesize关键字介绍 转载:http://www.cnblogs.com/QM80/p/3576282.html /** 注意:由@proper ...