Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将R会话中的值替换为SQL绑定变量占位符?_Sql_R_Oracle Sqldeveloper_Dbplyr_Bind Variables - Fatal编程技术网

如何将R会话中的值替换为SQL绑定变量占位符?

如何将R会话中的值替换为SQL绑定变量占位符?,sql,r,oracle-sqldeveloper,dbplyr,bind-variables,Sql,R,Oracle Sqldeveloper,Dbplyr,Bind Variables,我想在R脚本中重用原始SQL。但是,SQL提供了一个参数化查询的方法 在dbplyr中使用SQL时,是否有一种快速方法可以直接将R会话中的值替换为绑定变量占位符? 我想它不必是dbplyr,但这就是我使用的 我记得RMarkdown允许带有SQL的块将变量绑定到(全局?)环境中的值。(在中搜索文本“如果需要将R变量的值绑定到SQL查询中”)基于此,似乎有人已经建立了一种简单的变量绑定方法 例如,下面的代码使程序“Oracle SQL Developer”在运行时提示我输入:param1的值 se

我想在R脚本中重用原始SQL。但是,SQL提供了一个参数化查询的方法

在dbplyr中使用SQL时,是否有一种快速方法可以直接将R会话中的值替换为绑定变量占位符?

我想它不必是dbplyr,但这就是我使用的

我记得RMarkdown允许带有SQL的块将变量绑定到(全局?)环境中的值。(在中搜索文本“如果需要将R变量的值绑定到SQL查询中”)基于此,似乎有人已经建立了一种简单的变量绑定方法

例如,下面的代码使程序“Oracle SQL Developer”在运行时提示我输入
:param1
的值

select 
  * 
from 
  ( select 'test' as x, 'another' as y from dual )
where x = :param1 and y = :param2 
我希望在R中使用相同的代码,并使用一些参数运行它。这不起作用,但我认为如果有一个函数可以起作用:

# Assume "con" is a DB connection already established to an Oracle db.
tbl( con, 

  args_for_oracle_sql( 

    "select 
      * 
    from 
      ( select 'test' as x, 'another' as y from dual )
    where x = :param1 and y = :param2 ", 

    # Passing the named parameters
    param1 = "test", 

    param2 = "another" 

  ) 
)


# Here's another interface idea that is perhaps similar to 
# what is shown here for SQL: https://bookdown.org/yihui/rmarkdown/language-engines.html#sql

raw_sql <- "
    select 
      * 
    from 
      ( select 'test' as x, 'another' as y from dual )
    where x = :param1 and y = :param2 "

# Set variables that match parameter names in the current environment.
param1 <- "test"
param2 <- "another"

tbl( con, 

  exc_with_args_for_oracle_sql( 

    # Pass raw SQL with the ":param1" markers       
    sql = raw_sql, 

    # Pass the function an environment that contains the values 
    # of the named parameters in the SQL. In this case, the 
    # current environment where I've set these values above. 
    env_with_args = environment()

  ) 
)
使用dbplyr中的
build\u sql()
函数(用于字符串)

使用dbplyr中的
build\u sql()
函数(用于字符串)

library(RODBC)
library(RODBCext)
library(RODBCDBI)
library(DBI)
library(dplyr)
library(dbplyr)
library(odbc)
library(DBI)
library(dbplyr)
library(odbc)
param1 = readline("What is the value of param1 ?")  # base R
param1 = rstudioapi::askForPassword("What is the value of param1 ?") # RStudio
param2 = readline("What is the value of param2 ?")  # 

con = dbConnect('XXX') # Connection
# write your query (dbplyr)
sql_query = build_sql("select 
      * 
    from 
      ( select 'test' as x, 'another' as y from dual )
    where x = ",param1," and y = ", param2, con = con)

df = dbGetQuery(con,sql_query)