Oracle的存储过程写法。

create or replace procedure org_school_convert(--replace procedure:存储过程。org_school_convert:这里是存储过程的名称
  IN_ORG_ID integer,    --这里都是需要的条件
  IN_ORG_TYPE integer,
  IN_REGION_ID integer,
  student out sys_refcursor  --out这里是返回值
)
as
 v_sql varchar2(4000);
begin    --开始

  if IN_ORG_TYPE=1 then    --判断语句输入的值是否为1
          dbms_output.put_line('将组织变为学校');

          --更新org表的org_type=1
          update base_organization set org_type=1,region_id=IN_REGION_ID where id= IN_ORG_ID;
          commit;

          --激活学校
          update base_school_list set data_flag=1,region_id=IN_REGION_ID where org_id= IN_ORG_ID;
           commit;
          
          -- 在学校表中添加一条记录
          insert into base_school_list(school_name,region_id,org_id,create_date, data_flag)
          select org_name,IN_REGION_ID,id,sysdate,1 from base_organization where data_flag=1
          and  not exists(select 1 from base_school_list where base_school_list.org_id=base_organization.id and base_school_list.data_flag=1 )
          and id=IN_ORG_ID;
          commit;


         --先将原来的教师数据data_flag=1
         update sch_teacher_list t set data_flag=1
        -- select * from sch_teacher_list t
         where exists( select 1 from base_school_list a
                                inner join base_organization org on org.id=a.org_id and org.data_flag=1
                                inner join base_login_user_info info on info.org_id=org.id and info.data_flag=1
                       where t.user_id=info.user_id and t.data_flag=1
                      and org.id=IN_ORG_ID
                     );
          
          commit;

          --将该组织下的用户复制一份教师表
          insert into sch_teacher_list(school_id,user_id)
          select a.id,info.user_id from base_school_list a
          inner join base_organization org on org.id=a.org_id and org.data_flag=1
          inner join base_login_user_info info on info.org_id=org.id and info.data_flag=1
          where a.data_flag=1
          and not exists(select 1 from sch_teacher_list t where t.user_id=info.user_id and t.data_flag=1)
          and org.id=IN_ORG_ID;

          commit;

  else

         dbms_output.put_line('将学校变为组织');
          --更新org表的org_type=0
          update base_organization set org_type=0,region_id=IN_REGION_ID  where id= IN_ORG_ID;
          commit;
         --更新学校的data_flag=0
         update base_school_list set data_flag=0 where org_id=IN_ORG_ID;
         commit;
         --将学校的用户信息data_flag=0
         update sch_teacher_list t set data_flag=0
        -- select * from sch_teacher_list t
         where exists( select 1 from base_school_list a
                                inner join base_organization org on org.id=a.org_id and org.data_flag=1
                                inner join base_login_user_info info on info.org_id=org.id and info.data_flag=1
                       where t.user_id=info.user_id and t.data_flag=1
                      and org.id=IN_ORG_ID
                     );

         commit;


  end if;


  --execute immediate v_sql3;
  --dbms_output.put_line('org_id='||org_id);
end;

这里是具体语句,需要自行理解。要看Mybatis的存储过程使用可以去我的文章看看。

猜你喜欢

转载自blog.csdn.net/weixin_40620337/article/details/82111949
今日推荐