I am trying to transform some large data sets, and I have found the most optimized way is to write CTE's in Oracle. Performance wise, I found it to be very close in the findings from this article, (https://www.mssqltips.com/sqlservertip/5118/sql-server-cte-vs-temp-table-vs-table-variable-performance-test/).
However, the problem is that there are also tables which need to be created through this process in a sequential manner. However, I found out that although I am able to create tables using CTEs; I'm not able to use CREATE TABLE statements after CTEs, only before.
I have found that the following method would work. But some reason, I have to run the DROP TABLE & CREATE TABLE statements one at a time. Is there any way to modify the following code so that I can get both tmp and tmp2 tables updated just by running the script only once?
Code Example:
DROP TABLE tmp PURGE;
DROP TABLE tmp2 PURGE;
CREATE TABLE tmp ( tmp_id NUMBER(10));
CREATE TABLE tmp2 ( tmp_id NUMBER(10));
/* Part I - Insert the first table using WITH CTE */
INSERT INTO tmp( tmp_id )
WITH cte AS (
SELECT 1 AS tmp_id FROM dual union all
SELECT 2 AS tmp_id FROM dual
)
SELECT tmp_id
FROM cte;
/* Part II- Use tmp table created above in another CTE*/
INSERT INTO tmp2( tmp_id )
WITH cte AS (
SELECT tmp_id from tmp
)
SELECT tmp_id
FROM cte;
--/*Just for testing*/ select * from tmp
--/*Just for testing*/ select * from tmp2