How to use dynamic values while executing SQL scripts in R

混江龙づ霸主 提交于 2019-12-01 11:35:45

sqlInterpolate() is for substituting values only, not other components like table names. You could use other templating frameworks such as brew or whisker.

In ?DBI::SQL, you can read:

By default, any user supplied input to a query should be escaped using either dbQuoteIdentifier() or dbQuoteString() depending on whether it refers to a table or variable name, or is a literal string.

Also, on this page:

You may also need dbQuoteIdentifier() if you are creating tables or relying on user input to choose which column to filter on.

So you can use:

sqlInterpolate(ANSI(), 
               "SELECT count(*) FROM ?my_table", 
               my_table = dbQuoteIdentifier(ANSI(), "table_name"))
# <SQL> SELECT count(*) FROM "table_name"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!