简体   繁体   English

RODBC报价单中的从条款-SAP HANA

[英]From Clause in RODBC Quotations - SAP HANA

I cannot seem to get the below FROM clause to work using RODBC's sqlQuery . 我似乎无法使用RODBC的sqlQuery使以下FROM子句sqlQuery I have taken the advice of @Lars Br. 我已接受@Lars Br的建议。 in terms of the quoting but it still does not work. 的报价,但它仍然无法正常工作。 I am 我是

I know the placeholder piece works as I have used this in qlikview 我知道占位符的作品就像我在qlikview中使用的一样

Extracting Table from HANA using R 使用R从HANA提取表

So the below code works 所以下面的代码有效

table <- sqlQuery(myconn, 'SELECT *
                      FROM "_SYS_BIC"."mytable.TABLE/ALL_DATA"')

But when I try to add the following (I am passing my date parameters in) 但是当我尝试添加以下内容时(我在传递日期参数)

table <- sqlQuery(myconn, 'SELECT *
                      FROM "_SYS_BIC"."mytable.TABLE/ALL_DATA"')   
                    ('PLACEHOLDER' = ('$$AS_OF_DATE$$',
                     '2017-01-09'),
                    'PLACEHOLDER' = ('$$ABCD_ONE$$',
                     '0'),
                    'PLACEHOLDER' = ('$$ABCD_TWO$$',
                     '0'),
                     'PLACEHOLDER' = ('$$EFGH$$',
                    '12345'),
                    'PLACEHOLDER' = ('$$FLAG$$',
                      '1'))')

Now I know that my single quotes are what is messing it up so I tried the following. 现在我知道我的单引号弄乱了它,所以我尝试了以下内容。

  1. replaced all the single quotes with double quotes - This did not work 用双引号替换所有单引号-这不起作用
  2. encapsulated all the single quotes in double quotes 将所有单引号封装在双引号中
  3. removed all the single quotes completely. 完全删除所有单引号。

Here you fall into the traps of nested syntax and multi-level statement processing: 在这里,您将陷入嵌套语法和多级语句处理的陷阱:

table <- sqlQuery >(< myconn, 'SELECT *
                  FROM "_SYS_BIC"."mytable.TABLE/ALL_DATA"'>)< ---!   
                ('PLACEHOLDER' = ('$$AS_OF_DATE$$',
                 '2017-01-09'),
                'PLACEHOLDER' = ('$$ABCD_ONE$$',
                 '0'),
                'PLACEHOLDER' = ('$$ABCD_TWO$$',
                 '0'),
                 'PLACEHOLDER' = ('$$EFGH$$',
                '12345'),
                'PLACEHOLDER' = ('$$FLAG$$',
                  '1'))')

For R your command ends where I put the < ---! 对于R,您的命令在我放置< ---!地方结束< ---! and all the rest is invalid to R. 所有其余的对R都无效
In this case it's important to recall that the sqlQuery() function expects the whole SQL command string in the second parameter. 在这种情况下,重要的是要记住sqlQuery()函数期望在第二个参数中包含整个 SQL命令字符串。 This includes the WITH PARAMETER syntax of HANA. 这包括HANA的WITH PARAMETER语法。

To avoid such issues - would be to assign the SQL command to a variable first and only use the variable in the function call. 为避免此类问题,可以先将SQL命令分配给变量,然后仅在函数调用中使用该变量。
In order to set the parameters with R variables, you could use text substitution. 为了使用R变量设置参数,可以使用文本替换。

# create the date parameter in the right format YYYY-MM-DD
selDate <- format(Sys.Date() , "%F")

selDate [1] "2017-02-04" selDate [1]“ 2017-02-04”

# create the base SQL command with %D as a placeholder for the selDate
# note how all single quotes inside the sqlCMD need to be escaped by a \
 sqlCMD <- 'SELECT *
 +                   FROM "_SYS_BIC"."mytable.TABLE/ALL_DATA"   
 + (\'PLACEHOLDER\' = (\'$$AS_OF_DATE$$\', \'%D\'),
 + \'PLACEHOLDER\' = (\'$$ABCD_ONE$$\', \'0\'),
 + \'PLACEHOLDER\' = (\'$$ABCD_TWO$$\', \'0\'),
 + \'PLACEHOLDER\' = (\'$$EFGH$$\', \'12345\'),
 + \'PLACEHOLDER\' = (\'$$FLAG$$\', \'1\'))'

sqlCMD [1] "SELECT *\\n+ FROM \\"_SYS_BIC\\".\\"mytable.TABLE/ALL_DATA\\" \\n+ ('PLACEHOLDER' = ('$$AS_OF_DATE$$', '%D'),\\n+ 'PLACEHOLDER' = ('$$ABCD_ONE$$', '0'),\\n+ 'PLACEHOLDER' = ('$$ABCD_TWO$$', '0'),\\n+ 'PLACEHOLDER' = ('$$EFGH$$', '12345'),\\n+ 'PLACEHOLDER' = ('$$FLAG$$', '1'))" sqlCMD [1]“ SELECT * \\ n + FROM \\” _ SYS_BIC \\“。\\” mytable.TABLE / ALL_DATA \\“ \\ n +('PLACEHOLDER'=('$$ AS_OF_DATE $$','%D'),\\ n +' PLACEHOLDER'=(''$$ ABCD_ONE $$','0'),\\ n +'PLACEHOLDER'=('$$ ABCD_TWO $$,'0'),\\ n +'PLACEHOLDER'=('$$ EFGH $$ ','12345'),\\ n +'PLACEHOLDER'=('$$ FLAG $$','1'))“

# now subsitute the %D with the selDate
sqlCMD <- gsub ("%D", selDate, sqlCMD)

sqlCMD [1] "SELECT *\\n FROM \\"_SYS_BIC\\".\\"mytable.TABLE/ALL_DATA\\" \\n('PLACEHOLDER' = ('$$AS_OF_DATE$$', '2017-02-04'),\\n'PLACEHOLDER' = ('$$ABCD_ONE$$', '0'),\\n'PLACEHOLDER' = ('$$ABCD_TWO$$', '0'),\\n'PLACEHOLDER' = ('$$EFGH$$', '12345'),\\n'PLACEHOLDER' = ('$$FLAG$$', '1'))" sqlCMD [1]“ SELECT * \\ n FROM \\” _ SYS_BIC \\“。\\” mytable.TABLE / ALL_DATA \\“ \\ n('PLACEHOLDER'=('$$ AS_OF_DATE $$','2017-02-04'), \\ n'PLACEHOLDER'=('$$ ABCD_ONE $$','0'),\\ n'PLACEHOLDER'=('$$ ABCD_TWO $$','0'),\\ n'PLACEHOLDER'=('$$ EFGH $$','12345'),\\ n'PLACEHOLDER'=('$$ FLAG $$','1'))“

  # finally run the query
  table <- sqlQuery(myconn, sqlCMD)

Of course, all the general recommendations (like not to use SELECT * or to ensure correct filtering and aggregation before fetching the result data set) apply. 当然,所有常规建议都适用(例如不要使用SELECT *或在获取结果数据集之前确保正确的过滤和聚合)。

Alright so @Lars Br. 好吧@Lars Br。 You are answer was the first piece of the puzzle the second piece was escaping the single quotes. 您的答案是难题的第一部分,第二部分是转义单引号。 Below is the SQLcmd variable that work as well as the sqlQuery function in action. 以下是有效的SQLcmd变量以及实际使用的sqlQuery函数。 Thanks again! 再次感谢!

The below link helped me understand the concept. 下面的链接帮助我理解了这个概念。

Error: unexpected symbol/input/string constant/numeric constant/SPECIAL in my code 错误:我的代码中出现意外的符号/输入/字符串常量/数字常量/ SPECIAL

sqlCMD <- 'SELECT *
              FROM "_SYS_BIC"."mytable.TABLE/ALL_DATA"
              (\'PLACEHOLDER\' = (\'AS_OF_DATE$$\',\'2017-01-09\'),
              \'PLACEHOLDER\' = (\'$$ABCD_ONE$$\',\'0\'),
              \'PLACEHOLDER\' = (\'$$ABCD_TWO$$\',\'0\'),
              \'PLACEHOLDER\' = (\'$$EFGH$$\',\'123456\'),
              \'PLACEHOLDER\' = (\'$$FLAG$$\',\'1\'))'

table <- sqlQuery(myconn, sqlCMD)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM