從Oracle資料庫到SQL Server資料庫主鍵的遷移

來源:互聯網
上載者:User
oracle|server|資料|資料庫 由於項目需要要將以前Oracle的資料庫轉化為SQL Server,今天利用SQL Server的DTD進行資料庫的遷移,但匯入以後發現只匯入了表結構和資料,而表的一些主鍵約束都沒導過來,感覺很鬱悶,而手頭又沒有好的遷移工具,如Erwin,所以動手寫了個小工具,基本實現了主鍵的轉移,主要代碼如下:

主要控制項:

    ADOConnOrcale: TADOConnection;  //串連Oracle
    ADOConnSQLServer: TADOConnection; //串連SQL Server
   
    O1: TADOQuery;  //串連Oracle
    S1: TADOQuery; //串連SQL Server
    S2: TADOQuery; //串連SQL Server

    ProgressBar1: TProgressBar;  //進度條
    Memo1: TMemo;  //顯示出錯資訊
    EdtServer: TEdit;  //伺服器
    EdtDataBase: TEdit; //資料庫名稱
    EdtUser: TEdit;  //使用者名稱
    EdtPass: TEdit;  //口令

    Button1: TButton;  //執行按鈕

//常量
const
  ORAConnStr='Provider=MSDAORA.1;Data Source=%S;User ID=%S;Password=%S;Persist Security Info=True';
  SQLConnStr='Provider=SQLOLEDB.1;Data Source=%S;Initial Catalog=%S;User ID=%S;Password=%S;Persist Security Info=False';

在執行前先進行Oracle和SQL Server資料庫的串連。

串連Oracle:

  ADOConnOrcale.ConnectionString :=Format(ORAConnStr,[trim(EdtDataBase.Text),
         trim(EdtUser.Text),trim(EdtPass.Text)]);
  try
    ADOConnOrcale.Open;
    MsgBox('Oracle資料庫連接成功!');
  Except
    MsgBox('Oracle資料庫連接失敗!');
  end;

串連SQL Server:

  ADOConnSQLServer.ConnectionString :=Format(SQLConnStr,[trim(EdtServer.Text),
          trim(EdtDataBase.Text),trim(EdtUser.Text),trim(EdtPass.Text)]);
  try
    ADOConnSQLServer.Open;
    MsgBox('SQL Server資料庫連接成功!')
  except
    MsgBox('SQL Server資料庫連接失敗!');
  end;

主要執行代碼,比較亂,沒有整理,不過實現功能就行了。

procedure TForm1.Button1Click(Sender: TObject);
var
  i:Integer;
  FieldN, tableN, fieldM,aa:String;
begin
  if Not ADOConnOrcale.Connected then
  begin
    MsgBox('請先串連Oracle資料庫!');
    exit;
  end;
  if not ADOConnSQLServer.Connected then
  begin
    MsgBox('請先串連SQL Server資料庫!');
    exit;
  end;
  Screen.Cursor :=crHourGlass;
  try
    o1.Close;
    O1.SQL.Clear;
    //取oracle表使用者budget的所有主鍵約束資訊
    o1.SQL.Text :=' select a.CONSTRAINT_NAME,a.CONSTRAINT_TYPE,a.TABLE_NAME, b.COLUMN_NAME,b.position '+
                  ' from USER_CONSTRAINTS a,USER_CONS_COLUMNS b where a.CONSTRAINT_NAME=b.CONSTRAINT_NAME '+
                  ' and a.table_name=b.table_name and constraint_type=''P'' and a.owner=b.owner '+
                  ' and lower(a.owner)=''budget'' order by a.table_name,b.position ';
    O1.open;
    tableN:='';
    O1.First;
    ProgressBar1.Max:=O1.RecordCount;
    ProgressBar1.Min:=0;
    ProgressBar1.Step:=1;
    ProgressBar1.Visible :=true;
    for i:=0 to O1.RecordCount -1 do
    begin
      s2.Close;
      S2.SQL.Clear;
      //判斷SQL Server表是否存在當前的欄位資訊
      S2.SQL.Text:='SELECT a.name AS tanme, b.* FROM sysobjects a INNER JOIN '+
                   ' syscolumns b ON a.id = b.id '+
                   ' WHERE (a.xtype = ''U'') AND (a.name = '''+O1.fieldbyname('table_name').AsString+''''+
                   ') and b.name= '''+O1.fieldbyname('COLUMN_NAME').AsString+''''+
                   ' ORDER BY b.id';
      S2.Open;
      //不存在,輸出表明和欄位名
      if s2.RecordCount<=0 then
      begin
        Memo1.Text:=Memo1.Text+#13+'表:'''+O1.fieldbyname('table_name').AsString+''''+
                    '   欄位:'''+O1.fieldbyname('COLUMN_NAME').AsString+'''  不存在!';
        O1.Next;
        tableN:='';
        FieldN:='';
        Continue;
      end;
     //是當前表,迴圈讀取主鍵資訊
     if (tableN='') or (tableN= O1.fieldbyname('table_name').AsString) then
     begin
       FieldN:=FieldN+'['+O1.fieldbyname('COLUMN_NAME').AsString+'],';//表明相同或初試時
       tableN:= O1.fieldbyname('table_name').AsString;
     end
     else
     begin
       with S1 do
       begin
         try
           //取SQL Server表的主鍵資訊
           Close;
           sql.Clear;
           sql.Text:='SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME='''+tableN+'''';
           Open;
           first;
           aa:=fieldbyname('constraint_name').AsString;
           //如果該主鍵在SQL表中已存在,刪除該主鍵資訊,重建該表主鍵
           if recordcount>0 then
           begin
             sql.Clear;
             SQL.Text:='ALTER TABLE '+tableN+' DROP CONSTRAINT '+aa; //刪除主鍵
             ExecSQL;
           end;
           SQL.Clear;                              //COLUMN_NAME
           SQL.Text:='ALTER TABLE '+tableN+' WITH NOCHECK ADD '+
                      ' CONSTRAINT [PK_'+tableN+'] PRIMARY KEY  NONCLUSTERED '+
                      ' (  '+ copy(FieldN,1,length(FieldN)-1)+
                      ' )';
           ExecSQL;
           FieldN:='['+O1.fieldbyname('COLUMN_NAME').AsString+'],';
           tableN:= O1.fieldbyname('table_name').AsString;
         Except
           Memo1.Text :=Memo1.Text+'表:  '+tableN+'  欄位:  '+FieldN+'  匯入出錯!';
           exit;
         end;
       end;
     end;
     ProgressBar1.StepIt;
     Application.ProcessMessages;
     O1.Next;
    end;
    MsgBox('匯入完成!');
  finally
    Screen.Cursor :=crDefault;
    ProgressBar1.Visible :=False;
  end;
end;



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.