ROracle dbGetQuery/ dbSendQuery 如何处理多个绑定参数

How Multiple Bind Parameters Are Handled By ROracle dbGetQuery/ dbSendQuery

在 ROracle 包中,dbSendQuery 定义为 "dbSendQuery" (conn, statement, data=Null...),数据参数指定为“a data.frame 指定绑定数据”。

我的问题在于函数如何读取数据参数。

例如,我能够运行以下简化函数:

statement = “SELECT * FROM DATA WHERE DATE BETWEEN :START AND :END”

bind = data.frame(START = DT1, END = DT2)

DT1 = ‘01-jan-2020’
DT2 = ‘31-jan-2020’ 

dbSendQuery(con, statement = state, data = bind)

但是,当我尝试 运行 带有多次调用相同绑定参数的 SQL 语句的函数时,我收到一条错误消息。

例如,如果我尝试 运行 上面的代码,使用以下简化语句,代码不起作用:

statement = 
WITH DATA1 AS (SELECT * FROM TABLE1 WHERE DATE BETWEEN :START AND :END),
DATA2 AS (SELECT * FROM TABLE2 WHERE DATE BETWEEN :START AND :END),
DATA3 AS (SELECT * FROM DATA2 LEFT JOIN DATA1 ON DATA2.COLUMN = DATA1.COLUMN)

SELECT * 
FROM DATA3 
UNION ALL 
SELECT * 
FROM DATA3 A 
JOIN 
(SELECT * FROM TABLE2 
WHERE DATE BETWEEN :START AND :END) B
ON A.DATE = B.DATE"

但是,如果我使用以下语句 运行 dbSendQuery 并绑定:

,则该函数确实有效
statement = 
"WITH DATA1 AS (SELECT * FROM TABLE1 WHERE DATE BETWEEN :START1 AND :END1),
DATA2 AS (SELECT * FROM TABLE2 WHERE DATE BETWEEN :START2 AND :END2),
DATA3 AS (SELECT * FROM DATA2 LEFT JOIN DATA1 ON DATA2.COLUMN = DATA1.COLUMN)

SELECT * 
FROM DATA3 
UNION ALL 
SELECT * 
FROM DATA3 A 
JOIN 
(SELECT * FROM TABLE2 
WHERE DATE BETWEEN :START3 AND :END3) B
ON A.DATE = B.DATE"

bind = data.frame(START1, = DT1, START2 = DT1, START3 = DT1, END1 = DT2, END2 = DT2, END3 = DT2).

虽然上述方法有效,但我很好奇为什么我无法使用简单的数据帧 运行 dbSendQuery

bind = data.frame(START = DT1, END = DT2)

当语句重复使用相同的绑定参数时。

我也尝试过 运行ning dbSendQuery 与

bind = data.frame(START = rep(DT1,3), END = rep(DT2,3)) 

没有成功。

在与 ROracle 所有者进行一些讨论之后,对于 SQL 语句中绑定变量的每次使用,您似乎都需要绑定数据框中的一个条目。即使使用命名绑定也是如此:

bvn = as.numeric(1)
attr(bvn, "ora.parameter_name") <- "bv";
df <- data.frame(bvn, bvn)
dbGetQuery(con, "select * from dual where 1 = :bv and 1 = :bv", df)