Search code examples
sqlrrodbc

Pasting Values in SQL Query through R


I have the following dataframe which contains the AxiomaID.

x<-c(0123, 234, 2348, 345, 3454)

And trying to run the following SQL Query within R.

 SQL6<-data.frame(sqlQuery(myConn, "SELECT top 10 [AxiomaDate] 
  ,[RiskModelID]
  ,[AxiomaID]
  ,[Factor1]
   FROM [PortfolioAnalytics].[Data_Axioma].[SecurityExposures]
   Where AxiomaID = x"))

How can I paste all the x values which contain the AxiomaID's into the SQL Query?


Solution

  • Try the following query:

    SQL6<-data.frame(sqlQuery(myConn, paste("SELECT top 10 [AxiomaDate] 
      ,[RiskModelID]
      ,[AxiomaID]
      ,[Factor1]
       FROM [PortfolioAnalytics].[Data_Axioma].[SecurityExposures]
       Where AxiomaID IN (", paste(x, collapse = ", "), ")")))
    

    Hope it helps!