oracle 存储过程 转换为mysql存储过程 实例总结 (数据库有感篇一)

前端之家收集整理的这篇文章主要介绍了oracle 存储过程 转换为mysql存储过程 实例总结 (数据库有感篇一)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近在接触数据库的有关知识,因为水平有限,对数据库方面的一些知识缺乏了解,这次遇见的主要是 数据库的存储过程,根据公司项目需求,将oracle的存储过程切换为MysqL的存储过程,首先oracle的存储过程与MysqL的存储过程差别是很大的(语法差别很大),通过好多天的网上查询资料,与同事们的交流,简单的实现了基本的转换,在这里肯定还有一些错误的地方,也欢迎各位大牛指出,让我更加深入的了解 数据库的存储过程,有疑问的大家也可以一起交流进步。

下面就进入这次的话题:oracle 存储过程 转换为MysqL存储过程 实例总结


首先我门先看一下我最近大致转换的存储过程的一个例子代码,首先是原始的 oracle的存储过程:


  1. CREATE OR REPLACE PROCEDURE xp_stat_BigDiseaseAnalyse(
  2. v_beginDate varchar2,--统计开始日期,格式:yyyyMMdd
  3. v_endDate varchar2,--统计结束日期,格式:yyyyMMdd
  4. v_orgCode varchar2,--检查医疗机构
  5. v_dataCur out PK_base.cur,--返回结果集
  6. v_total out number,--返回查询记录总数
  7. v_msg out varchar2 --返回查询记录总数
  8. ) is
  9. v_sql varchar2(5000);
  10. begin
  11. v_sql := 'select * from ( select max(ehr.forg_code) forg_code,''冠心病'' fdiag_name,count(a.fehrid) FDiseaseCount,sum(famount)
  12.  
  13. famount,sum(famount_insurance) famount_insurance,sum(famount_self) famount_self
  14. from tbl_ehr_comm_disease a
  15. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  16. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  17. where ehr.fdata_type=''3001''
  18. and a.fdiag_name like ''%冠心病%''
  19. and ehr.fehr_date >= ''20160301''
  20. and ehr.fehr_date <= ''' || v_endDate || ''' ';
  21. if v_orgCode is not null then
  22. v_sql := v_sql || ' and ehr.forg_code =''' || v_orgCode || '''';
  23. end if;
  24. v_sql := v_sql || ' union select max(ehr.forg_code) forg_code,''椎基底供血不足'' fdiag_name,count(a.fehrid)
  25.  
  26. FDiseaseCount,sum(famount) famount,sum(famount_self) famount_self
  27. from tbl_ehr_comm_disease a
  28. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  29. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  30. where ehr.fdata_type=''3001'' and a.fdiag_name like ''%椎基底供血不足%''
  31. and ehr.fehr_date >= ''20160301''
  32. and ehr.fehr_date <= ''' || v_endDate || ''' ';
  33. if v_orgCode is not null then
  34. v_sql := v_sql || ' and ehr.forg_code =''' || v_orgCode || '''';
  35. end if;
  36. v_sql := v_sql || ' union select max(ehr.forg_code) forg_code,''糖尿病'' fdiag_name,sum
  37.  
  38. (famount) famount,sum(famount_self) famount_self
  39. from tbl_ehr_comm_disease a
  40. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  41. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  42. where ehr.fdata_type=''3001'' and a.fdiag_name like ''%糖尿病%''
  43. and ehr.fehr_date >= ''20160301''
  44. and ehr.fehr_date <= ''' || v_endDate || ''' ';
  45. if v_orgCode is not null then
  46. v_sql := v_sql || ' and ehr.forg_code =''' || v_orgCode || '''';
  47. end if;
  48. v_sql := v_sql || ' union select max(ehr.forg_code) forg_code,''脑出血'' fdiag_name,sum(famount_self) famount_self
  49. from tbl_ehr_comm_disease a
  50. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  51. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  52. where ehr.fdata_type=''3001'' and a.fdiag_name like ''%脑出血%''
  53. and ehr.fehr_date >= ''20160301''
  54. and ehr.fehr_date <= ''' || v_endDate || ''' ';
  55. if v_orgCode is not null then
  56. v_sql := v_sql || ' and ehr.forg_code =''' || v_orgCode || '''';
  57. end if;
  58. v_sql := v_sql || ' union select max(ehr.forg_code) forg_code,''无指征剖腹产分娩'' fdiag_name,sum(famount_self) famount_self
  59. from tbl_ehr_comm_disease a
  60. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  61. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  62. where ehr.fdata_type=''3001'' and a.fdiag_name like ''%无指征剖腹产分娩%''
  63. and ehr.fehr_date >= ''20160301''
  64. and ehr.fehr_date <= ''' || v_endDate || ''' ';
  65. if v_orgCode is not null then
  66. v_sql := v_sql || ' and ehr.forg_code =''' || v_orgCode || '''';
  67. end if;
  68. v_sql := v_sql || ' union select max(ehr.forg_code) forg_code,''肺炎'' fdiag_name,sum(famount_self) famount_self
  69. from tbl_ehr_comm_disease a
  70. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  71. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  72. where ehr.fdata_type=''3001'' and a.fdiag_name like ''%肺炎%''
  73. and ehr.fehr_date >= ''20160301''
  74. and ehr.fehr_date <= ''' || v_endDate || ''' ';
  75. if v_orgCode is not null then
  76. v_sql := v_sql || ' and ehr.forg_code =''' || v_orgCode || '''';
  77. end if;
  78. v_sql := v_sql || ' union select max(ehr.forg_code) forg_code,''肺恶性肿瘤'' fdiag_name,sum(famount_self) famount_self
  79. from tbl_ehr_comm_disease a
  80. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  81. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  82. where ehr.fdata_type=''3001'' and a.fdiag_name like ''%肺恶性肿瘤%''
  83. and ehr.fehr_date >= ''20160301''
  84. and ehr.fehr_date <= ''' || v_endDate || ''' ';
  85. if v_orgCode is not null then
  86. v_sql := v_sql || ' and ehr.forg_code =''' || v_orgCode || '''';
  87. end if;
  88. v_sql := v_sql || ' union select max(ehr.forg_code) forg_code,''高血压'' fdiag_name,sum(famount_self) famount_self
  89. from tbl_ehr_comm_disease a
  90. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  91. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  92. where ehr.fdata_type=''3001'' and a.fdiag_name like ''%高血压%''
  93. and ehr.fehr_date >= ''20160301''
  94. and ehr.fehr_date <= ''' || v_endDate || ''' ';
  95. if v_orgCode is not null then
  96. v_sql := v_sql || ' and ehr.forg_code =''' || v_orgCode || '''';
  97. end if;
  98. v_sql := v_sql || ' union select max(ehr.forg_code) forg_code,''乳腺恶性肿瘤'' fdiag_name,sum(famount_self) famount_self
  99. from tbl_ehr_comm_disease a
  100. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  101. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  102. where ehr.fdata_type=''3001'' and a.fdiag_name like ''%乳腺恶性肿瘤%''
  103. and ehr.fehr_date >= ''20160301''
  104. and ehr.fehr_date <= ''' || v_endDate || ''' ';
  105. if v_orgCode is not null then
  106. v_sql := v_sql || ' and ehr.forg_code =''' || v_orgCode || ''' ) tt where tt.FDiseaseCount>0 order by tt.FDiseaseCount desc';
  107. end if;
  108. open v_dataCur for v_sql;
  109. exception
  110. when others then
  111. pk_base.xp_saveLog(sqlcode,sqlerrm,'pk_stat.xp_stat_BigDiseaseAnalyse','大病补偿统计出错');
  112. open v_dataCur for
  113. select * from dual where 1 = 2;
  114. end;



下面是 经过大致转换的的MysqL 存储过程 :


  1. DROP PROCEDURE IF EXISTS xp_stat_BigDiseaseAnalyse;
  2. CREATE PROCEDURE xp_stat_BigDiseaseAnalyse(
  3. v_beginDate varchar(100),-- 统计开始日期,格式:yyyyMMdd
  4. v_endDate varchar(100),-- 统计结束日期,格式:yyyyMMdd
  5. v_orgCode varchar(50),-- 检查医疗机构
  6. -- v_dataCur PK_base.cur,--返回结果集
  7. out v_total int,-- 返回查询记录总数
  8. out v_msg varchar(100) -- 返回查询记录总数
  9. )
  10. begin
  11. DECLARE<span style="white-space:pre"> </span>v_sql varchar(5000);
  12. DECLARE iPos int;
  13. DECLARE iCount int;
  14. DECLARE v_sqltmp varchar(420);
  15.  
  16.  
  17. DECLARE CONTINUE HANDLER FOR sqlEXCEPTION
  18. begin
  19. CALL xp_saveLog(@sqlcode,@sqlerrm,'pk_him.xp_stat_BigDiseaseAnalyse','大病补偿统计出错');
  20. COMMIT;
  21. end;
  22. -- =================================================== 异常处理结束
  23.  
  24.  
  25.  
  26. SET v_sql = CONCAT(' select * from (select max(ehr.forg_code) forg_code,SUM(famount)
  27. famount,SUM(famount_insurance) famount_insurance,SUM(famount_self) famount_self
  28. from tbl_ehr_comm_disease a
  29. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  30. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  31. where ehr.fdata_type=''3001''
  32. and a.fdiag_name like ''%冠心病%''
  33. and ehr.fehr_date >= ''',v_endDate,'''');
  34.  
  35.  
  36. SET v_sql = CONCAT(v_sql,v_beginDate,' '' and ehr.fehr_date <= ''','''');
  37.  
  38.  
  39. if (v_orgCode is not null and v_orgCode !='' ) then
  40. -- SET v_sql = v_sql || ' and ehr.forg_code =''' || v_orgCode || '''';
  41. SET v_sql = CONCAT(v_sql,' and ehr.forg_code = ''',v_orgCode,'''');
  42. end if;
  43.  
  44.  
  45.  
  46.  
  47. -- =================================================脑出血=======================================================
  48. SET v_sqltmp = CONCAT(' union select max(ehr.forg_code) forg_code,sum(famount_self) famount_self
  49. from tbl_ehr_comm_disease a
  50. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  51. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  52. where ehr.fdata_type=''3001'' and a.fdiag_name like ''%脑出血%''
  53. and ehr.fehr_date >=''','''');
  54.  
  55.  
  56.  
  57.  
  58. SET v_sql = CONCAT(v_sql,v_sqltmp,'''');
  59. end if;
  60.  
  61.  
  62. -- ==========================================椎基底供血不足==============================================================
  63. SET v_sqltmp = CONCAT(' union select max(ehr.forg_code) forg_code,count(a.fehrid)FDiseaseCount,sum(famount_self) famount_self
  64. from tbl_ehr_comm_disease a
  65. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  66. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  67. where ehr.fdata_type=''3001'' and a.fdiag_name like ''%椎基底供血不足%''
  68. and ehr.fehr_date >=''','''');
  69. end if;
  70.  
  71.  
  72.  
  73.  
  74. -- ========================================糖尿病================================================================
  75. SET v_sqltmp = CONCAT(' union select max(ehr.forg_code) forg_code,sum(famount_self) famount_self
  76. from tbl_ehr_comm_disease a
  77. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  78. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  79. where ehr.fdata_type=''3001'' and a.fdiag_name like ''%糖尿病%''
  80. and ehr.fehr_date >=''',''' ');
  81.  
  82.  
  83.  
  84.  
  85. SET v_sql = CONCAT(v_sql,'''');
  86. end if;
  87.  
  88.  
  89.  
  90.  
  91. -- ========================================================================================================
  92. SET v_sqltmp = CONCAT('union select max(ehr.forg_code) forg_code,sum(famount_self) famount_self
  93. from tbl_ehr_comm_disease a
  94. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  95. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  96. where ehr.fdata_type=''3001'' and a.fdiag_name like ''%无指征剖腹产分娩%''
  97. and ehr.fehr_date >= ''','''');
  98. end if;
  99.  
  100.  
  101.  
  102.  
  103. -- =======================================================肺炎=================================================
  104. SET v_sqltmp = CONCAT(' union select max(ehr.forg_code) forg_code,sum(famount_self) famount_self
  105. from tbl_ehr_comm_disease a
  106. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  107. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  108. where ehr.fdata_type=''3001'' and a.fdiag_name like ''%肺炎%''
  109. and ehr.fehr_date >=''','''');
  110. end if;
  111.  
  112.  
  113.  
  114.  
  115. -- ===============================================肺恶性肿瘤=========================================================
  116. SET v_sqltmp = CONCAT(' union select max(ehr.forg_code) forg_code,sum(famount_self) famount_self
  117. from tbl_ehr_comm_disease a
  118. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  119. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  120. where ehr.fdata_type=''3001'' and a.fdiag_name like ''%肺恶性肿瘤%''
  121. and ehr.fehr_date >=''','''');
  122. end if;
  123.  
  124.  
  125. -- ================================================高血压========================================================
  126. SET v_sqltmp = CONCAT(' union select max(ehr.forg_code) forg_code,sum(famount_self) famount_self
  127. from tbl_ehr_comm_disease a
  128. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  129. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  130. where ehr.fdata_type=''3001'' and a.fdiag_name like ''%高血压%''
  131. and ehr.fehr_date >=''','''');
  132. end if;
  133.  
  134.  
  135. -- ============================================乳腺恶性肿瘤============================================================
  136. SET v_sqltmp = CONCAT(' union select max(ehr.forg_code) forg_code,count(a.fehrid)
  137.  
  138.  
  139. FDiseaseCount,sum(famount_self) famount_self
  140. from tbl_ehr_comm_disease a
  141. join tbl_ehr_archives ehr on ehr.fid=a.fehrid
  142. join tbl_ehr_zy_payway pay on pay.fehrid=a.fehrid
  143. where ehr.fdata_type=''3001'' and a.fdiag_name like ''%乳腺恶性肿瘤%''
  144. and ehr.fehr_date >=''',''' ');
  145.  
  146.  
  147. SET v_sql = CONCAT(v_sql,'''');
  148.  
  149.  
  150. if (v_orgCode is not null and v_orgCode !='' ) then
  151. SET v_sql = CONCAT(v_sql,' '' ');
  152. end if;
  153.  
  154.  
  155.  
  156.  
  157. SET v_sql = CONCAT(v_sql,' ) tt order by tt.FDiseaseCount desc');
  158.  
  159.  
  160. -- where tt.FDiseaseCount>0
  161. -- 创建临时表
  162. create temporary table if not exists tmpTab(
  163. FID int not null auto_increment,forg_code varchar(20),fdiag_name varchar(50),fDiseaseCount decimal(19,0),fAmount decimal(19,2),famount_insurance decimal(19,famount_self decimal(19,primary key (FID)
  164. ) ;
  165.  
  166.  
  167. truncate TABLE tmpTab;
  168. -- SET v_sql = 'insert into tmpTab(forg_code,fdiag_name,FDiseaseCount,famount,famount_insurance,famount_self) ' || v_sql;
  169. SET v_sql = CONCAT('insert into tmpTab(forg_code,famount_self) ',v_sql);
  170.  
  171.  
  172. set @s_sql = v_sql;
  173. prepare stmt from @s_sql;
  174. execute stmt;
  175.  
  176.  
  177.  
  178.  
  179. SELECT * from tmpTab;
  180.  
  181.  
  182. end


通过大致阅读上面的代码,我们可以发现,不同的地方有很多,不单单从字面上,从逻辑上也有很多不同之处。

为了更加直观的说明不同之处,下面列出一个表格来大致说明:

oracle 与 MysqL 存储过程的区别
区别 oracle MysqL
创建存储过程的语句不同 CREATE OR REPLACE PROCEDURE 存储过程名称() DROP PROCEDURE IF EXISTS xp_stat_BigDiseaseAnalyse;
CREATE PROCEDURE 存储过程名称()
参数的不同(参数类型,出参入参格式,) 1.入参(类型前加in 可以省略不写),出参必须在参数类型前加 out
2.字符类型为varchar2,后边不用初始化参数大小;
数字类型为number,不用初始化参数大小;
decimal类型,decimal(5,2)表示位数一共有5位,小数点后有两位;
PK_base.cur 为游标类型。
其他的数据类型请参考:oracle数据类型
1.入参(类型前加in也可以省略不写),出参必须在参数前加out(注意与oracle的区别)
2.字符类型为varchar,后面必须初始化参数大小;
数字类型有int,bigint等;
decimal类型,这个和oracle一样。
详细的其他的数据类型请参考:mysql数据类型
变量的定义方式 1.定义:直接在存储过程的的创建语句中声明变量,以及类型。
2.变量的赋值:变量名 := 赋值内容 即可。
1.定义:使用declare语句,例如: declare v_sql varchar(5000)
定义一个大小为5000 字符变量 v_sql
2.变量的赋值: set 变量名= 赋值内容
字符串连接的方式 oracle中使用 || 连接字符串,
例如:v_sql :='woshi' || 'zifuchuan'
则,v_sql的值为 woshizifuchuan
MysqL中字符串的连接使用caoncat()函数
例如: set v_sql=concat('woshi','zifuchuan')
v_sql 的值为 woshizifuchuan
游标的问题 详细参见:oracle游标使用 详细参见:mysql游标使用
分页查询的问题 oracle中分页查询主要依赖于rownum(它是一个Oracle中的系统变量,无需定义即可使用,有特定的含义)。详细的分页查询例子参见:
oracle分页查询

MysqL中的分页查询,主要依赖limit语句来实现 limit 1,10 意思就是从第一行开始查询查询10个记录。例如:

MysqL分页查询有两种方式,一种是使用COUNT(*)的方式,具体代码如下
SELECT COUNT(*) FROM foo WHERE b = 1;

SELECT a FROM foo WHERE b = 1 LIMIT 100,10;
另外一种是使用sql_CALC_FOUND_ROWS
SELECT sql_CALC_FOUND_ROWS a FROM foo WHERE b = 1 LIMIT 100,10;
SELECT FOUND_ROWS();

第二种方式调用sql_CALC_FOUND_ROWS之后会将WHERE语句查询的行数放在FOUND_ROWS()之中,第二次只需


查询FOUND_ROWS()就可以查出有多少行了。

异常处理 当存储过程语句执行错误,或者查询不到数据等情况出现时,就该设置异常,对异常进行处理。
oracle异常处理详见:oracle异常处理

当存储过程语句执行错误,或者查询不到数据等情况出现时,就该设置异常,对异常进行处理。
MysqL异常处理详见:mysql异常处理
条件语句 if then else 等 具体实例参考:oracle if语句 详见:mysql中if语句的使用
函数的区别 (具体如日期转换函数等) 有时涉及到日期的转换(比如格式),有时要返回一个日期中的月份,第几天等等,都需要使用函数来实现.详见:oracle函数小结 本次涉及到的MysqL函数有concat,以及一些日期转换函数
其他的函数参考:mysql日期函数小结
单引号,双引号问题 详见:oracle单引号双引号 详见:mysql单引号双引号
注释的问题 oracle注释: --注释内容 MysqL注释: -- 注释内容 (--后面比oracle多一个空格)
可否打包 oracle的存储过程可以,装在一个包中,即一个包中可以存在多个存储过程。 MysqL不可以打包



通过上述大致的介绍,也许会有点收获,但是错误之处请各位指出。

猜你在找的Oracle相关文章