-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_sql_create_bulk_insert.R
55 lines (47 loc) · 1.96 KB
/
03_sql_create_bulk_insert.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
library(RODBC) # Needed to connect to SQL DB
# SQL DB connection
# Note connection string sensative to spaces before/after '='
conn <- odbcDriverConnect(connection = "Driver={SQL Server Native Client 11.0};
server=localhost;
database=dbHealth;
trusted_connection=yes;")
# Create SQL table
query_create <- "
CREATE TABLE [dbo].[GP_Prescribing](
[HB] [nvarchar](255) NULL,
[Locality] [nvarchar](255) NULL,
[PracticeID] [nvarchar](255) NULL,
[BNFCode] [nvarchar](255) NULL,
[BNFName] [nvarchar](255) NULL,
[Items] [float] NULL,
[NIC] [float] NULL,
[ActCost] [float] NULL,
[Quantity] [float] NULL,
[DDD] [float] NULL,
[ADQ] [float] NULL,
[Period] [float] NULL
) ON [PRIMARY]
"
# Run SQL script
sqlQuery(conn, query_create)
# Create a sequence and incrent by 1 month. Must be Y,M,D as date needs day, not just month and year
date <- seq(from = as.Date("2016/01/01"),
to = Sys.Date(),
by = 'month')
# Change date fotmat to match file naming string
date <- format(as.Date(date), '%Y%m')
# Loop through files and use SQL bulk insert into SQL DB
# Note *'* after FROM and before *"* in SQL statement
for ( i in seq_along(date) )
{
query_insert <- paste0("BULK INSERT GP_Prescribing
FROM '" , getwd(), "/Input/GP Prescribing/Unzipped/GPData", date[i], ".csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
) "
)
sqlQuery(conn, query_insert)
}