I am facing the oracle error ORA-00928: missing SELECT keyword
with a query, generated by sqlalchemy. The issue was already described and answered here .
My query looks like:
WITH table2 (id) AS (
SELECT id
FROM table3
)
UPDATE table SET id=1
WHERE EXISTS (
SELECT *
FROM table
WHERE id IN (SELECT id FROM table2)
)
and gets generated by this:
table2 = session.query(table3.id).cte()
update(table).where(exists().where(table.id.in_(table2))).values(id=1)
Now I am wondering how to tell sqlachemy to put the CTEs inside the WHERE
clause and not above the UPDATE
.
UPDATE table SET id=1
WHERE EXISTS (
WITH table2 (id) AS (
SELECT id
FROM table3
)
SELECT *
FROM table
WHERE id IN (SELECT id FROM table2)
)
CTE is a nice way to extract an inline view from the query and, by doing so, make your code easier to read and maintain.
When the CTE wasn't invented yet, we used inline views. Here are a few examples (based on Scott's schema) to show what I mean.
First, a CTE:
SQL> with tab as
2 (select deptno from dept
3 where deptno > 10
4 )
5 select e.deptno, count(*)
6 from emp e join tab t on t.deptno = e.deptno
7 group by e.deptno;
DEPTNO COUNT(*)
---------- ----------
30 6
20 5
It can be moved into an inline view:
SQL> select e.deptno, count(*)
2 from emp e join (select deptno from dept
3 where deptno > 10
4 ) t on t.deptno = e.deptno
5 group by e.deptno;
DEPTNO COUNT(*)
---------- ----------
30 6
20 5
Or, using the old syntax, where tables (in the FROM
clause) were separated by commas and joins were done within the WHERE
clause (this might look familiar):
SQL> select e.deptno, count(*)
2 from emp e,
3 (select deptno from dept
4 where deptno > 10
5 ) t
6 where t.deptno = e.deptno
7 group by e.deptno;
DEPTNO COUNT(*)
---------- ----------
30 6
20 5
It means that your query might look like this; note the comment which shows position of your CTE:
update table set
id = 1
where exists (select *
from table
where id in (select id
from
(select id from table3) --> this line is your CTE
)
);
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.