Recent

Author Topic: TSQLQuery InsertSQL does not accept params.  (Read 6403 times)

jack

  • Full Member
  • ***
  • Posts: 107
TSQLQuery InsertSQL does not accept params.
« on: August 02, 2012, 09:48:01 pm »
It seems that one cannot set ParamByName in the InsertSQL. <<Error Parameter Not Found>>

How then might one make use of the InsertSQL property of TSQLQuery?

Knipfty

  • Full Member
  • ***
  • Posts: 232
Re: TSQLQuery InsertSQL does not accept params.
« Reply #1 on: August 02, 2012, 09:58:38 pm »
Hi Jack,

Can you provide an example of the SQL you are using to do the insert?  Are you inserting 1 record or multiple?

Thanks

Knipfty
64-bit Lazarus 2.2.0 FPC 3.2.2, 64-bit Win 11

jack

  • Full Member
  • ***
  • Posts: 107
Re: TSQLQuery InsertSQL does not accept params.
« Reply #2 on: August 03, 2012, 03:00:40 am »
To answer your question directly:
Here is the code that handles my save button:
====================================================
procedure TForm1.SaveClick(Sender: TObject);
begin
  // Save Button
  // Fill in Insert Alternative
  SQLQuery1.Params.ParamByName('aid').AsInteger:=10;
  SQLQuery1.Params.ParamByName('aname').AsString:='Hogwarts';
  SQLQuery1.Params.ParamByName('asize').AsString:='Medium';
  //
  SQLQuery1.ApplyUpdates;
  SQLTransaction1.CommitRetaining;
  SQLQuery1.Active:=true;
end;
==========================================================
The SQLQuery1.SQL is:
SELECT * FROM  ANIMALS ORDER BY A_NAME
==========================================================
The SQLQuery1.InsertSQL is:
INSERT INTO ANIMALS
(A_ID, A_NAME, A_SIZE)
VALUES
(:aid, :aname, :asize);
==========================================================
The Failure occurs when the
SQLQuery1.Params.ParamByName('aid').AsInteger:=10;
is executed.
« Last Edit: August 03, 2012, 03:02:18 am by jack »

Lacak2

  • Guest
Re: TSQLQuery InsertSQL does not accept params.
« Reply #3 on: August 03, 2012, 08:02:20 am »
You can not set param values in InsertSQL using ParamByName ...
(only in SQL)
Params in InsertSQL are handled automaticaly by sqlDB in ApplyUpdates and are set using values in record which you are applied.

So in you example you should:
with SQLQuery1 do begin
  Edit;
  FieldByName('aid').AsInteger := 10;
  ...
  Post;
  ApplyUpdates;
end;
...

IIRC there is also some topic about this in wiki

jack

  • Full Member
  • ***
  • Posts: 107
Re: TSQLQuery InsertSQL does not accept params.
« Reply #4 on: August 06, 2012, 03:52:49 am »
Thanks I'll give this a try.

Appreciate the help!

 

TinyPortal © 2005-2018