標籤:mysql 預存程序 oracle
mysql 、oracle預存程序文法區別
1、 條件陳述式:mysql使用elseif關鍵字,oracle是elsif關鍵字;
oracle:
if運算式 then
運算式;
elsif
運算式;
endif;
mysql:
if運算式then
運算式;
elseif
運算式;
endif;
2、 字串串連
oracle使用 || ;
mysql 使用concat函數;
3、 日期計算(年月日數)
mysql:
函數TimeStampDiff()是MySQL本身提供的可以計算兩個時間間隔的函數,文法為:TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2),其中unit單位有如下幾種,分別是:SECOND, MINUTE, HOUR, DAY,WEEK, MONTH, QUARTER, or YEAR。
目前時間:sysdate()
字元轉日期:str_to_date() 分隔字元一致,年月日要一致;樣本:
select str_to_date(‘2008-4-2 15:3:28‘,‘%Y-%m-%d%H:%i:%s‘);
日期轉字元:DATE_FORMAT(date,format)
SELECT DATE_FORMAT(sysdate(), ‘%Y-%m-%d %H:%i:%s‘);
數字轉字元:concat(num,’’)
oracle:
months_between 求日期間隔月份,除以12即為間隔年份;
天數,只需要日期直接相減;
目前時間:sysdate
字元轉日期:to_date()
日期轉字元:to_char(date,format) to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘)
數字轉字元:to_char(num)
4、 定義遊標
oracel:
CURSOR curPlanIndex is
SELECT a.INDEX_SCORE
,c.enum_value,c.dn_value,c.up_value,c.score,c.score_desc
FROM eval_plan_index a
JOIN eval_index_score c onc.index_id=a.index_id and a.plan_id = c.plan_id
WHERE a.plan_id = V_PLAN_ID and a.index_id= V_INDEX_ID
order by dn_value;
MYSQL:
declare curPlanIndex cursor for
SELECT a.INDEX_SCORE
,c.enum_value,c.dn_value,c.up_value,c.score,c.score_desc
FROM eval_plan_index a
JOIN eval_index_score c onc.index_id=a.index_id and a.plan_id = c.plan_id
WHERE a.plan_id = V_PLAN_ID and a.index_id= V_INDEX_ID
order by dn_value;
5、 selectinto 賦值
oracle 有exception錯誤處理
begin
select value_name into vc_num_unit fromsys_dict
where dict_code = ‘szdw‘ and value_code =v_num_unit and rownum <=1 ;
exception
when no_data_found then
vc_num_unit := ‘‘;
end;
mysql 如果select 沒有資料,則不執行into操作,變數值保持為上次結果,需要手工重設。最好能limit 1;只返回一條資料;
mysql 、oracle預存程序文法區別