标签:style io os ar 使用 strong sp 数据 on
一、概述:
oracle数据库的临时表的特点:
临时表分为事务型和会话型
语法:
create global temporary table 临时表名
(
……
)
on commit [preserve|delete] rows;
而TRANSACTION级的临时表数据在TRANACTION结束后消失,即COMMIT/ROLLBACK或结束SESSION都会清除TRANACTION临时表数据。
二、示例:
1、创建临时表
declare tempisexist integer:=0;
begin
select count(*) into tempisexist from all_tables where table_name=‘NK_SLTJ‘;
if tempisexist=0 then--不存在临时表就创建一个
execute immediate(‘
CREATE GLOBAL TEMPORARY TABLE NK_SLTJ
(
LCK_FJNM varchar(36),
LCMC varchar(70),
GFX integer,
ZFX integer,
DFX integer,
KZDSL integer
)
on commit preserve rows‘ --preserve表示回话级。
);
end if;
end;
二、使用临时表
declare
FXZ NUMBER;
FJNM varchar(36);
ZZNM varchar(36):=‘77c48880-a2be-4d3c-97b7-26f8de0bee63‘;
CURSOR NKFXZcur is select NKFXJZ_FXZ,NKLCK_FJNM from NKFXJZ INNER JOIN NKLCK ON NKLCK_NM=NKFXJZ_LCNM where NKLCK_ZZNM=ZZNM;
begin
delete NK_SLTJ; --防止在统一会话中多次执行导致数据重复,因此程序一开始就应清空临时表数据
insert into NK_SLTJ SELECT NKLCK_FJNM, NKLCK_MC,0,0,0,KZD FROM --向临时表插入数据。
( select KZDSL.NKLCK_FJNM,KZDSL.KZD,LCJZ.NKLCK_MC from
(select substr(NKLCK_FJNM,1,4) as NKLCK_FJNM, count(NKNKJZ_KZD) AS KZD from NKLCK LEFT join NKNKJZ ON NKNKJZ_LCNM =NKLCK_NM WHERE NKLCK_ZZNM=ZZNM group by substr(NKLCK_FJNM,1,4)) KZDSL
INNER JOIN
(select NKLCK_FJNM, NKLCK_MC from NKLCK WHERE NKLCK_JC=1 and NKLCK_ZZNM=ZZNM) LCJZ ON LCJZ.NKLCK_FJNM=KZDSL.NKLCK_FJNM) ccc;
open NKFXZcur; --打开游标
fetch NKFXZcur INTO FXZ,FJNM; --提取游标数据
while NKFXZcur%FOUND loop --循环
if FXZ>=3.5 and FXZ<=5 then --高风险
update NK_SLTJ set GFX=GFX+1 where LCK_FJNM=substr(FJNM,0,4);
elsif FXZ>2 and FXZ<3.5 then --中风险
update NK_SLTJ set ZFX=ZFX+1 where LCK_FJNM=substr(FJNM,0,4);
else
if FXZ>=0 and FXZ<=2 then --低风险
update NK_SLTJ set DFX=DFX+1 where LCK_FJNM=substr(FJNM,0,4);
end if;
end if;
fetch NKFXZcur INTO FXZ,FJNM ;
end loop;--结束循环
close NKFXZcur; --关闭游标
end;
三、与SqlServer 临时表的区别
oracle:
SqlServer则是本地和全局临时表。
标签:style io os ar 使用 strong sp 数据 on
原文地址:http://www.cnblogs.com/SunBlog/p/4032023.html