mirror of
https://github.com/HeidiSQL/HeidiSQL.git
synced 2025-08-06 18:24:26 +08:00
Split copy table query into two: a) CREATE and b) INSERT INTO target (..) SELECT (..) FROM src, to avoid "Duplicate column name" error on old 4.0 servers. Fixes issue #2205.
This commit is contained in:
@ -358,18 +358,19 @@ end;
|
|||||||
|
|
||||||
procedure TCopyTableForm.btnOKClick(Sender: TObject);
|
procedure TCopyTableForm.btnOKClick(Sender: TObject);
|
||||||
var
|
var
|
||||||
CreateCode, Clause, DataCols, TableExistance: String;
|
CreateCode, InsertCode, TargetTable, Clause, DataCols, TableExistance: String;
|
||||||
ParentNode, Node: PVirtualNode;
|
ParentNode, Node: PVirtualNode;
|
||||||
DoData: Boolean;
|
DoData: Boolean;
|
||||||
begin
|
begin
|
||||||
// Compose and run CREATE query
|
// Compose and run CREATE query
|
||||||
|
TargetTable := Mainform.mask(comboDatabase.Text)+'.'+Mainform.mask(editNewTablename.Text);
|
||||||
TableExistance := MainForm.ActiveConnection.GetVar('SHOW TABLES FROM '+MainForm.mask(comboDatabase.Text)+' LIKE '+esc(editNewTablename.Text));
|
TableExistance := MainForm.ActiveConnection.GetVar('SHOW TABLES FROM '+MainForm.mask(comboDatabase.Text)+' LIKE '+esc(editNewTablename.Text));
|
||||||
if TableExistance <> '' then begin
|
if TableExistance <> '' then begin
|
||||||
if MessageDlg('Target table exists. Drop it and overwrite?', mtConfirmation, [mbYes, mbCancel], 0) = mrCancel then begin
|
if MessageDlg('Target table exists. Drop it and overwrite?', mtConfirmation, [mbYes, mbCancel], 0) = mrCancel then begin
|
||||||
ModalResult := mrNone;
|
ModalResult := mrNone;
|
||||||
Exit;
|
Exit;
|
||||||
end;
|
end;
|
||||||
MainForm.ActiveConnection.Query('DROP TABLE '+MainForm.mask(comboDatabase.Text)+'.'+MainForm.mask(editNewTablename.Text));
|
MainForm.ActiveConnection.Query('DROP TABLE '+TargetTable);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
Screen.Cursor := crHourglass;
|
Screen.Cursor := crHourglass;
|
||||||
@ -399,7 +400,7 @@ begin
|
|||||||
ParentNode := TreeElements.GetNextSibling(ParentNode);
|
ParentNode := TreeElements.GetNextSibling(ParentNode);
|
||||||
end;
|
end;
|
||||||
Delete(CreateCode, Length(CreateCode)-2, 3);
|
Delete(CreateCode, Length(CreateCode)-2, 3);
|
||||||
CreateCode := 'CREATE TABLE '+Mainform.mask(comboDatabase.Text)+'.'+Mainform.mask(editNewTablename.Text)+' ('+CRLF+CreateCode+CRLF+')'+CRLF;
|
CreateCode := 'CREATE TABLE '+TargetTable+' ('+CRLF+CreateCode+CRLF+')'+CRLF;
|
||||||
|
|
||||||
// Add collation and engine clauses
|
// Add collation and engine clauses
|
||||||
if FDBObj.Collation <> '' then
|
if FDBObj.Collation <> '' then
|
||||||
@ -416,19 +417,22 @@ begin
|
|||||||
CreateCode := CreateCode + ' AUTO_INCREMENT=' + IntToStr(FDBObj.AutoInc);
|
CreateCode := CreateCode + ' AUTO_INCREMENT=' + IntToStr(FDBObj.AutoInc);
|
||||||
CreateCode := CreateCode + ' COMMENT=' + esc(FDBObj.Comment);
|
CreateCode := CreateCode + ' COMMENT=' + esc(FDBObj.Comment);
|
||||||
|
|
||||||
// Append SELECT .. FROM OrgTable clause
|
// Add INSERT .. SELECT .. FROM OrgTable clause
|
||||||
|
InsertCode := '';
|
||||||
if DoData and (DataCols <> '') then begin
|
if DoData and (DataCols <> '') then begin
|
||||||
DataCols := Trim(DataCols);
|
DataCols := Trim(DataCols);
|
||||||
Delete(DataCols, Length(DataCols), 1);
|
Delete(DataCols, Length(DataCols), 1);
|
||||||
CreateCode := CreateCode + ' SELECT ' + DataCols + ' FROM ' + MainForm.mask(FDBObj.Name);
|
InsertCode := 'INSERT INTO '+TargetTable+' ('+DataCols+') SELECT ' + DataCols + ' FROM ' + MainForm.mask(FDBObj.Name);
|
||||||
if MemoFilter.GetTextLen > 0 then
|
if MemoFilter.GetTextLen > 0 then
|
||||||
CreateCode := CreateCode + ' WHERE ' + MemoFilter.Text;
|
InsertCode := InsertCode + ' WHERE ' + MemoFilter.Text;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// Run query and refresh list
|
// Run query and refresh list
|
||||||
try
|
try
|
||||||
MainForm.ShowStatusMsg('Creating table ...');
|
MainForm.ShowStatusMsg('Creating table ...');
|
||||||
MainForm.ActiveConnection.Query(CreateCode);
|
MainForm.ActiveConnection.Query(CreateCode);
|
||||||
|
if InsertCode <> '' then
|
||||||
|
MainForm.ActiveConnection.Query(InsertCode);
|
||||||
MainForm.actRefresh.Execute;
|
MainForm.actRefresh.Execute;
|
||||||
except
|
except
|
||||||
on E:EDatabaseError do begin
|
on E:EDatabaseError do begin
|
||||||
|
Reference in New Issue
Block a user