我试图在SQLAlchemy中为Oracle 12CR1数据库生成以下设计的DML语句:
INSERT INTO baz (name, qty) WITH bar AS ( SELECT bar.name, bar.qty FROM bar ) SELECT foo.name, bar.qty FROM foo JOIN bar ON foo.name = bar.name
但是,SQLAlchemy似乎不按顺序生成它,如下所示:
WITH bar AS ( SELECT bar.name, bar.qty FROM bar ) INSERT INTO baz (name, qty) SELECT foo.name, bar.qty FROM foo JOIN bar ON foo.name = bar.name
使用ORA-00923: FROM keyword not found where expected
执行此操作失败。
我的理论是SQLAlchemy假定PostgreSQL语法为WITH ... INSERT INTO ... SELECT ...
,并且不遵守INSERT INTO ... WITH ... SELECT ...
的Oracle语法。
有没有办法让这个与Oracle合作,或者至少是一种修改insert().from_select()
编译方式的方法?
重现:
from sqlalchemy import (Table, Column, String, Integer,
MetaData, select, func)
from sqlalchemy.dialects import oracle
metadata = MetaData()
foo = Table('foo', metadata, Column('name', String))
bar = Table('bar', metadata, Column('name', String), Column('qty', Integer))
baz = Table('baz', metadata, Column('name', String), Column('qty', Integer))
with_bar = select([bar.c.name, bar.c.qty]).cte('bar')
sel = select([foo.c.name, with_bar.c.qty])
sel = sel.select_from(
foo.join(with_bar,
foo.c.name == with_bar.c.name
)
)
ins = baz.insert().from_select([baz.c.name, baz.c.qty], sel)
print ins.compile(dialect=oracle.dialect())