SQLServer数据库之oracle数据库脚本转为sqlServer、DB2
小标 2018-09-07 来源 : 阅读 1465 评论 0

摘要:本文主要向大家介绍了SQLServer数据库之oracle数据库脚本转为sqlServer、DB2,通过具体的内容向大家展现,希望对大家学习SQLServer数据库有所帮助。

本文主要向大家介绍了SQLServer数据库之oracle数据库脚本转为sqlServer、DB2,通过具体的内容向大家展现,希望对大家学习SQLServer数据库有所帮助。


package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author wyl
 * @Description TODO 
 * @date 2016-5-19
 *
 */

public class OracleToOthers {

    /**
     * @Description: TODO 
     * @param args  
     */

    public static void main(String[] args) {
        /*
         * 原oracle数据库脚本由plSql导出,导出时只选择create tables选项
         * 对于原oracle数据库中的表或者列的注释,如果注释中存在换行符,生成的目标脚本会有错误,请在生成的目标脚本做修改
         */
        //原文件路径
        String oracleFile = "C:\\Users\\admin\\Desktop\\table.sql";
        //目标文件路径
        String sqlServerFile = "C:\\Users\\admin\\Desktop\\table_sqlserver.sql";
        String db2File = "C:\\Users\\admin\\Desktop\\table_db2.sql";

        oracleToSqlserver(oracleFile,sqlServerFile);
        oracleToDB2(oracleFile,db2File);
        

    }
    /**
     * @Description: 由oracle脚本转为sqlserver脚本
     * @param oracleFile    原oracle数据库脚本文件路径
     * @param sqlServerFile    转为sqlserver脚本文件的目标路径
     */
    public static void oracleToSqlserver(String oracleFile,String sqlServerFile) {
        File readFile = new File(oracleFile);
        File writeFile = new File(sqlServerFile);
        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new BufferedReader(new FileReader(readFile));
            writer = new BufferedWriter(new FileWriter(writeFile));
            StringBuffer sb = new StringBuffer();
            String tempString = null;
            String tempStr = null;
            String tempS = null;
            
            String tableName = null;
            String column = null;
            String common = null;
            int endIndex = 0;
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                //System.out.println("line " + line + ": " + tempString);
                
                //转换数据类型
                tempString=tempString.replaceAll("VARCHAR2","VARCHAR");
                tempString=tempString.replaceAll("NUMBER","DECIMAL");
                tempString=tempString.replaceAll("CLOB","TEXT");
                if(tempString.indexOf("TIMESTAMP")!=-1){
                    tempString=tempString.replaceAll("TIMESTAMP","DATETIME");
                    int str = tempString.indexOf("(");
                    int end = tempString.indexOf(")");
                    System.out.println(tempString.substring(str, end+1));
                    tempString=tempString.replaceAll("\\("+tempString.substring(str+1, end)+"\\)", "");
                }
                
                //添加注释
                if(tempString.startsWith("prompt")||tempString.startsWith("set ")||tempString.startsWith("commit;")||tempString.startsWith("spool")){
                    tempString = "--"+tempString;
                }
                
                //更改sql注释脚本
                if(tempString.startsWith("comment on")){
                    tempStr = tempString;
                    continue;
                }
                if(tempString.startsWith("  is ‘")){
                    tempString = tempStr + tempString;
                    
                    if(!tempString.endsWith("‘;")){
                        tempS = tempString;
                        tempStr = "";
                        continue;
                    }
                    
                }
                if(tempString.startsWith("‘;")){
                    tempString = tempS+tempString;
                    tempS = "";
                }
                //表注释
                if(tempString.startsWith("comment on table ")&&tempString.endsWith("‘;")){
                    endIndex = tempString.indexOf("  is ‘");
                    //表名
                    tableName = tempString.substring(17,endIndex);
                    String[] strA = tempString.split("‘");
                    common = strA[1];
                    tempString = "EXECUTE sp_addextendedproperty N‘MS_Description‘, N‘"+common+"‘, N‘user‘, N‘dbo‘, N‘table‘, N‘"+tableName+"‘, NULL, NULL";
                }
                //列注释
                if(tempString.startsWith("comment on column ")&&tempString.endsWith("‘;")){
                    endIndex = tempString.indexOf(".");
                    //表名
                    tableName = tempString.substring(18,endIndex);
                    String[] strA = tempString.split("‘");
                    int endIn = strA[0].indexOf("  is");
                    column = strA[0].substring(endIndex+1, endIn);
                    common = strA[1];
                    tempString = "EXECUTE sp_addextendedproperty N‘MS_Description‘, N‘"+common+"‘, N‘user‘, N‘dbo‘, N‘table‘, N‘"+tableName+"‘, N‘column‘, N‘"+column+"‘";
                }
                // 打印
                System.out.println(tempString);
                sb.append(tempString+"\r\n");
            }
            
            reader.close();
            //写入目标文件
            writer.write(sb.toString());
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }
    /**
     * @Description: 由oracle脚本转为DB2脚本 
     * @param oracleFile    原oracle数据库脚本文件路径
     * @param db2File    转为DB2脚本的目标文件路径
     */
    public static void oracleToDB2(String oracleFile,String db2File) {
        File readFile = new File(oracleFile);
        File writeFile = new File(db2File);
        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new BufferedReader(new FileReader(readFile));
            writer = new BufferedWriter(new FileWriter(writeFile));
            StringBuffer sb = new StringBuffer();
            String tempString = null;
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                //System.out.println("line " + line + ": " + tempString);
                
                //转换数据类型
                tempString=tempString.replaceAll("VARCHAR2","VARCHAR");
                tempString=tempString.replaceAll("NUMBER","DECIMAL");
                tempString=tempString.replaceAll("CLOB","CLOB");
                //添加注释
                if(tempString.startsWith("prompt")||tempString.startsWith("set ")||tempString.startsWith("commit;")||tempString.startsWith("spool")){
                    tempString = "--"+tempString;
                }
                
                System.out.println(tempString);
                sb.append(tempString+"\r\n");
            }
            
            reader.close();
            //写入目标文件
            writer.write(sb.toString());
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }
}

 oracle数据库脚本转为sqlServer、DB2原文地址://www.cnblogs.com/luckydog1/p/5725756.html    

以上就介绍了SQL Server的相关知识,希望对SQL Server有兴趣的朋友有所帮助。了解更多内容,请关注职坐标数据库SQL Server频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程