1,select curdate() /*2016-10-08*/
2,select date_sub(curdate(), interval 6 day) /*2016-10-02*/
3,case when then else 拼接條件查詢:
where
(case when ifnull(@belongcity,'0')='0' or @belongcity='' then 1 else
cast(locate(@belongcity,worker_order.belong_city) as signed )
end)=1
and (case when ifnull(@energystation,'0')='0' then 1 else
cast(locate(@energystation,worker_order.repository_id) as signed )
end)=1
and (case when ifnull(@servicestatioin,'0')='0' then 1 else
cast(locate(@servicestatioin,worker_order.station_id) as signed )
end)=1
and date_format(worker_order.create_time, '%y-%m-%d') between date_format(cast(@begindatetime as datetime), '%y-%m-%d') and date_format(cast(@enddatetime as datetime), '%y-%m-%d')
group by date_format(worker_order.create_time,'%y-%m-%d');
或者是這種:
where
locate((case when ifnull(@belongcity,'0')='0' or @belongcity='' then worker_order.belong_city else @belongcity end),worker_order.belong_city)>0
and
locate((case when ifnull(@energystation,'0')='0' then worker_order.repository_id else @energystation end),worker_order.repository_id)>0
and locate((case when ifnull(@servicestatioin,'0')='0' then worker_order.station_id else @servicestatioin end),worker_order.station_id)>0
and date_format(worker_order.create_time, '%y-%m-%d') between date_format(cast(@begindatetime as datetime), '%y-%m-%d') and date_format(cast(@enddatetime as datetime), '%y-%m-%d')
group by date_format(worker_order.create_time,'%y-%m-%d');
4,拼接starttime和endtime:
set @begindatetime=(
case
when isnull(begindatetime) then
date_sub(curdate(), interval 6 day)
else
date_format(begindatetime,'%y-%m-%d')
end); /*起始時間*/
set @enddatetime=(
case
when isnull(enddatetime) then
curdate()
else
date_format(enddatetime,'%y-%m-%d')
end); /*結束時間*/
5,建立儲存過程及呼叫儲存過程:
drop procedure if exists pro_query_work_order_report;
create procedure pro_query_work_order_report (
in citycode varchar(32),/*城市code*/
in energystation int,/*能源站code*/
in servicestatioin int,/*網點code*/
in begindatetime datetime,/*開始時間*/
in enddatetime datetime/*結束時間*/
)begin
set @belongcity = citycode;/*所屬城市code*/
set @energystation=energystation; /*能源站code*/
set @servicestatioin=servicestatioin; /*站點code*/
set @begindatetime=(
case
when isnull(begindatetime) then
date_sub(curdate(), interval 6 day)
else
date_format(begindatetime,'%y-%m-%d')
end); /*起始時間*/
set @enddatetime=(
case
when isnull(enddatetime) then
curdate()
else
date_format(enddatetime,'%y-%m-%d')
end); /*結束時間*/
select
date_format(worker_order.create_time,'%y-%m-%d') as create_time,/*建立時間*/
( select count(0)
from biz_work_order tmp
where
date_format(tmp.create_time,'%y-%m-%d')=date_format(worker_order.create_time,'%y-%m-%d')
or (tmp.create_time
6,呼叫:(mysql 使用call進行呼叫,且不支援預設引數;sql server 使用exec進行呼叫,支援預設引數,有預設值的引數可以不傳)
call pro_query_work_order_report('50010',null,270,date_sub(curdate(), interval 6 day),curdate())
發現mysql怎麼資料量大了,還沒有sql server好用。。。難道是我的幻覺麼。。
常用sql儲存過程集錦
1.給表中字段新增描述資訊 create table t2 id int,name char 20 go exec sp addextendedproperty ms description employee id user dbo,table t2,column id exec sp update...
mysql之sql儲存過程
儲存過程 解決某一特徵功能的sql語句集 儲存過程的建立 1 無入參無返回 drop procedure ifexists get student create procedure get student begin select from student end 呼叫儲存過程 call get s...
如何匯出Oracle的儲存過程為Sql語句
oracle中,過程 type 函式等物件的原始碼可以通過user source或dba source查詢 注意觀察查詢結果中,帶有line欄位,表示該段原始碼的行號。所以我們可以通過拼接 create or replace package 拼接查詢結果,生成建立過程 函式等的sql語句,舉例 se...