ODBC

13 views
Skip to first unread message

gooloo

unread,
Mar 31, 2016, 8:00:16 AM3/31/16
to Israel R User Group
שלום לכולם,

אני מתחבר מתוך r ל - SQL Server ושולף מידע.

השאלה שלי היא האם אני יכול לשלוט על פרמטרים בתוך השאילתה בעזרת r.

דוגמא:


a <- 3
    r <- "SELECT TOP 1000 * FROM database Where flag = a "
    Con <- odbcConnect("xxxx", uid="xxxx", pwd="xxx*")
    run1 <- sqlQuery(Con , r)

האם אני יכול להגביל את ה - where ע"י ערך שחושב/ הוגדר בסקריפט.


תודה
אריאל

amit gal

unread,
Mar 31, 2016, 8:09:29 AM3/31/16
to israel-r-...@googlegroups.com

באמצעות paste או sprintf

--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ariel Pinco

unread,
Mar 31, 2016, 9:04:46 AM3/31/16
to israel-r-...@googlegroups.com
אפשר דוגמא?
--
Thanks
Ariel Pinco


Efrat Vil

unread,
Mar 31, 2016, 2:42:54 PM3/31/16
to Israel R User Group
תנסה

a <- 3
    query_str <-  paste("SELECT TOP 1000 * FROM database Where flag =", a, sep="")
    Con <- odbcConnect("xxxx", uid="xxxx", pwd="xxx*")
   run1 <- sqlQuery(Con , query_str)

ואם הפרמטר שלך הוא מחרוזת
a <- "abc"
query_str <- paste("SELECT TOP 1000 * FROM database Where flag ='", a, "'", sep="")
Con <- odbcConnect("xxxx", uid="xxxx", pwd="xxx*")
run1 <- sqlQuery(Con , query_str)


Ariel Pinco

unread,
Mar 31, 2016, 2:58:45 PM3/31/16
to israel-r-...@googlegroups.com

תודה רבה.

amit gal

unread,
Mar 31, 2016, 3:09:58 PM3/31/16
to israel-r-...@googlegroups.com
או בעזרת sprintf המהיר יותר (לא שזה צריך להיות בעיה):

a <- 3
    query_str <-  sprintf("SELECT TOP 1000 * FROM database Where flag = %s",a)
    Con <- odbcConnect("xxxx", uid="xxxx", pwd="xxx*")
   run1 <- sqlQuery(Con , query_str)

בכל מקרה הדבר הנכון תכנותית לעשות הוא להשתמש בgsub
נניח יש לך כמה שאילתות בכולן יש כמה פרמטרים שצריכים החלפה כל פעם בערך אחר, אז מייצרים וקטור של שאילתות וניתן להחליף בבת אחת את כל הפרמטרים בערכים רצויים. לדוגמא:

query1 = "select * from table_parameter where column_parameter= 5"
query2 = "select * from xxx inner join table_parameter on xxx.column_parameter = table_parameter.column_parameter"

templates = c(query1,query2)
my_current_table = "yyy"
my_current_column = "ccc"

my_queries = templates %>%
gsub(pattern="table_parameter",replacement=my_current_table) %>%
gsub(pattern="column_parameter",replacement=my_current_column)




Yoni Sidi

unread,
Apr 1, 2016, 7:14:26 AM4/1/16
to Israel R User Group
if you want to use dplyr or dplyrr and DBI to connect to ODBC this is a nice option

https://github.com/mdsumner/dplyrodbc/blob/master/readme.md

Ariel Pinco

unread,
Apr 4, 2016, 2:19:13 AM4/4/16
to israel-r-...@googlegroups.com
תודה רבה לכולם.

On Fri, Apr 1, 2016 at 2:14 PM, Yoni Sidi <yon...@gmail.com> wrote:
if you want to use dplyr or dplyrr and DBI to connect to ODBC this is a nice option

https://github.com/mdsumner/dplyrodbc/blob/master/readme.md
--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages