oracle sql writing merge statement inside a select - sql

Guys is it possible to write a merge statement inside a select like below for correlated subqueries
my requirement is that i have pass to each date from top select statement and for the each date select statement inside using clause should check any data exists for that date and then do insert or update
select date from A a where exists (
merge in to B b
using (
)
..
..
)

Surely you just want to use the SELECT inside the using clause?
merge into B b
using (
select ... where datecol = (select datecol from A a where ...)
)
..
..
)

Related

How to use partition pruning in google big query without hardcoding the filter condition?

I have a big table in GBQ which is partitioned by date. and I want to use partition pruning to optimize my query. the problem is that filter condition is a value that is read from another table and I can't hardcode the value. I wonder if there is any way to use partition pruning in this case and also I can't use declare statement because scheduling engine that I am working with doesn't allow that. here is my code:
WITH CTE AS
(
SELECT tmp.partitionStartDate
FROM tmp_table tmp
)
SELECT *
FROM table1 t
WHERE sbcc.partitionDate = (select partitionStartDate from cte)
Try this:
execute immediate "SELECT * FROM table1 WHERE partitionDate = ?" using (SELECT partitionStartDate FROM tmp_table);
Script below will work:
DECLARE partitionDate DEFAULT (
SELECT tmp.partitionStartDate
FROM tmp_table tmp
);
SELECT *
FROM table1 t
WHERE sbcc.partitionDate = partitionDate;
Or
EXECUTE IMMEDIATE
"""
SELECT *
FROM table1 t
WHERE sbcc.partitionDate = #partitionDate;
""" USING (
SELECT tmp.partitionStartDate
FROM tmp_table tmp
) AS partitionDate;

SQL: Insert into temp table on nested common table expression

I want to insert into temp table inside the nested CTE as on below code. I can select from first x inside second expression but cannot insert into temp table.
WITH x AS
(
SELECT * FROM MyTable
),
y AS
(
SELECT * INTO #temp FROM x
)
SELECT * FROM y
I have to do this using nested CTE as there is other logic to implement. I know i can insert into temp table outside of expression. Is there a way to achieve this? Please help.
SQL Server is quite explicit. A query cannot both return a result set and save the results to a query. You can easily do:
WITH x AS (
SELECT * FROM MyTable
)
SELECT x.*
INTO #temp
FROM x;
And then:
SELECT t.*
FROM #temp t;
The first query saves the result set into a temporary table. The second returns the values from the query.

SQL multiple merge statements with same data source (WITH as)

I would like to have multiple merge statements (one after another) in same query
but i can't use same date source. Example:
WITH DATA as(
SELECT * FROM tables_or_joins
)
MERGE table_name as Target
USING
(SELECT * FROM DATA JOIN another table
)
....
do something more; --and finish this statement here
-- start another merge here
MERGE table_name_2 as Target
USING(
SELECT * FROM DATA and join with another table
)
do something else
But output is Invalid object name 'DATA'. In second merge. Is any other way how to use data in both merge? Hope this is clear enough.
CTE is only good for one statement
Cannot even do two simple selects on DATA
You can have multiple CTE but still only one actual statement
You can use mutiple CTE statements
WITH DATA as( SELECT * FROM tables_or_joins
), DATA2 AS( (SELECT * FROM DATA JOIN another table )
SELECT * from DATA d join DATA2 d2 on d.id=d2.id
OR
Select * from DATA union Select * from DATA2

With structure - using multiple select queries without repeating the "with"

I would like to use a with structure with multiple sql select queries. Ex:
;with temptable as ( ... )
select id from temptable
select name from temptable
However after the first select query is done, SQL Server 2008 doesnt allow me to do it and it pushes me to write the same with structure again above the second query. Ex:
;with temptable as ( ... )
select id from temptable
;with temptable as ( ... )
select name from temptable
I tried using comma after the first select query but didnt work. How can I use multiple select queries under one with structure in SQL Server 2008.
Common table expression works only for one statement.
Specifies a temporary named result set, known as a common table
expression (CTE). This is derived from a simple query and defined
within the execution scope of a single SELECT, INSERT, UPDATE, or
DELETE statement.
select id from temptable;
select name from temptable;
are two statements, so you cannot use it in second query.
The alternative is to use temp table:
SELECT .... INTO #temptable FROM ...; -- your query from CTE
SELECT id FROM #temptable;
SELECT name FROM #temptable;
CTE only exists for the "duration of the query" you can't use the same CTE in two different SELECT statements as each is a separate query.
you need to store the result set of CTE into temp table to and fire multiple select statements on temp table.
;with temptable as ( ... )
select id
into #tempResultSet
from temptable
select name from tempResultSet

Creating a table in Oracle using a query that contains a WITH clause within it

I am basically trying to run a create table statement using a query that has a with clause within it, but I am getting an error. Is there a different way to run this? The query statement is something like this:
CREATE TABLE DATA_TABLE AS
(
WITH X AS
(.....)
SELECT * FROM X
)
I would appreciate any help. Thanks.
Here's what you want.
CREATE TABLE t
AS
WITH some_data AS (
SELECT 1 as some_value
FROM dual
UNION ALL
SELECT 2
FROM dual
)
SELECT *
FROM some_data