Skip to content
Advertisement

How can I change vartypes of a SQL table from R using ODBC package?

This works, however I want to change the sepal.width to decimal(28,0). Is it possible to do it before writing to SQL, or can I modify the SQL table to change the column type from R?

library(odbc)
library(DBI)

con <- DBI::dbConnect(odbc::odbc(),
              Driver   = "SQL Server",
              Server   = "localhost\SQLEXPRESS",
              Database = "testdb",
             
              Port     = 1433)
dbWriteTable(con, "test", iris)

I know I can use RODBC, but I am forced to use R Version 3.6, so it is not an option.

Advertisement

Answer

Look for field.types when you read ?dbWriteTable.

Without:

# con2 <- dbConnect(...)
library(DBI)
dbWriteTable(con2, "test", iris)
dbGetQuery(con2, "select column_name, data_type, numeric_precision, numeric_precision_radix, numeric_scale from information_schema.columns where table_name='test'")
#    column_name data_type numeric_precision numeric_precision_radix numeric_scale
# 1 Sepal.Length     float                53                       2            NA
# 2  Sepal.Width     float                53                       2            NA
# 3 Petal.Length     float                53                       2            NA
# 4  Petal.Width     float                53                       2            NA
# 5      Species   varchar                NA                      NA            NA
dbExecute(con2, "drop table test")
# [1] 0

With:

dbWriteTable(con2, "test", iris, field.types=c("Sepal.Width"="decimal(28,0)"))
dbGetQuery(con2, "select column_name, data_type, numeric_precision, numeric_precision_radix, numeric_scale from information_schema.columns where table_name='test'")
#    column_name data_type numeric_precision numeric_precision_radix numeric_scale
# 1 Sepal.Length     float                53                       2            NA
# 2  Sepal.Width   decimal                28                      10             0
# 3 Petal.Length     float                53                       2            NA
# 4  Petal.Width     float                53                       2            NA
# 5      Species   varchar                NA                      NA            NA
dbExecute(con2, "drop table test")
# [1] 0

Tested using docker and the microsoft/mssql-server-linux image:

$ docker images | grep -E 'REPO|mssql'
REPOSITORY                     TAG          IMAGE ID       CREATED         SIZE
microsoft/mssql-server-linux   latest       314918ddaedf   2 years ago     1.35GB

$ docker run -d -p "11433:1433" --name localss -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Mysecretpassword1" -e "MSSQL_PID=Developer" --cap-add SYS_PTRACE microsoft/mssql-server-linux
dbGetQuery(con2, "select @@version")[[1]]
# [1] "Microsoft SQL Server 2017 (RTM-CU13) (KB4466404) - 14.0.3048.4 (X64) ntNov 30 2018 12:57:58 ntCopyright (C) 2017 Microsoft CorporationntDeveloper Edition (64-bit) on Linux (Ubuntu 16.04.5 LTS)"
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement