Query in Sqlserver

Asked

Viewed 113 times

2

I have the following problem: I would like the person to type the number of Ribbons in an Edit and these values are compared with the values in the database and when they are equal. present them in the report:

SQL.add('Select * from "controle" where "ribbons" = :ribbons');
FrMRelatorio.ADOQuery1.Parameters.ParamByName('ribbons').Value := edit1.Text;

With the following code it always returns the Ribbons for 4 where I might be missing?

3 answers

1

You should remove double quotes ("") table controle and the countryside ribbons and also specify the query parameter you are using.

Would look like this:

FrMRelatorio.ADOQuery1.SQL.add('Select * from controle where ribbons = :ribbons');
FrMRelatorio.ADOQuery1.Parameters.ParamByName('ribbons').Value := edit1.Text;

See more about how to work with parameters in the query.

1


You can also create parameters instead of adding edit1.text inside SQL, thus:

SQL.Add('Select * from controle where ribbons = '+QuotedStr(edit1.text.Text)+'');
  • It worked exactly as I wanted it.

0

When you use parameters, you need to set them in the "Parameters" property more when you need to make some change is need, for example, loading the fields from your table you may have problems is you need to reset everything again in the same way that if you need to set fields mascara you should use the Before Open event. I usually use (Adoquery + Datasetprovider + Clientdataset). I don’t use parameters, I do the queries using pure sql.

You can mount up to a precedent so that you can make all your queries, usually I create an Adoquery only for queries as follows: (sqlGenerico + dspGenerico + cdsGenerico);

    //adicionar no type
    procedure ConsultaDadosGenerico(NomeDoClienteDataSet:TClientDataSet; sqlConsulta :String);
procedure TForm1.Button1Click(Sender: TObject);
begin
  ConsultaDadosGenerico(cdsCliente, 'Select * from controle where ribbons = ' +  QuotedStr(Trim(Edit1.Text)) );
end;

procedure TForm1.ConsultaDadosGenerico(NomeDoClienteDataSet: TClientDataSet; sqlConsulta :String);
begin
  NomeDoClienteDataSet.Close;
  NomeDoClienteDataSet.CommandText := '';
  NomeDoClienteDataSet.CommandText := sqlConsulta;
  NomeDoClienteDataSet.Open;
end;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.