如何用SQL生成临时表? insert into tmptable select * from tablename 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是要产生一个表,还是要临时存储数据 create table table_name as select * from tablename; 申明转贴:不记得作者(抱歉);11:25:05 SQL> create or replace procedure pro_temp(v_col1 varchar2,v_col2 varchar2) as11:25:05 2 v_num number;11:25:05 3 begin11:25:05 4 select count(*) into v_num from user_tables where table_name='T_TEMP';11:25:05 5 --create temporary table11:25:05 6 if v_num<1 then11:25:05 7 execute immediate 'CREATE GLOBAL TEMPORARY TABLE T_TEMP (11:25:05 8 COL1 VARCHAR2(10),11:25:05 9 COL2 VARCHAR2(10)11:25:05 10 ) ON COMMIT PRESERVE ROWS';11:25:05 11 end if;11:25:05 12 --insert data11:25:05 13 execute immediate 'insert into t_temp values('''||v_col1||''','''||v_col2||''')';11:25:05 14 end pro_temp;11:25:06 15 /过程已创建。已用时间: 00: 00: 00.4411:27:31 SQL> exec pro_temp('11','22');PL/SQL 过程已成功完成。已用时间: 00: 00: 00.4311:27:41 SQL> select * from t_temp;COL1 COL2---------- ----------11 22已用时间: 00: 00: 00.1611:27:47 SQL> 当前session 断开以后,数据自动清空。 我的意思是这样:我想从一个现有的A表中生成一个与A表的结构和数据都一模一样的B表,但如果我断开当前与数据库的连接,B表就自然消失。能否实现? 临时表当用户会话结束时,数据会自动删除, 但也分为2中类型的临时表:create global temporary table 表名 (字段名 类型) on commit preserve rows;当COMMIT时数据保留~create global temporary table 表名 (字段名 类型) on commit delete rows当COMMIT时数据删除~例1:SQL> create global temporary table aa (c1 number) on commit preserve rows;Table created.SQL> insert into aa values (11);1 row created.SQL> commit;Commit complete.SQL> select * from aa; C1---------- 11SQL> exitDisconnected from Oracle9i Enterprise Edition Release 9.2.0.1.0 - ProductionWith the Partitioning, Oracle Label Security, OLAP and Oracle Data Mining optionsJServer Release 9.2.0.1.0 - ProductionC:\>sqlplus scott/tigerSQL*Plus: Release 9.2.0.1.0 - Production on 星期三 12月 15 13:20:16 2004Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.Connected to:Oracle9i Enterprise Edition Release 9.2.0.1.0 - ProductionWith the Partitioning, Oracle Label Security, OLAP and Oracle Data Mining optionsJServer Release 9.2.0.1.0 - ProductionSQL> select * from aa;no rows selectedSQL>例2:SQL> create global temporary table bb (c1 number) on commit delete rows;Table created.SQL> insert into bb values (11);1 row created.SQL> select * from bb; C1---------- 11SQL> commit;Commit complete.SQL> select * from bb;no rows selectedSQL> 在Oracle8i中,可以创建以下两种临时表: 1。会话特有的临时表 CREATE GLOBAL TEMPORARY <TABLE_NAME> (<column specification> ) ON COMMIT PRESERVE ROWS; 2。事务特有的临时表 CREATE GLOBAL TEMPORARY <TABLE_NAME> (<column specification> ) ON COMMIT DELETE ROWS; CREATE GLOBAL TEMPORARY TABLE MyTempTable 所建的临时表虽然是存在的,但是你试一下insert 一条记录然后用别的连接登上去select,记录是空的,明白了吧。下面两句话再贴一下: --ON COMMIT DELETE ROWS 说明临时表是事务指定,每次提交后ORACLE将截断表(删除全部行) --ON COMMIT PRESERVE ROWS 说明临时表是会话指定,当中断会话时ORACLE将截断表。这种临时表不占用表空间,而且不同的SESSION之间互相看不到对方的数据在会话结束后表中的数据自动清空,如果选了DELETE ROWS,则在提交的时候即清空数据,PRESERVE则一直到会话结束 Oracle在这方面做得还是没有Sybase好 create table a select * from b;....drop table a; 楼上的,这个不是跟SYBASE一样吗?你的目的是不要占用空间,Oracle这样的临时表也做到了啊。 创建物化视图出错 关于字符串缓冲区太小的问题,ORA-06502!在线等啊! 一个SQL查询问题 这句shell脚本什么意思? 请各位oracle高手给优化一下查询sql Oracle的执行计划保存在什么地方? oracle错误ORA-12560:TNS:协议适配器错误 Table(表)和View(视图)有何联系和区别? 怎样在vc中用odbc加载日期型数据?急! 如何在oracle中去从1~10条记录? 这样的结果可以用sql来实现吗? 请教一个触发器的写法
create table table_name as select * from tablename;
11:25:05 2 v_num number;
11:25:05 3 begin
11:25:05 4 select count(*) into v_num from user_tables where table_name='T_TEMP';
11:25:05 5 --create temporary table
11:25:05 6 if v_num<1 then
11:25:05 7 execute immediate 'CREATE GLOBAL TEMPORARY TABLE T_TEMP (
11:25:05 8 COL1 VARCHAR2(10),
11:25:05 9 COL2 VARCHAR2(10)
11:25:05 10 ) ON COMMIT PRESERVE ROWS';
11:25:05 11 end if;
11:25:05 12 --insert data
11:25:05 13 execute immediate 'insert into t_temp values('''||v_col1||''','''||v_col2||''')';
11:25:05 14 end pro_temp;
11:25:06 15 /过程已创建。已用时间: 00: 00: 00.4411:27:31 SQL> exec pro_temp('11','22');PL/SQL 过程已成功完成。已用时间: 00: 00: 00.43
11:27:41 SQL> select * from t_temp;COL1 COL2
---------- ----------
11 22已用时间: 00: 00: 00.16
11:27:47 SQL> 当前session 断开以后,数据自动清空。
断开当前与数据库的连接,B表就自然消失。
能否实现?
create global temporary table 表名 (字段名 类型) on commit preserve rows;
当COMMIT时数据保留~
create global temporary table 表名 (字段名 类型) on commit delete rows
当COMMIT时数据删除~例1:
SQL> create global temporary table aa (c1 number) on commit preserve rows;Table created.SQL> insert into aa values (11);1 row created.SQL> commit;Commit complete.SQL> select * from aa; C1
----------
11
SQL> exit
Disconnected from Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
With the Partitioning, Oracle Label Security, OLAP and Oracle Data Mining option
s
JServer Release 9.2.0.1.0 - ProductionC:\>sqlplus scott/tigerSQL*Plus: Release 9.2.0.1.0 - Production on 星期三 12月 15 13:20:16 2004Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
With the Partitioning, Oracle Label Security, OLAP and Oracle Data Mining option
s
JServer Release 9.2.0.1.0 - ProductionSQL> select * from aa;no rows selectedSQL>
例2:
SQL> create global temporary table bb (c1 number) on commit delete rows;Table created.SQL> insert into bb values (11);1 row created.SQL> select * from bb; C1
----------
11SQL> commit;Commit complete.SQL> select * from bb;no rows selectedSQL>
1。会话特有的临时表
CREATE GLOBAL TEMPORARY <TABLE_NAME> (<column specification> )
ON COMMIT PRESERVE ROWS; 2。事务特有的临时表
CREATE GLOBAL TEMPORARY <TABLE_NAME> (<column specification> )
ON COMMIT DELETE ROWS;
CREATE GLOBAL TEMPORARY TABLE MyTempTable
所建的临时表虽然是存在的,但是你试一下insert 一条记录然后用别的连接登上去select,记录是空的,明白了吧。
下面两句话再贴一下:
--ON COMMIT DELETE ROWS 说明临时表是事务指定,每次提交后ORACLE将截断表(删除全部行)
--ON COMMIT PRESERVE ROWS 说明临时表是会话指定,当中断会话时ORACLE将截断表。
这种临时表不占用表空间,而且不同的SESSION之间互相看不到对方的数据
在会话结束后表中的数据自动清空,如果选了DELETE ROWS,则在提交的时候即清空数据,PRESERVE则一直到会话结束
drop table a;