Created
March 14, 2017 02:14
-
-
Save kyoro1/0f7def180728bbf81ecd7d30662e8560 to your computer and use it in GitHub Desktop.
Azure SQL ServerのデータテーブルをRODBCで抽出してみる ref: http://qiita.com/kyoro1/items/926cd575a4cf0ba6cbdf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## library | |
library(RODBC) | |
## initial setting | |
## 上述の通り、適宜設定値は変えて下さい | |
myServer <- "abc_server.database.windows.net" | |
myUser <- "abc_user" | |
myPassword <- "abc_password" | |
myDatabase <- "abc_database" | |
myDriver <- "SQL Server" | |
# Must correspond to an entry in the Drivers tab of "ODBC Data S | |
## ↑の設定をまとめて1つの変数にします | |
connectionString <- paste0( | |
"Driver=", myDriver, | |
";Server=", myServer, | |
";Database=", myDatabase, | |
";Uid=", myUser, | |
";Pwd=", myPassword) | |
## 接続定義 | |
## Azure上のFirewallの許可設定をしておかないとココでエラーになるので注意。 | |
conn <- odbcDriverConnect(connectionString) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 件数確認をするSQLを定義 | |
sqlQuery <- "select count(*) from SalesLT.Customer;" | |
## SQLをAzure SQL Serverに送ってみます | |
sqlQuery(conn, sqlQuery) | |
# 847 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 全部もってくるSQLを定義 | |
sqlQuery <- "select * from SalesLT.Customer;" | |
## 持ってきた内容をdataframeに格納 | |
df <- sqlQuery(conn, sqlQuery) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> dim(df) | |
[1] 847 15 | |
> head(df) | |
CustomerID NameStyle Title FirstName MiddleName LastName Suffix | |
1 1 0 Mr. Orlando N. Gee <NA> | |
2 2 0 Mr. Keith <NA> Harris <NA> | |
3 3 0 Ms. Donna F. Carreras <NA> | |
4 4 0 Ms. Janet M. Gates <NA> | |
5 5 0 Mr. Lucy <NA> Harrington <NA> | |
6 6 0 Ms. Rosmarie J. Carroll <NA> | |
CompanyName SalesPerson | |
1 A Bike Store adventure-works\\pamela0 | |
2 Progressive Sports adventure-works\\david8 | |
3 Advanced Bike Components adventure-works\\jillian0 | |
4 Modular Cycle Systems adventure-works\\jillian0 | |
5 Metropolitan Sports Supply adventure-works\\shu0 | |
6 Aerobic Exercise Company adventure-works\\linda3 | |
EmailAddress Phone | |
1 orlando0@adventure-works.com 245-555-0173 | |
2 keith0@adventure-works.com 170-555-0127 | |
3 donna0@adventure-works.com 279-555-0130 | |
4 janet1@adventure-works.com 710-555-0173 | |
5 lucy0@adventure-works.com 828-555-0186 | |
6 rosmarie0@adventure-works.com 244-555-0112 | |
PasswordHash PasswordSalt | |
1 L/Rlwxzp4w7RWmEgXX+/A7cXaePEPcp+KwQhl2fJL7w= 1KjXYs4= | |
2 YPdtRdvqeAhj6wyxEsFdshBDNXxkCXn+CRgbvJItknw= fs1ZGhY= | |
3 LNoK27abGQo48gGue3EBV/UrlYSToV0/s87dCRV7uJk= YTNH5Rw= | |
4 ElzTpSNbUW1Ut+L5cWlfR7MF6nBZia8WpmGaQPjLOJA= nm7D5e4= | |
5 KJqV15wsX3PG8TS5GSddp6LFFVdd3CoRftZM/tP0+R4= cNFKU4w= | |
6 OKT0scizCdIzymHHOtyJKQiC/fCILSooSZ8dQ2Y34VM= ihWf50M= | |
rowguid ModifiedDate | |
1 3F5AE95E-B87D-4AED-95B4-C3797AFCB74F 2005-08-01 | |
2 E552F657-A9AF-4A7D-A645-C429D6E02491 2006-08-01 | |
3 130774B1-DB21-4EF3-98C8-C104BCD6ED6D 2005-09-01 | |
4 FF862851-1DAA-4044-BE7C-3E85583C054D 2006-07-01 | |
5 83905BDC-6F5E-4F71-B162-C98DA069F38A 2006-09-01 | |
6 1A92DF88-BFA2-467D-BD54-FCB9E647FDD7 2007-09-01 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment