WEB开发网
开发学院数据库Oracle 实例讲解Oracle到SQL Server主键迁移 阅读

实例讲解Oracle到SQL Server主键迁移

 2008-05-27 12:41:56 来源:WEB开发网   
核心提示: 连接SQL Server:ADOConnSQLServer.ConnectionString:=Format(SQLConnStr,[trim(EdtServer.Text),trim(EdtDataBase.Text),trim(EdtUser.Text),trim(EdtPass.Te

连接SQL Server:

  ADOConnSQLServer.ConnectionString:=Format(SQLConnStr,[trim(EdtServer.Text),
  trim(EdtDataBase.Text),trim(EdtUser.Text),trim(EdtPass.Text)]);
  try
  ADOConnSQLServer.Open;
  MsgBox('SQLServer数据库连接成功!')
  except
  MsgBox('SQLServer数据库连接失败!');
  end;

主要执行代码,比较乱,没有整理,不过实现功能就行了。

  procedureTForm1.Button1Click(Sender:TObject);
  var
  i:Integer;
  FieldN,tableN,fieldM,aa:String;
  begin
  ifNotADOConnOrcale.Connectedthen
  begin
  MsgBox('请先连接Oracle数据库!');
  exit;
  end;
  ifnotADOConnSQLServer.Connectedthen
  begin
  MsgBox('请先连接SQLServer数据库!');
  exit;
  end;
  Screen.Cursor:=crHourGlass;
  try
  o1.Close;
  O1.SQL.Clear;
  //取oracle表用户budget的所有主键约束信息
  o1.SQL.Text:='selecta.CONSTRAINT_NAME,a.CONSTRAINT_TYPE,a.TABLE_NAME,b.COLUMN_NAME,b.position'+
  'fromUSER_CONSTRAINTSa,USER_CONS_COLUMNSbwherea.CONSTRAINT_NAME=b.CONSTRAINT_NAME'+
  'anda.table_name=b.table_nameandconstraint_type=''P''anda.owner=b.owner'+
  'andlower(a.owner)=''budget''orderbya.table_name,b.position';
  O1.open;
  tableN:='';
  O1.First;
  ProgressBar1.Max:=O1.RecordCount;
  ProgressBar1.Min:=0;
  ProgressBar1.Step:=1;
  ProgressBar1.Visible:=true;
  fori:=0toO1.RecordCount-1do
  begin
  s2.Close;
  S2.SQL.Clear;
  //判断SQLServer表是否存在当前的字段信息
  S2.SQL.Text:='SELECTa.nameAStanme,b.*FROMsysobjectsaINNERJOIN'+
  'syscolumnsbONa.id=b.id'+
  'WHERE(a.xtype=''U'')AND(a.name='''+O1.fieldbyname('table_name').AsString+''''+
  ')andb.name='''+O1.fieldbyname('COLUMN_NAME').AsString+''''+
  'ORDERBYb.id';
  S2.Open;
  //不存在,输出表明和字段名
  ifs2.RecordCount<=0then
  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
  withS1do
  begin
  try
  //取SQLServer表的主键信息
  Close;
  sql.Clear;
  sql.Text:='SELECT*FROMINFORMATION_SCHEMA.KEY_COLUMN_USAGEWHERETABLE_NAME='''+tableN+'''';
  Open;
  first;
  aa:=fieldbyname('constraint_name').AsString;
  //如果该主键在SQL表中已存在,删除该主键信息,重建该表主键
  ifrecordcount>0then
  begin
  sql.Clear;
  SQL.Text:='ALTERTABLE'+tableN+'DROPCONSTRAINT'+aa;//删除主键
  ExecSQL;
  end;
  SQL.Clear;//COLUMN_NAME
  SQL.Text:='ALTERTABLE'+tableN+'WITHNOCHECKADD'+
  'CONSTRAINT[PK_'+tableN+']PRIMARYKEYNONCLUSTERED'+
  '('+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;

上一页  1 2 

Tags:实例 讲解 Oracle

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接