I'm new to sql, so maybe it is a dumb question, but is there any possibility to use With clause with Insert Into? Or are there any common workarounds? I mean something like this:
With helper_table As (
Select * From dummy2
)
Insert Into dummy1 Values (Select t.a From helper_table t Where t.a = 'X' );
Thx!
My example is too dummy, so I add some extended code (thx for the answers so far).
INSERT
INTO dummy values (a,b) //more values
WITH helper_table AS
(
SELECT *
FROM dummy2
)
WITH helper_table2 AS //from more tables
(
SELECT *
FROM dummy3
)
SELECT t.value as a, t2.value as b
FROM helper_table t
join helper_table t2 on t.value = t2.value //some join
WHERE t.value = 'X' and t2.value = 'X' //other stuff
Keep in mind, using helper tables or CTE's is intended for a small amount of data. Having thousands of rows in helper tables may cause performance degradation.
This is because all the helper table content is stored in PGA.
You may use as many 'helper_tables' as you wish.
With this in mind, you may be able to do all of your joins via normal joining of the tables to the master table
You can do something like
For your updated query