1。臨時表
1drop
procedure2if
exists
`p_getmonitorpeople`;
3create
procedure p_getmonitorpeople (in fgid int, in mins int,in lens int)4
begin
5if fgid>
0&&mins>
0&&lens>
0then
6begin7--
建表tb_temp1
8drop
table
ifexists
tb_temp1;
9create
temporary
table
tb_temp1(
10 `tmp_id` int unsigned not
null
auto_increment,
11 `gatherid` int
notnull
,12 `phonemac` varchar(12) not
null
,13 `number` int
notnull,14
primary
key(`tmp_id`)
15 ) engine = myisam default charset =
utf8;
16--
填充tb_temp1
17insert
into tb_temp1(gatherid,phonemac,number)18
select gatherid, phonemac,count(phonemac) as
number
from(19
select
distinct s.gatherid,s.phonemac from
20left
join tb_web_config_gather g on g.gatherid=
s.gatherid
21where s.logtime between date_sub(now(),interval mins minute) and now() and g.floorid=(select floorid from tb_web_floors where groupid=
fgid)
22order
by s.logtime) as tb group
by phonemac order
bynumber
desc
limit lens;
2324
select
distinct g.gatherid, t.phonemac,g.locationx,g.locationy from
tb_temp1 t
25left
join tb_web_config_gather g on g.gatherid=
t.gatherid;
26end;27
endif;28
#銷毀記憶體表
29drop
table
ifexists
tb_temp1;
30end
3132
--顯示
33 call p_getmonitorpeople(3,1,50);
2。游標
dropprocedure
ifexists
p_getmonitordata;
create
procedure p_getmonitordata(fgid int,mins int,lens int,mac varchar(12
))begin
declare tmpid int
;declare gid varchar(10
) ;declare done int
default
false;
declare pregatherid int
default0;
declare
strvarchar(200
);--
定義游標
declare rs cursor
forselect tmp_id,gatherid from
tb_temp2;
--錯誤定義,標記迴圈結束
declare
continue handler for
not found set done=
true;
--開啟游標
open
rs;--
迴圈執行
repeat
fetch rs into
tmpid,gid ;
ifnot done then
#執行操作
if pregatherid=gid && pregatherid <>
0then
delete
from tb_temp2 where tmp_id=
tmpid;
else
set pregatherid=
gid;
--set str=concat(gid,str);
endif
;endif;
#當_done
=false時退出被循
until done
endrepeat;
/*關閉游標
*/close
rs;select
*from
tb_temp2;
set done=
false;#只有定義為false,新的迴圈才能繼續。
end
儲存過程示例 在儲存過程中使用臨時表
create or replace procedure product temp update prc aspc delestr varchar2 50 刪除臨時表記錄語句 pc createstr varchar2 500 建立臨時表 tabext varchar2 10 用於判斷臨時表是否存在中...
SQL在儲存過程中使用臨時表
1 create or replace procedure product temp update prc as 2 pc delestr varchar2 50 刪除臨時表記錄語句 3 pc createstr varchar2 500 建立臨時表 4 tabext varchar2 10 用於判...
Oracle儲存過程中使用臨時表的替代方案
近日接手乙個分析類老專案改造工作,使用者要求使用oracle資料庫 原先版本為sql server2005 由於原專案中大量使用儲存過程實現複雜的業務資料查詢,在sql server中由於使用錶值函式 臨時表等非常方便,所以當時實現起來並不費事,而現在轉為oracle就不一樣了,限制太多 說實話 甲...