我有以下SQL查询,我通过RODBC
提交给SQL Server
test = "SELECT ticker, date, PX_LAST FROM dbo.bb_si_ts_data_lc
WHERE ticker = 'SPX Index' AND date > '2014-12-31'"
ts_db_monthly = sqlQuery(conn_db,test)
这完全没问题。
现在我尝试参数化查询
date_input_start = paste("2014-12-31")
date_sql_start = paste("AND date>= (\"",date_input_start,"\")")
ticker_input = 'SPX Index'
ticker_sql_input = paste("WHERE ticker IN(\"",ticker_input,"\")")
fields_input = c("ticker","date","PX_LAST")
db = "dbo.bb_si_ts_data_lc"
query_test = paste("SELECT ticker,date,PX_LAST FROM",db,ticker_sql_input,date_sql_start)
ts_db_monthly_test = sqlQuery(conn_db,query_test)
然而,这不起作用。我得到的错误信息是:
[1] "42S22 207 [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name ' SPX Index '."
[2] "42S22 207 [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name ' 2014-12-31 '."
[3] "[RODBC] ERROR: Could not SQLExecDirect 'SELECT ticker,date,PX_LAST FROM dbo.bb_si_ts_data_lc WHERE ticker IN(\" SPX Index \") AND date>= (\" 2014-12-31 \")'"
你能帮我解决这个问题吗?在我看来,问题围绕引号。
答案 0 :(得分:3)
使用单引号,而不是双引号。
但您应该使用问号来指示您的参数并分别传递参数。试试这篇文章来帮助:http://cran.r-project.org/web/packages/RODBCext/vignettes/Parameterized_SQL_queries.html