Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CTEs also with other main statements than SELECT [CORE3169] #3544

Open
firebird-automations opened this issue Oct 8, 2010 · 4 comments

Comments

@firebird-automations
Copy link
Collaborator

Submitted by: @paulvink

Votes: 3

It would be nice to be able to use CTEs with other main statements than just SELECT:

with my_cte as (select ... from ... where ...)
update this_table set ... where <condition involving my_cte>

with your_cte as (select ... from ... where ...),
his_cte as (select ... from ... where ...)
merge into that_table using your_cte
on <condition involving his_cte>
...

Etc.

Also, I would prefer the following syntax in PSQL:

with some_cte as (select ... from ... where ...)
for select ... from some_cte into ...
do

Although this involves just as much typing as the currently supported FOR WITH... SELECT syntax, it is much cleaner: first any CTEs are defined, then the loop is set up.
It also keeps FOR SELECT together. FOR WITH is just plain nonsense, English-wise. At least most of the time. For with any rule, there are exceptions.

@firebird-automations
Copy link
Collaborator Author

Commented by: Boltik Evgeny (bolt)

in my opinion "WITH" is where it should be.
Tested in the Firebird-2.5.6.27017 and FB3.0.32527.
Everything works. You can close CORE3169

MERGE
INTO d009 c
USING (
with xxx as (SELECT * FROM d009 WHERE d009_1 > '10')
select * from xxx) cd
ON (c.d009_1 = cd.d009_1)
WHEN MATCHED THEN UPDATE SET d009_1 = cd.d009_1
ok.

UPDATE d009 SET d009_1 = d009_1
where d009_1 in (
with xxx as (SELECT d009_1 FROM d009 WHERE d009_1 > '10')
select * from xxx)
ok.

delete from d009
where d009_1 in (
with xxx as (SELECT d009_1 FROM d009 WHERE d009_1 = 'x10')
select * from xxx)
ok.

@firebird-automations
Copy link
Collaborator Author

Commented by: @paulvink

No Boltik, you've got it the other way around.
Of course you can nest CTE's as tables within another statement, but I would like to be able to perform update, merge etc. operations within a CTE construct, i.e. with the CTE or CTE's neatly defined in the preamble and the operations following it. See my examples.
Defining CTE's in the preamble gives cleaner code. Nesting them parenthesized within another statement looks messy.

@firebird-automations
Copy link
Collaborator Author

Commented by: @livius2

the first sample syntax is vital.
Update statements should support CTE, this simplify it a lot.

instead writing many "same" subqueries to put value for fields we then can use simple cte to do the same

e.g.

UPDATE TABLE1 T1
SET
T1.FIELD1=(SELECT http://T2.XXX FROM TABLE2 JOIN ... JOIN ... WHERE ...)
,T1.FIELD1=(SELECT http://T2.XXX FROM TABLE2 JOIN ... JOIN ... WHERE ...)
WHERE
....

we can write it simply as

WITH TX AS
(
SELECT http://T2.XXX, T3.YY FROM TABLE2 JOIN T3... JOIN ...
)
UPDATE TABLE1 T1
SET
T1.FIELD1=(SELECT http://TX.XXX FROM TX WHERE ...)
,T1.FIELD1=(SELECT TX.YYY FROM TX WHERE ...)
WHERE
....

@firebird-automations
Copy link
Collaborator Author

Commented by: @asfernandes

IMO this ticket is about invalid things. Just use MERGE ... USING WITH. A CTE for UPDATE would be needed only if Firebird allowed to update the CTE (a simple map from a select), not for read data of a CTE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant