PDA

Ver la Versión Completa : funcion


agora18
06-10-2003, 02:21:57
Hola amigos

Necesito realizar una función que me retorne si un registro está en un tabla, debo enviarle la tabla y los campos a buscar como parametros.
Como debo realizar el SQL...

__cadetill
06-10-2003, 07:18:30
debes de crear la sentencia SQL dinámicamente. No obstante, también deberás de pasar por parámetrolos valores a buscar

La función podría ser algo así (no está probada pero sí que compila)


function TForm1.Existe(Tabla : string; Campos, Valores : TStrings) : boolean;
var
sql : string;
i : integer;
Q : TQuery;
begin
sql := 'select * from ' + Tabla;
if Campos.Count <> 0 then
sql := sql + ' where ';
for i := 0 to Campos.Count - 1 do
begin
if i = Campos.Count - 1 then
sql := sql + Campos[i] + ' = ' + Valores[i]
else
sql := sql + Campos[i] + ' = ' + Valores[i] + ' and ';
end;

Q := TQuery.Create(Self);
Q.DatabaseName := 'MyDataBasename';
Q.SQL.Text := sql;
try
try
Q.Open;
if Q.IsEmpty then Result := false
else Result := true;
finally
FreeAndNil(Q);
end;
except
Result := false;
end;
end;


Espero te sirva la idea