If you use INSERT INTO , use parameters (for readability, avoid SQL injection, SQL caching), for example:
adoquery1.sql.text := 'INSERT INTO table_foo (field1, field2) values (:field1, :field2)'; adoquery1.Parameters.ParamByName('field1').Value := value1; adoquery1.Parameters.ParamByName('field2').Value := value2;
I prefer the second way (with a little tweak, which I will explain). Since you are inserting a single record, the setup is to select an empty set of entries ie:
SELECT * FROM table_foo where 1=0
Thus, you do not select all records from the table. Also, you do not need to use QuotedStr when assigning ie values:
adoquery1.FieldByName('field1').AsString := 'value1';
The main reason I use this method is that it is easy to read and maintain. I do not need to worry about pure SQL queries. I do not need to deal with the parameters that were once required to indicate the data type for the parameters (for example, Parameters.ParamByName('field1').DataType := ftInteger ). No need for ParseSQL . I just use DataSet As(Type) , for example.
FieldByName('field1').AsBoolean := True;
I would also prefer to use this method if I need to insert multiple records into one transaction. The disadvantage of the second method is a short trip to the SQL server through SELECT FROM .
Another option would be to create a SQL stored procedure, pass your values ββto the SP, and write all the SQL logic inside the SP.
kobik source share