Sql server Oracle到SQL Server的大规模迁移

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文件。(通过手动分隔符选项,因

我正在尝试将大量的行和列从Oracle动态迁移到SQL Server。我正在尝试每天为大约20张桌子实现自动化

我的过程如下:

  • 运行Oracle配置脚本:运行查询,返回Oracle表数据,但数据有重复数据和错误数据

  • Oracle格式脚本:调用配置脚本,然后修复数据以确保准确(删除重复数据和坏数据)。使用“~”分隔符将数据返回到.txt

  • .txt
    .csv
    :(这是我的解决方法/破解)使用Excel,我打开所有的.txt文件,并使用自动格式化将其更改为格式完美的.csv文件。(通过手动分隔符选项,因为当我使用自动分隔符=“~”时,它会出现随机的单字母列)

  • R:使用新生成的.csv,我在SQLServer中基于这些头创建表结构

  • R:使用从运行每个表的格式化脚本中收集的数据,我有~20.txt文件。我将它们加载到R Studio中,然后尝试使用以下脚本插入新数据:

  • 初始化:

    RScript,用于加载一个.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()