Board index » delphi » Passing Null Values to Query's Parameters

Passing Null Values to Query's Parameters

The following commands:

query1.SQL.Clear;
query1.SQL.Add('INSERT INTO table1 (field1,field2) VALUES ("a",Null)');
query1.ExecSQL;

where field2 is a Foreign Key to some other table, works perfectly.

When I try to do the same thing using Parameters:

query1.SQL.Clear;
query1.SQL.Add('INSERT INTO table1 (field1,field2) VALUES (:field1,
:field2)');
query1.prepare;
query1.ParamByName('field1').AsString:='a';
query1.ParamByName('field2').Value:=Null;
query1.ExecSQL;

doesn't work and displays an error message saying that the field2 is of
uknown type.
I also tried using the command:
query1.ParamByName('field2').Clear;
but the problem was not fixed.
So, how I can pass Null Values to a Query's parameter?

Thanks,
Nicholas Arvanitidis

 

Re:Passing Null Values to Query's Parameters


Call Clear on the ParamByName and also set the Bound to true like

query1.ParamByName('field2').Clear;
query1.ParamByName('field2').Bound := true;

Quote
Nicholas Arvanitidis wrote:

> The following commands:

> query1.SQL.Clear;
> query1.SQL.Add('INSERT INTO table1 (field1,field2) VALUES ("a",Null)');
> query1.ExecSQL;

> where field2 is a Foreign Key to some other table, works perfectly.

> When I try to do the same thing using Parameters:

> query1.SQL.Clear;
> query1.SQL.Add('INSERT INTO table1 (field1,field2) VALUES (:field1,
> :field2)');
> query1.prepare;
> query1.ParamByName('field1').AsString:='a';
> query1.ParamByName('field2').Value:=Null;
> query1.ExecSQL;

> doesn't work and displays an error message saying that the field2 is of
> uknown type.
> I also tried using the command:
> query1.ParamByName('field2').Clear;
> but the problem was not fixed.
> So, how I can pass Null Values to a Query's parameter?

> Thanks,
> Nicholas Arvanitidis

--
Jeff Overcash (TeamB)   On waves of silver I dreamed of gold
(Please do not email    'Till I lost the peace that dreaming gives
 me directly unless     I dreamed of the moment of my own death
 asked.  Thank You)     That no one ever dreams and lives (Marillion)

Other Threads