简体   繁体   English

选择到临时表

[英]Select into temp table

how to perform temp table in oracle from below script? 如何从下面的脚本在oracle中执行临时表?

I tried using select * into #temp but it does not work. 我尝试select * into #temp使用select * into #temp但无法正常工作。 Please assist since i am new in oracle. 请协助,因为我是Oracle新手。

select * into temp from
(SELECT 
CASE WHEN Aaddress = '16' THEN 'A'
ELSE 'OTHERS'
END AS PRODUCT
FROM NAME
WHERE name.INACTIVE_CODE IN ('1', '2'))

Oracle uses create table as : Oracle将create table as

create table temp as
    SELECT (CASE WHEN Aaddress = '16' THEN 'A' ELSE 'OTHERS' END) AS PRODUCT
    FROM NAME
    WHERE name.INACTIVE_CODE IN ('1', '2');

Note if either Aaddress or INACTIVE_CODE are numbers, then the constants used for the comparisons should not have single quotes. 请注意,如果AaddressINACTIVE_CODE是数字,则用于比较的常量不应使用单引号。 Don't compare numbers to strings. 不要将数字与字符串进行比较。

In SQL Server, #temp is a temporary table, but temp is not. 在SQL Server中, #temp temp是一个临时表,而temp不是。 In Oracle, you need to explicitly declare a temporary table: 在Oracle中,您需要显式声明一个临时表:

create global temporary table temp as

Temporary tables in Oracle are created in advance, so that the definition is known before you use them. Oracle中的临时表是预先创建的,因此在使用它们之前先知道定义。

So you could do 所以你可以

create global temporary table temp on commit preserve rows as select ... from ...

but this is not a recommendation to do this every time you run the query. 但这并不建议您每次运行查询时都这样做。 More typical usage is specify the definition once: 更典型的用法是一次指定定义:

create global temporary table temp (
  col1 ...
  col2 );

and then use INSERT to populate the table as required. 然后使用INSERT根据需要填充表。 By default, ie, as per my latter 'create' statement above, the moment you commit, the rows are lost. 默认情况下,即,按照我后面的“ create”语句,提交后,行将丢失。 If you want to retain the rows after a commit, you add the 'on commit preserve rows' as per my first example. 如果要在提交后保留行,请按照我的第一个示例添加“提交时保留行”。 The reason you would have to do this in a create-table-as-select scenario is that otherwise your table would be created, populated with rows, and then immediately emptied as the command completes. 在“按选择创建表”方案中必须执行此操作的原因是,否则将创建表,在表中填充行,然后在命令完成后立即将其清空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM