請問如何從一個資料庫的資料表匯入另一個資料庫的資料表? |
缺席
|
funging
一般會員 發表:13 回覆:41 積分:10 註冊:2002-10-28 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
Have you tried DataPump? I think you're trying to work on 2 different connections, which do not have a general solution (except reading and inserting record by record in the code). There should be quite a number of discussion on this topics before.
|
sfy257000
一般會員 發表:21 回覆:13 積分:6 註冊:2002-07-11 發送簡訊給我 |
|
ddy
站務副站長 發表:262 回覆:2105 積分:1169 註冊:2002-07-13 發送簡訊給我 |
這是SQL Server 的做法
drop table ntis.dbo.college
select * into ntis.dbo.college from college 或是用DTS --【KTop SNG新聞現場】--記者:ddy----------------------------------------- 請各位市民做好資源回收與垃圾分類,讓不良標題與不當發言在KTop 市消失 ------------------------------------------------------------------------- 發表人 - ddy 於 2002/12/10 13:00:55
|
funging
一般會員 發表:13 回覆:41 積分:10 註冊:2002-10-28 發送簡訊給我 |
嗯!謝謝你們的回答!不過,我datadump沒用過耶!不曉得怎麼使用?
目前是打算利用sql語法來做匯入,不過出現錯誤很難解決!
有發生錯誤:
同一筆資料一直在insert,而因為資料表有primary key的問題,所以會出現重覆的值無法輸入,不曉得為何會這樣??
以下是我的部分程式:
sourceTable是公用變數,儲來源資料表的名稱;
NTISTable是公用變數,儲目的資料表的名稱; with qr_Source do
begin
close;
sql.clear;
sql.add('SELECT COL_ID,COL_CNAME,COL_ENAME FROM ' SourceTable);
Prepare;
Open;
first;
end; with qr_NTIS do
begin
close;
sql.clear;
sql.Add('TRUNCATE TABLE COLLEGE');
Prepare;
ExecSQL;
ShowMessage('學院資料刪除成功');
end;
while not qr_Source.Eof do
begin
//with qr_NTIS do
//begin
qr_NTIS.close;
qr_NTIS.sql.clear;
qr_NTIS.sql.add('INSERT INTO COLLEGE(COL_ID,COL_CNAME,COL_ENAME) VALUES(:Vcol_id,:Vcol_cname,:Vcol_ename)');
qr_NTIS.Prepare;
qr_NTIS.ParamByName('Vcol_id').AsString:= qr_Source.fieldbyname('COL_ID').AsString;
qr_NTIS.ParamByName('Vcol_cname').AsString:= qr_Source.fieldbyname('COL_CNAME').AsString;
qr_NTIS.ParamByName('Vcol_ename').AsString:= qr_Source.fieldbyname('COL_ENAME').AsString;
qr_NTIS.ExecSQL;
Next;
end;
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
引言: 嗯!謝謝你們的回答!不過,我datadump沒用過耶!不曉得怎麼使用? 目前是打算利用sql語法來做匯入,不過出現錯誤很難解決! 有發生錯誤: 同一筆資料一直在insert,而因為資料表有primary key的問題,所以會出現重覆的值無法輸入,不曉得為何會這樣?? 以下是我的部分程式: sourceTable是公用變數,儲來源資料表的名稱; NTISTable是公用變數,儲目的資料表的名稱;Missed qr_Source?with qr_Source do begin close; sql.clear; sql.add('SELECT COL_ID,COL_CNAME,COL_ENAME FROM ' SourceTable); Prepare; Open; first; end; with qr_NTIS do begin close; sql.clear; sql.Add('TRUNCATE TABLE COLLEGE'); Prepare; ExecSQL; ShowMessage('學院資料刪除成功'); end; while not qr_Source.Eof do begin //with qr_NTIS do //begin qr_NTIS.close; qr_NTIS.sql.clear; qr_NTIS.sql.add('INSERT INTO COLLEGE(COL_ID,COL_CNAME,COL_ENAME) VALUES(:Vcol_id,:Vcol_cname,:Vcol_ename)'); qr_NTIS.Prepare; qr_NTIS.ParamByName('Vcol_id').AsString:= qr_Source.fieldbyname('COL_ID').AsString; qr_NTIS.ParamByName('Vcol_cname').AsString:= qr_Source.fieldbyname('COL_CNAME').AsString; qr_NTIS.ParamByName('Vcol_ename').AsString:= qr_Source.fieldbyname('COL_ENAME').AsString; qr_NTIS.ExecSQL; qr_Source.Next; end; |
funging
一般會員 發表:13 回覆:41 積分:10 註冊:2002-10-28 發送簡訊給我 |
william先生,我照你的方法改了!會出現程式一直跑個不停,只能強迫中止,最後也只有insert一筆資料,理應有六筆才對! 還有不懂你說的missed qr_Source? 是指找不到嗎?
應該存在吧? 還有我不曉得下面的程式最後沒有sql.close,是否正確呢??
with qr_NTIS do
begin
close;
sql.clear;
sql.Add('TRUNCATE TABLE COLLEGE');
Prepare;
ExecSQL;
ShowMessage('學院資料刪除成功');
end;
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
There is no need to call Close after ExecSQL, there should not be any infinite loop in the following code< >< >
while not qr_Source.Eof do begin { perform insert here } qr_Source.Next; end;In you previous code I cannot figure out which object is associated with the Next statement (copy/paste error?), so using qr_Source.Next can make it clear. |
ko
資深會員 發表:28 回覆:785 積分:444 註冊:2002-08-14 發送簡訊給我 |
funging你好:
不好意思,修改你的code
with qr_Source do
begin
close;
sql.clear;
sql.add('SELECT COL_ID,COL_CNAME,COL_ENAME FROM '+SourceTable);
Prepare;
Open;
Last;
end; with qr_NTIS do
begin
close;
sql.clear;
sql.Add('Delete *from COLLEGE');
Prepare;
ExecSQL;
ShowMessage('學院資料刪除成功');
end; for i:=0 to qr_Source.RecordCount -1 do
begin
qr_Source.MoveBy(i);
qr_NTIS.close;
qr_NTIS.sql.clear;
qr_NTIS.sql.add('INSERT INTO COLLEGE(COL_ID,COL_CNAME,COL_ENAME) VALUES(:Vcol_id,:Vcol_cname,:Vcol_ename)');
qr_NTIS.Prepare;//不建議用------可以拿掉
qr_NTIS.ParamByName('Vcol_id').AsString:= qr_Source.fieldbyname('COL_ID').AsString;
qr_NTIS.ParamByName('Vcol_cname').AsString:= qr_Source.fieldbyname('COL_CNAME').AsString;
qr_NTIS.ParamByName('Vcol_ename').AsString:= qr_Source.fieldbyname('COL_ENAME').AsString;
qr_NTIS.ExecSQL;
sleep(100);
end;
------
====================== 昏睡~ 不昏睡~ 不由昏睡~ |
funging
一般會員 發表:13 回覆:41 積分:10 註冊:2002-10-28 發送簡訊給我 |
各位好!我之前一在的修改,終於有結果了!不過,我有點忘了到底是改了什麼才成功的!現在將我的原始碼,列在下面!謝謝各位的解答,小妹感激不盡! 程式:
with qr_Source do
begin
close;
sql.clear;
sql.add('SELECT COL_ID,COL_CNAME,COL_ENAME FROM ' SourceTable);
Prepare;
Open;
first;
end;
// qr_Source.close; with qr_NTIS do
begin
close;
sql.clear;
sql.Add('TRUNCATE TABLE COLLEGE');
Prepare;
ExecSQL;
ShowMessage('學院資料刪除成功');
end;
qr_NTIS.close; while not qr_Source.Eof do
begin
with qr_NTIS do
begin
close;
sql.clear;
sql.add('INSERT INTO COLLEGE(COL_ID,COL_CNAME,COL_ENAME) VALUES(:Vcol_id,:Vcol_cname,:Vcol_ename)');
Prepare;
ParamByName('Vcol_id').AsString:= qr_Source.fieldbyname('COL_ID').AsString;
ParamByName('Vcol_cname').AsString:= qr_Source.fieldbyname('COL_CNAME').AsString;
ParamByName('Vcol_ename').AsString:= qr_Source.fieldbyname('COL_ENAME').AsString;
ExecSQL;
end;
qr_Source.Next;
ShowMessage('學院資料匯入成功');
end;
//qr_Source.close;
with qr_NTIS do
begin
close;
sql.clear;
sql.add('SELECT COL_ID,COL_CNAME,COL_ENAME FROM ' NTISTable);
Prepare;
Open;
end; with qr_Source do
begin
close;
sql.clear;
sql.add('SELECT COL_ID,COL_CNAME,COL_ENAME FROM ' SourceTable);
Prepare;
Open;
end;
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
Your previous code is a little bit inefficient since you clear the prepared SQL for every record, here is my suggestion: < class="code">
with qr_NTIS do
begin
close;
sql.clear;
sql.add('INSERT INTO COLLEGE(COL_ID,COL_CNAME,COL_ENAME) VALUES(:Vcol_id,:Vcol_cname,:Vcol_ename)');
Prepare;
end;
while not qr_Source.Eof do
begin
with qr_NTIS do
begin
ParamByName('Vcol_id').AsString:= qr_Source.fieldbyname('COL_ID').AsString;
ParamByName('Vcol_cname').AsString:= qr_Source.fieldbyname('COL_CNAME').AsString;
ParamByName('Vcol_ename').AsString:= qr_Source.fieldbyname('COL_ENAME').AsString;
ExecSQL;
end;
qr_Source.Next;
end;
ShowMessage('學院資料匯入成功');
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |