Pass R Vector to Sql query

前端 未结 4 628
小蘑菇
小蘑菇 2020-12-21 01:14

I\'m using RODBC package to access my sql database in R. I haven\'t been able to find any useful information on how to pass a vector from R to sql as a vector.



        
4条回答
  •  太阳男子
    2020-12-21 01:49

    I want to try and figure out how to do this using R Notebook SQL chunk, but have not been able to figure it out. I had lots of trouble with other methods. This works for me.

    library(RODBC)
    myconn<-odbcConnect(dsn = "Data Source Name", 
                    uid = rstudioapi::askForPassword("User ID"), 
                    pwd = rstudioapi::askForPassword("Database password"))
    
    id<-('00003', '00100') # vector of ID's
    
    id<-paste0(personid, collapse = ", ") #create string for entry into query
    
    query<-paste("select *
                  from prod_cust_vw.store_dim
                  where store_num in (", id,")", sep = "") #query -- use "paste". I have not tried with "paste0")
    
    data<-sqlQuery(myconn,query)    #obtain the data by applying the query through the connection.
    

提交回复
热议问题