【发布时间】:2021-03-14 22:55:55
【问题描述】:
我正在使用 oracle 19c,我正在尝试使用 union all 方法插入。我试图自动化它,我得到了 ORA-00907。
这是我的代码:
def insert(items):
# items is a list of dicts ->
# [{"test": "Test", "Test": "test", "r": "a"}, {"test": "Test", "Test": "test", "s": "a"}...]
cursor = connection.cursor()
insertions = []
for item in items:
insertions.append(item["test"], item["Test"])
query = """INSERT INTO C##USER.RANDOM
SELECT (:1, :2) FROM dual
""" + "\n".join(["UNION ALL SELECT (:{i}, {i+1}) FROM dual" for i in range(3, len(insertions), 2)])
cursor.execute(query, insertions)
【问题讨论】:
-
您的代码生成的实际 SQL 是什么?
-
不知道,据我所知,我无法通过 cx_Oracle 进行检查
-
如果您的目标(基于您的其他问题)是提高性能,生成数百万条语句,每条语句都有数十万条
union all语句,数据库每次都必须硬解析,不太可能有益。 -
为什么
select中的值要加括号? Oracle does not allow 你这样做,因为(<some_value>, <some_value>)不是一个表达式。