Sql server Oracle到SQL Server的大规模迁移
我正在尝试将大量的行和列从Oracle动态迁移到SQL Server。我正在尝试每天为大约20张桌子实现自动化 我的过程如下:Sql server Oracle到SQL Server的大规模迁移,sql-server,r,oracle,csv,rodbc,Sql Server,R,Oracle,Csv,Rodbc,我正在尝试将大量的行和列从Oracle动态迁移到SQL Server。我正在尝试每天为大约20张桌子实现自动化 我的过程如下: 运行Oracle配置脚本:运行查询,返回Oracle表数据,但数据有重复数据和错误数据 Oracle格式脚本:调用配置脚本,然后修复数据以确保准确(删除重复数据和坏数据)。使用“~”分隔符将数据返回到.txt .txt到.csv:(这是我的解决方法/破解)使用Excel,我打开所有的.txt文件,并使用自动格式化将其更改为格式完美的.csv文件。(通过手动分隔符选项,因
.txt
到.csv
:(这是我的解决方法/破解)使用Excel,我打开所有的.txt文件,并使用自动格式化将其更改为格式完美的.csv文件。(通过手动分隔符选项,因为当我使用自动分隔符=“~”时,它会出现随机的单字母列)library(RODBC)
#set table name
tname <- "tablename"
#connect to MSSQL
conn <- odbcDriverConnect("driver={SQL Server};
Server=serv; Database=dbname;
Uid=username; Pwd=pwd;trusted_connection=yes")
#get df headers from .csv
updatedtables <- read.csv(file = paste("<PATH>", tname, ".csv", sep=""), sep = ",")
#save to MSSQL
save <- sqlSave(conn, updatedtables, tablename = paste("dbo.",tname, sep = ""), append = F, rownames = F, verbose = T, safer = T, fast = F)
#add update record in log file
write(paste(Sys.time(), ": Updated dbo.",tname, sep=""), file = "<PATH>", append = TRUE)
odbcClose(conn)
return(0)
库(RODBC)
#设置表名
名称
library(RODBC)
##~~~~~~~~~~~~~~ Oracle to MSSQL Automation Script ~~~~~~~~~~
#.bat script first runs which updates .txt dumps containing Oracle database info using configuration and format .sql queries for each table.
#function that writes to a table tname using data from tname.txt
writeTable <- function (tname){
#connect to MSSQL
conn <- odbcDriverConnect("driver={SQL Server};
Server=serv; Database=db;
Uid=uid; Pwd=pw;trusted_connection=yes")
#remove all data entries for table
res <- sqlQuery(conn, paste("TRUNCATE TABLE dbo.", tname, sep = ""))
#load updated data from .txt files
#this skips three because of blank lines and skipping the headers
updatedtables <- read.csv(file = paste("<PATH>", tname, ".txt", sep=""),skip=3, sep = "~")
**ERROR**
#save to MSSQL
save <- sqlSave(conn, updatedtables, tablename = paste("dbo.",tname, sep = ""), append = T, rownames = F, verbose = F, safer = T, fast = F)
#add update record in log file
write(paste(Sys.time(), ": Updated dbo.",tname, sep=""), file = "<PATH>", append = TRUE)
#close connection
odbcClose(conn)
return(0)
}
#gets all file names and dynamically inputs the data for every file in the path directory
update <- function(){
#insert line separator for log organization which represents new script run
write("-UPDATE--------------------", file = "<PATH>", append = T)
path = "<PATH>"
#save all .txt file names in path to vector
file.names <- dir(path, pattern =".txt")
#go through path's files
for(i in 1:length(file.names)){
#get proper table name of file.names[i]
temp <- gsub(".txt", "", file.names[i])
#call helper func
writeTable(temp)
}
}
#run
update()