Topic: [Скрипт] Проверка дубликат записи
Проверка существования дубликата записи перед сохранением.
Проект с примером:
http://myvisualdatabase.com/forum/misc. … download=1
Скрипт
function CheckDublicate (Action, sTable, sField, sValue: string; id: integer;): boolean;
var
s: string;
sIdSQL: string;
arrFields: array of string;
arrValues: array of string;
sWhere: string;
i,c: integer;
begin
result := False;
arrFields := SplitString(sField, ';');
arrValues := SplitString(sValue, ';');
if Length(arrFields) <> Length(arrFields) then exit;
sWhere := '';
c := Length(arrFields)-1;
for i := 0 to c do
begin
if arrValues[i] <> 'NULL' then
sWhere := sWhere + arrFields[i]+' LIKE ' + arrValues[i] + ' AND '
else sWhere := sWhere + arrFields[i]+' IS NULL AND '
end;
if sWhere<>'' then SetLength(sWhere, Length(sWhere)-4);
if Action = 'NewRecord' then
begin
s := SQLExecute ('SELECT Count(*) FROM '+sTable+' WHERE '+sWhere);
if StrToInt(s) > 0 then result := True;
end;
if Action = 'ShowRecord' then
begin
if id <> -1 then sIdSQL := ' AND (id <> '+ IntToStr(id) +')';
s := SQLExecute ('SELECT Count(*) FROM '+sTable+' WHERE ('+ sWhere +') ' + sIdSQL);
if StrToInt(s) > 0 then result := True;
end;
end;
procedure frmEmployee_Button2_OnClick (Sender: string; var Cancel: boolean);
var
sFields, sValues: string;
begin
sFields := 'lastname;firstname';
sValues := frmEmployee.edLastName.sqlValue+';'+frmEmployee.edFistName.sqlValue;
if CheckDublicate(frmEmployee.dbAction, 'employees', sFields, sValues, frmEmployee.ButtonSave.dbGeneralTableId) then
begin
ShowMessage('Person already exists.');
Cancel := True;
end;
end;
begin
end.