Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameters in SQL - Delphi 7

I am using Delphi 7 and Access 2007.

I want to know can anyone show me how to use Parameters with SQL statements and ADO.

What is the necessary coding and so forth. Sorry I am new to Delphi .

like image 965
0x436f72647265 Avatar asked Jun 04 '13 18:06

0x436f72647265


People also ask

What are SQL query parameters?

A parameterized query is one that includes one or more parameters. A parameter is a placeholder for a value in the WHERE clause of SELECT, UPDATE, or DELETE queries.

How do I add a parameter to a SQL query?

Create a select query, and then open the query in Design view. In the Criteria row of the field you want to add a parameter to, type Like "*"&[, the text that you want to use as a prompt, and then ]&"*".

What is SQL in Delphi?

In Delphi: TQuery TQuery has a property called SQL, which is used to store the SQL statement. TQuery encapsulates one or more SQL statements, executes them and provides methods by which we can manipulate the results.


1 Answers

Simply set the query's SQL, and then populate the parameters. Use parameter names that make sense to you, of course; I've just used LastName and FirstName for examples. I've updated to use TADOQuery instead of just TQuery after your edit to the question.

ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('SELECT * FROM MyTable');
ADOQuery1.SQL.Add('WHERE LastName = :LastName AND');
ADOQuery1.SQL.Add('FirstName = :FirstName');

// Populate the parameters and open it
ADOQuery1.Parameters.ParamByName('LastName').Value := 'Jones';
ADOQuery1.Parameters.ParamByName('FirstName').Value := 'James';
ADOQuery1.Open;
// Use query results

ADOQuery1.Close;
// Populate parameters with new values and open again
// Populate the parameters and open it
ADOQuery1.Parameters.ParamByName('LastName').Value := 'Smith';
ADOQuery1.Parameters.ParamByName('FirstName').Value := 'Sam';
ADOQuery1.Open;
// Use new query results

ADOQuery1.Close;
like image 182
Ken White Avatar answered Sep 22 '22 12:09

Ken White