简体   繁体   English

如何使用 ODBC 包从 R 更改 SQL 表的 vartypes?

[英]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).这有效,但是我想将 sepal.width 更改为十进制(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?是否可以在写入 SQL 之前执行此操作,或者我可以修改 SQL 表以从 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.我知道我可以使用 RODBC,但我被迫使用 R 版本 3.6,所以它不是一个选项。

Look for field.types when you read ?dbWriteTable .查找field.types当你读?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 和microsoft/mssql-server-linux镜像进行测试:

$ 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) \n\tNov 30 2018 12:57:58 \n\tCopyright (C) 2017 Microsoft Corporation\n\tDeveloper Edition (64-bit) on Linux (Ubuntu 16.04.5 LTS)"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 ODBC package 从 SQL 服务器读取大表(>100 列(变量)和 100,000 个观察值)到 R - How to read a large table (>100 columns (variables) and 100,000 observations) from SQL Server into R using ODBC package 在 ODBC 链接的 Access 表中,什么决定了字段大小,我该如何更改它? - In an ODBC linked Access table, what determines the Field Size, and how can I change it? 如何基于R中的数据帧从ODBC数据库中删除记录 - How can I delete records from an ODBC database based on a dataframe in R 如何使用ODBC从Matlab访问Microsoft SQL Server? - How do I access Microsoft SQL Server from Matlab using ODBC? 使用 ODBC 时,我可以在 SQL 评论中放置任意文本吗? - Can I put arbitrary text in SQL comments when using ODBC? 如何从本地 sql 表中删除 ID 列或将其更改为 AUTO INCREMENT? - How can I remove the ID column from a local sql table or change it to AUTO INCREMENT? 如何在SQL Server 2012中使用html更改表中特定单元格的颜色? - How can I change the color of a specific cell in a table using html in SQL server 2012? 如何将表从SQL 2000复制到SQL 2008? - How can I replicate a table from SQL 2000 to SQL 2008? 如何仅使用sql查询从Excel中填充SQL Server表? - How can I fill SQL Server table from excel only using sql query? 如何使用插入查询将数据从一个 SQL 表插入另一个表 - How can I insert data from one SQL table to another table using Insert query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM