<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0035)http://sosuny.iteye.com/blog/551006 -->
<HTML dir=ltr xml:lang="zh-CN"
xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>Oracle存储过程中使用临时表 - 万丈阳光 - ITeye技术网站</TITLE>
<META content="text/html; charset=UTF-8" http-equiv=Content-Type>
<META name=description
content="一、Oracle临时表知识 在Oracle中,临时表分为SESSION、TRANSACTION两种,SESSION级的临时表数据在整个SESSION都存在,直到结束此次SESSION;而 TRANSACTION级的临时表数据在TRANACTION结束后消失,即COMMIT/ROLLBACK或结束SESSION都会清除 TRANACTION临时表数据。 1) 会话级临时 ...">
<META name=keywords content="Oracle, SQL Oracle存储过程中使用临时表"><LINK
rel="shortcut icon" type=image/x-icon href="/images/favicon.ico"><LINK
title=ITeye rel=search type=application/opensearchdescription+xml
href="/open_search.xml"><LINK title=万丈阳光 rel=alternate type=application/rss+xml
href="/rss"><LINK rel=stylesheet type=text/css
href="Oracle存储过程中使用临时表%20-%20万丈阳光%20-%20ITeye技术网站_files/blog.css"
media=screen><LINK rel=stylesheet type=text/css
href="Oracle存储过程中使用临时表%20-%20万丈阳光%20-%20ITeye技术网站_files/blue.css" media=screen>
<SCRIPT type=text/javascript
src="Oracle存储过程中使用临时表%20-%20万丈阳光%20-%20ITeye技术网站_files/application.js"></SCRIPT>
<SCRIPT type=text/javascript>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-535605-1']);
_gaq.push(['_setDomainName', 'iteye.com']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</SCRIPT>
<LINK rel=stylesheet type=text/css
href="Oracle存储过程中使用临时表%20-%20万丈阳光%20-%20ITeye技术网站_files/SyntaxHighlighter.css"
media=screen>
<SCRIPT type=text/javascript
src="Oracle存储过程中使用临时表%20-%20万丈阳光%20-%20ITeye技术网站_files/shCoreCommon.js"></SCRIPT>
<SCRIPT type=text/javascript
src="Oracle存储过程中使用临时表%20-%20万丈阳光%20-%20ITeye技术网站_files/hotkey.js"></SCRIPT>
<SCRIPT type=text/javascript
src="Oracle存储过程中使用临时表%20-%20万丈阳光%20-%20ITeye技术网站_files/code_favorites.js"></SCRIPT>
<SCRIPT type=text/javascript
src="Oracle存储过程中使用临时表%20-%20万丈阳光%20-%20ITeye技术网站_files/weiboshare.js"></SCRIPT>
<META name=GENERATOR content="MSHTML 8.00.7601.17874"></HEAD>
<BODY>
<DIV id=header>
<DIV id=blog_site_nav><A class=homepage href="http://www.iteye.com/">首页</A> <A
href="http://www.iteye.com/news">资讯</A> <A
href="http://www.iteye.com/magazines">精华</A> <A
href="http://www.iteye.com/forums">论坛</A> <A
href="http://www.iteye.com/ask">问答</A> <A
href="http://www.iteye.com/blogs">博客</A> <A
href="http://www.iteye.com/blogs/subjects">专栏</A> <A
href="http://www.iteye.com/groups">群组</A> <A id=msna onclick="return false;"
href="http://sosuny.iteye.com/blog/551006#"><U>更多</U> <SMALL>▼</SMALL></A>
<DIV style="DISPLAY: none" class=quick_menu><A
href="http://www.iteye.com/job">招聘</A> <A
href="http://www.iteye.com/search">搜索</A> </DIV></DIV>
<DIV id=user_nav><A class=welcome title=登录
href="http://sosuny.iteye.com/login">您还未登录 !</A> <A
href="http://sosuny.iteye.com/login">登录</A> <A class=nobg
href="http://sosuny.iteye.com/signup">注册</A> </DIV></DIV>
<DIV id=page>
<DIV id=branding class=clearfix>
<DIV id=blog_name>
<H1><A href="http://sosuny.iteye.com/">万丈阳光</A></H1></DIV>
<DIV id=fd></DIV>
<DIV id=blog_navbar>
<UL>
<LI class=blog_navbar_for><A
href="http://sosuny.iteye.com/"><STRONG>博客</STRONG></A></LI>
<LI><A href="http://sosuny.iteye.com/weibo">微博</A></LI>
<LI><A href="http://sosuny.iteye.com/album">相册</A></LI>
<LI><A href="http://sosuny.iteye.com/link">收藏</A></LI>
<LI><A href="http://sosuny.iteye.com/blog/guest_book">留言</A></LI>
<LI><A href="http://sosuny.iteye.com/blog/profile">关于我</A></LI></UL>
<DIV class=search>
<FORM method=get action=/blog/search><INPUT
style="WIDTH: 110px; MARGIN-LEFT: 10px" id=query class=search_text type=text
name=query> <INPUT class=submit_search type=submit> </FORM></DIV>
<DIV id=fd></DIV></DIV></DIV>
<DIV id=content class=clearfix>
<DIV id=main>
<DIV class=blog_main>
<DIV class=blog_title>
<H3><A href="http://sosuny.iteye.com/blog/551006">Oracle存储过程中使用临时表</A> <EM
class=actions></EM></H3>
<UL class=blog_categories><STRONG>博客分类:</STRONG>
<LI><A href="http://sosuny.iteye.com/category/68759">数据库</A></LI></UL>
<DIV class=news_tag><A href="http://www.iteye.com/blogs/tag/Oracle">Oracle</A><A
href="http://www.iteye.com/blogs/tag/SQL">SQL</A> </DIV></DIV>
<DIV id=blog_content class=blog_content>
<P><SPAN
style="COLOR: #ff0000; FONT-SIZE: medium"><STRONG>一、Oracle临时表知识 </STRONG>
</SPAN> <SPAN style="FONT-SIZE: small"></SPAN><SPAN
style="FONT-SIZE: small"></SPAN></P>
<P><SPAN
style="FONT-SIZE: small">在Oracle中,临时表分为SESSION、TRANSACTION两种,SESSION级的临时表数据在整个SESSION都存在,直到结束此次SESSION;而
TRANSACTION级的临时表数据在TRANACTION结束后消失,即COMMIT/ROLLBACK或结束SESSION都会清除
TRANACTION临时表数据。 <BR><BR><STRONG>1) 会话级临时表 示例</STRONG> <BR><BR>1创建</SPAN> </P><PRE class=sql name="code">create global temporary table temp_tbl(col_a varchar2(30))
on commit preserve rows</PRE>
<P><SPAN style="FONT-SIZE: small"> 2插入数据</SPAN> </P><PRE class=sql name="code">insert into temp_tbl values('test session table')</PRE>
<P> </P>
<P><SPAN style="FONT-SIZE: small"> 3提交</SPAN> </P><PRE class=sql name="code">commit;
</PRE>
<P><SPAN style="FONT-SIZE: small"> 4查询</SPAN> </P><PRE class=sql name="code">select *from temp_tbl
</PRE>
<P> </P>
<P><SPAN style="FONT-SIZE: small"> 可以看到数据'test session
table'记录还在。<BR>结束SESSION,重新登录,再查询数据select *from
temp_tbl,这时候记录已不存在,因为系统在结束SESSION时自动清除记录 。</SPAN> </P>
<P><SPAN style="FONT-SIZE: small"><BR><STRONG>2) 事务级临时表 示例</STRONG>
<BR><BR>1创建</SPAN> </P><PRE class=sql name="code">create global temporary table temp_tbl(col_a varchar2(30))
on commit delete rows
</PRE>
<P> <SPAN style="FONT-SIZE: small"> </SPAN></P>
<P> </P>
<P><SPAN style="FONT-SIZE: small">2插入数据</SPAN> </P><PRE class=sql name="code">insert into temp_tbl values('test transaction table')
</PRE>
<P> <SPAN style="FONT-SIZE: small"> </SPAN></P>
<P> </P>
<P><SPAN style="FONT-SIZE: small">3提交</SPAN> </P><PRE class=sql name="code">commit ;
</PRE>
<P><SPAN style="FONT-SIZE: small">4查询</SPAN> </P><PRE class=sql name="code">select *from temp_tbl
</PRE>
<P> </P>
<P><SPAN style="FONT-SIZE: small"> 这时候可以看到刚才插入的记录'test transaction
table'已不存在了,因为提交时已经晴空了数据库;同样,如果不提交而直接结束SESSION,重新登录记录也不存在 。</SPAN> </P>
<P> </P>
<P><STRONG><SPAN
style="COLOR: #ff0000; FONT-SIZE: medium">二、在Oracle存储中使用临时表的一个例子</SPAN>
</STRONG></P>
<P>描述:档案册借阅时,需要把册拆�