这让我很疯狂,但无法让它发挥作用。
我想将以下内容粘贴到R。
中的SQL查询中UKWinnersID<-c("'1WKX6'", "'ULTY8'", "'JNZX0'", "'8J4D8'",
"'KZJAJ0'", "'W8BH47'", "'CP8RPW9'", "'52TD5'", "'TLKV4'")
sqlQuery(myConn, paste("SELECT TOP 10000 [AxiomaDate]
,[RiskModelID] ,[AxiomaID],[Factor1],[Factor2],[Factor3]
FROM [PortfolioAnalytics].[Data_Axioma]
Where AxiomaID IN (",(paste(UKWinnersID, collapse = ","),")")))
我现在正在使用粘贴功能,但似乎无法正常工作。有人有个主意吗?最后一行的输入将是这样的(数据存储在a中)以使查询起作用..
AxiomaID IN ('1WKX6', 'ULTY8', 'JNZX0', '8J4D8',
'KZJAJ0', 'W8BH47', 'CP8RPW9', '52TD5', 'TLKV4')
当前输出
sqlQuery等AxiomaID IN(&#39; 1WKX6&#39;,&#39; ULTY8&#39;,&#39; JNZX0&#39;,&#39; 8J4D8&#39;,&#39; KZJAJ0& #39;,&#39; W8BH47&#39;,&#39; CP8RPW9&#39;,&#39; 52TD5&#39;,&#39; TLKV4&#39;)&#39;&#34;
基本上我想删除此粘贴中的最后两个字符,这是一个&#39;和&#34;。
答案 0 :(得分:1)
我无法重新创建您的确切输出。尝试以下方法;它产生了我期望的SQL代码。
UKWinnersID<-c("'1WKX6'", "'ULTY8'", "'JNZX0'", "'8J4D8'",
"'KZJAJ0'", "'W8BH47'", "'CP8RPW9'", "'52TD5'", "'TLKV4'")
sqlQuery(myConn, paste("SELECT TOP 10000 [AxiomaDate]
,[RiskModelID] ,[AxiomaID],[Factor1],[Factor2],[Factor3]
FROM [PortfolioAnalytics].[Data_Axioma]
Where AxiomaID IN (",paste(UKWinnersID, collapse = ","),")"))
答案 1 :(得分:0)
我推荐了类似问题的解决方案。见Pasting Values in SQL Query through R
您可以尝试使用查询值定义字符变量。然后你可以调用函数InsertListInQuery传递查询和向量作为参数,如下所示:
UKWinnersID<-c("'1WKX6'", "'ULTY8'", "'JNZX0'", "'8J4D8'",
"'KZJAJ0'", "'W8BH47'", "'CP8RPW9'", "'52TD5'", "'TLKV4'")
query <- "SELECT TOP 10000 [AxiomaDate]
,[RiskModelID] ,[AxiomaID],[Factor1],[Factor2],[Factor3]
FROM [PortfolioAnalytics].[Data_Axioma]
Where AxiomaID IN ()"
SQLQuery <- InsertListInQuery(query, UKWinnersID)
SQLQuery
[1] "SELECT TOP 10000 [AxiomaDate]\n,[RiskModelID] ,[AxiomaID],[Factor1],[Factor2],[Factor3]\nFROM [PortfolioAnalytics].[Data_Axioma]\nWhere AxiomaID IN ( '1WKX6' , 'ULTY8' , 'JNZX0' , '8J4D8' , 'KZJAJ0' , 'W8BH47' , 'CP8RPW9' , '52TD5' ,'TLKV4')"
我希望它有所帮助。