從Oracle數(shù)據(jù)庫到SQL Server數(shù)據(jù)庫主鍵的遷移
主要控件:
ADOConnOrcale: TADOConnection; //連接Oracle
ADOConnSQLServer: TADOConnection; //連接SQL Server
O1: TADOQuery; //連接Oracle
S1: TADOQuery; //連接SQL Server
S2: TADOQuery; //連接SQL Server
ProgressBar1: TProgressBar; //進(jìn)度條
Memo1: TMemo; //顯示出錯(cuò)信息
EdtServer: TEdit; //服務(wù)器
EdtDataBase: TEdit; //數(shù)據(jù)庫名稱
EdtUser: TEdit; //用戶名
EdtPass: TEdit; //口令
Button1: TButton; //執(zhí)行按鈕
//常量
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';
在執(zhí)行前先進(jìn)行Oracle和SQL Server數(shù)據(jù)庫的連接。
連接Oracle:
ADOConnOrcale.ConnectionString :=Format(ORAConnStr,[trim(EdtDataBase.Text),
trim(EdtUser.Text),trim(EdtPass.Text)]);
try
ADOConnOrcale.Open;
MsgBox('Oracle數(shù)據(jù)庫連接成功!');
Except
MsgBox('Oracle數(shù)據(jù)庫連接失敗!');
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數(shù)據(jù)庫連接成功!')
except
MsgBox('SQL Server數(shù)據(jù)庫連接失敗!');
end;
主要執(zhí)行代碼,比較亂,沒有整理,不過實(shí)現(xiàn)功能就行了。
procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
FieldN, tableN, fieldM,aa:String;
begin
if Not ADOConnOrcale.Connected then
begin
MsgBox('請先連接Oracle數(shù)據(jù)庫!');
exit;
end;
if not ADOConnSQLServer.Connected then
begin
MsgBox('請先連接SQL Server數(shù)據(jù)庫!');
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表是否存在當(dāng)前的字段信息
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;
//是當(dāng)前表,循環(huán)讀取主鍵信息
if (tableN='') or (tableN= O1.fieldbyname('table_name').AsString) then
begin
FieldN:=FieldN+'['+O1.fieldbyname('COLUMN_NAME').AsString+'],';//表明相同或初試時(shí)
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+' 導(dǎo)入出錯(cuò)!';
exit;
end;
end;
end;
ProgressBar1.StepIt;
Application.ProcessMessages;
O1.Next;
end;
MsgBox('導(dǎo)入完成!');
finally
Screen.Cursor :=crDefault;
ProgressBar1.Visible :=False;
end;
end;
關(guān)鍵字:Oracle、數(shù)據(jù)庫、SQL Server
新文章:
- CentOS7下圖形配置網(wǎng)絡(luò)的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統(tǒng)后丟失windows啟動(dòng)項(xiàng)
- CentOS單網(wǎng)卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗(yàn)證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網(wǎng)打印機(jī)IP講解
- CentOS7使用hostapd實(shí)現(xiàn)無AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網(wǎng)絡(luò)重啟出錯(cuò)
- 解決Centos7雙系統(tǒng)后丟失windows啟動(dòng)項(xiàng)
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統(tǒng)有什么不同呢
- Centos 6.6默認(rèn)iptable規(guī)則詳解