procedure TfrmBsqdDts.bitbttnBscrClick(Sender: TObject); var wsSQLTxt : widestring; sArmr : string; begin wsSQLTxt := 'SELECT Ca_Area, Ca_Titulo, Ca_Nombre, Ca_Paterno, Ca_Materno, '+ 'Ca_institucion, Ca_Dirg, Ca_Dir, Ca_Sub, Ca_Jud FROM carea'+ ' WHERE (ca_cancelado = 0) '; if (edtArea.Text <> '') OR (edtTitulo.Text <> '') OR (edtNombres.Text <> '') OR (edtAplldPaterno.Text <> '') OR (edtAplldMaterno.Text <> '') then begin if (edtArea.Text <> '') then begin sArmr := '%'+edtArea.Text+'%'; wsSQLTxt := wsSQLTxt + 'AND (ca_area LIKE '+sArmr+')'; end; if (edtTitulo.Text <> '') then wsSQLTxt := wsSQLTxt + 'AND (ca_titulo LIKE '+QuotedStr('%'+edtTitulo.Text+'%')+') '; if (edtNombres.Text <> '') then wsSQLTxt := wsSQLTxt + 'AND (ca_nombre LIKE '+QuotedStr('%'+edtNombres.Text+'%')+') '; if (edtAplldPaterno.Text <> '') then wsSQLTxt := wsSQLTxt + 'AND (ca_paterno LIKE '+QuotedStr('%'+edtAplldPaterno.Text+'%')+') '; if (edtAplldMaterno.Text <> '') then wsSQLTxt := wsSQLTxt + 'AND (ca_materno LIKE '+QuotedStr('%'+edtAplldMaterno.Text+'%')+') '; end; wsSQLTxt := wsSQLTxt +';'; with dtamdlDts.ADOQryBsqd do begin Active := False; SQL.Clear; SQL.Text := wsSQLTxt; Active := True; end; end;
Arrojándome éste valor para la variable wsSQLTxt:
SELECT Ca_Area, Ca_Titulo, Ca_Nombre, Ca_Paterno, Ca_Materno, Ca_institucion, Ca_Dirg, Ca_Dir, Ca_Sub, Ca_Jud FROM carea WHERE (ca_cancelado = 0) AND (ca_area LIKE %APOYO%);
El problema esencial es que me envía éste error desde el SQLServer:
Project AreaTree.exe raised exception class EOleException with message '[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near 'APOYO'.
Y obviamente no ejecuta ni me muestra los datos que se requieren.
En mi confiabilísimo manejador de SQL Server, detecté parte del problema, que consiste en la inexistencia de dos comillas simples junto a los signos de porcentaje, y SQL Server acepta una sintaxis así:
SELECT Ca_Area, Ca_Titulo, Ca_Nombre, Ca_Paterno, Ca_Materno, Ca_institucion, Ca_Dirg, Ca_Dir, Ca_Sub, Ca_Jud FROM carea WHERE (ca_cancelado = 0) AND (ca_area LIKE '%APOYO%');
Mi pregunta va en el sentido, ¿cómo puedo asegurarme que solamente UNA comilla simple será añadida a mi texto? He usado QuotedStr() y juegos de comillas dentro del código, pero me pone DOS comillas simples y obviamente ésto genera un error dentro del motor de base de datos...
¿Algún consejo o solución que puedan aportarme, por favor?