binho
asked on
Delphi X SQL Server (inserting null value)
Hi,
I have to execute a SQL statement into a SQL Server database using a TQuery component. I already used "INSERT INTO TABLE (FIELD1, FIELD2, ..) VALUES (:FIELD1, :FIELD2, ...)
I'm using
ParamsByName('FIELD1').AsI nteger := Variable1;
ParamsByName('FIELD2').AsI nteger := Variable2;
The problem is: Variable2 can be null. In Oracle, I would use ParamsByName('FIELD1').AsS tring := Variable2, so if Variable2 is null it would by the same as ParamsByName('FIELD1').AsS tring := ''
In SQL Server that won't work, because FIELD2 is a smallint field and it doesn't allow me to use AsString. Any idea?
Thanks
I have to execute a SQL statement into a SQL Server database using a TQuery component. I already used "INSERT INTO TABLE (FIELD1, FIELD2, ..) VALUES (:FIELD1, :FIELD2, ...)
I'm using
ParamsByName('FIELD1').AsI
ParamsByName('FIELD2').AsI
The problem is: Variable2 can be null. In Oracle, I would use ParamsByName('FIELD1').AsS
In SQL Server that won't work, because FIELD2 is a smallint field and it doesn't allow me to use AsString. Any idea?
Thanks
ASKER
Kretzschmar, I tried that, but I get the error
Query1: Field 'FIELD2' is of an unknown type.
It seems Delphi doesn't recognize the parameter (as if I didn't set a value to the parameter). Note: I'm not using Query1.FieldByName('FIELD2 ')..., but ParamByName('FIELD2')
Tks
Query1: Field 'FIELD2' is of an unknown type.
It seems Delphi doesn't recognize the parameter (as if I didn't set a value to the parameter). Note: I'm not using Query1.FieldByName('FIELD2
Tks
A quick solution would be to add an if statement.
ParamsByName('FIELD1').AsI nteger := Variable1;
if Variable1 = NULL then
ParamsByName('FIELD2').AsI nteger := Variable1
else
ParamsByName('FIELD2').AsI nteger := Variable2;
I'm assuming that you're working with variants all the way. It's allowed to put null values in a field, unless NOT NULL has been specified on the field. Then you'd have to insert any numeric value instead.
Unless I totally misunderstood you, that is. :-)
ParamsByName('FIELD1').AsI
if Variable1 = NULL then
ParamsByName('FIELD2').AsI
else
ParamsByName('FIELD2').AsI
I'm assuming that you're working with variants all the way. It's allowed to put null values in a field, unless NOT NULL has been specified on the field. Then you'd have to insert any numeric value instead.
Unless I totally misunderstood you, that is. :-)
ASKER
Kalroth
If I use the way you suggested, I will get an "Invalid variant type conversion" error
Fabio
If I use the way you suggested, I will get an "Invalid variant type conversion" error
Fabio
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try also ftInteger,
if the datatype above not works
if the datatype above not works
ASKER
Kalroth
If I use the way you suggested, I will get an "Invalid variant type conversion" error
Fabio
If I use the way you suggested, I will get an "Invalid variant type conversion" error
Fabio
ASKER
kretzschmar,
It worked out fine. Wouldn't it be easier if I just use a TQuery with Insert method and just write the FieldByName('FIELDXX').AsI nteger := ... code to those parameters which are not null??
So, if I don't write the code to the "null situation", the field will be null by default...
It worked out fine. Wouldn't it be easier if I just use a TQuery with Insert method and just write the FieldByName('FIELDXX').AsI
So, if I don't write the code to the "null situation", the field will be null by default...
ParamsByName('FIELD2').Cle
works also for oracle