Skip to content

Instantly share code, notes, and snippets.

@xingfuqiu
Created August 16, 2012 07:38
Show Gist options
  • Save xingfuqiu/3368123 to your computer and use it in GitHub Desktop.
Save xingfuqiu/3368123 to your computer and use it in GitHub Desktop.
Delphi:在已有的表中添加字段
//MMWIN:MEMBERSCOPY
unit _MM_Copy_Buffer_;
interface
type
TCodeTemplate = class(TDataModule)
private
//---------------------------------增加字段-------------------------------------
function AppendField(Fields: TStringList; TableName, FieldName, FieldType, FieldSize: String): Boolean;
end;
implementation
//---------------------------------增加字段-------------------------------------
function TCodeTemplate.AppendField(Fields: TStringList; TableName, FieldName, FieldType, FieldSize: String): Boolean;
var
sSQL: String;
sTmp: String;
begin
Result := False;
sTmp := FieldName;
if UpperCase(FieldName) = 'INDEX' then
FieldName := '[Index]';
if UpperCase(FieldName) = 'GROUP' then
FieldName := '[group]';
if LowerCase(FieldType) = 'char' then
begin
sSQL := 'Alter Table '+TableName+' add '+FieldName+' '+FieldType+' ('+FieldSize+')';
end;
if LowerCase(FieldType) = 'int' then
begin
sSQL := 'Alter Table '+TableName+' add '+FieldName+' '+FieldType+' DEFAULT 0';
end;
if LowerCase(FieldType) = 'datetime' then
begin
sSQL := 'Alter Table '+TableName+' add '+FieldName+' '+FieldType+' '+FieldSize;
end;
if LowerCase(FieldType) = 'image' then
begin
sSQL := 'Alter Table '+TableName+' add '+FieldName+' '+FieldType;
end;
if LowerCase(FieldType) = 'memo' then
begin
sSQL := 'Alter Table '+TableName+' add '+FieldName+' '+FieldType;
end;
if Fields.IndexOf(sTmp) = -1 then
begin
CreateField_qy.Close;
CreateField_qy.SQL.Clear;
CreateField_qy.SQL.Text := sSQL;
CreateField_qy.ExecSQL;
Result := True;
end;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment