简体   繁体   中英

Select into temp table

how to perform temp table in oracle from below script?

I tried using select * into #temp but it does not work. Please assist since i am new in 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 :

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. Don't compare numbers to strings.

In SQL Server, #temp is a temporary table, but temp is not. In Oracle, you need to explicitly declare a temporary table:

create global temporary table temp as

Temporary tables in Oracle are created in advance, so that the definition is known before you use them.

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. By default, ie, as per my latter 'create' statement above, the moment you commit, the rows are lost. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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