我用ParamByName的形式传参数值到SQL语句里,如果某个参数值为空值,怎么传进去构造SQL?
下面示例是从一个表里select出和另一个表完全相同的数据。
同事告诉我用ParamByName这种形式传空值是不可能的,要逐个字段构造SQL语句。可是我字段很多。再说,真的不能把Null当做空值传进去吗?在线等。 with ADOQuery1 do
begin
Close;
str1 := 'select * from table1 where id=:id and Name=:Name and ...' //字段很多,放在FList_Flds里。
SQL.Text := str1;
for j:=0 to FList_Flds.Count-1 do
if Query1.FieldByName(FList_Flds[j]).IsNull then //判断字段值是否为空
Parameters.ParamByName(FList_Flds[j]).Value := 'Null' //??
else
Parameters.ParamByName(FList_Flds[j]).Value := ADOQuery2.FieldValues[FList_Flds[j]];
Open;
end;
下面示例是从一个表里select出和另一个表完全相同的数据。
同事告诉我用ParamByName这种形式传空值是不可能的,要逐个字段构造SQL语句。可是我字段很多。再说,真的不能把Null当做空值传进去吗?在线等。 with ADOQuery1 do
begin
Close;
str1 := 'select * from table1 where id=:id and Name=:Name and ...' //字段很多,放在FList_Flds里。
SQL.Text := str1;
for j:=0 to FList_Flds.Count-1 do
if Query1.FieldByName(FList_Flds[j]).IsNull then //判断字段值是否为空
Parameters.ParamByName(FList_Flds[j]).Value := 'Null' //??
else
Parameters.ParamByName(FList_Flds[j]).Value := ADOQuery2.FieldValues[FList_Flds[j]];
Open;
end;
Parameters.ParamByName(FList_Flds[j]).Value := NULL
这样编译不会通过
“不正常的定义参数对象,提供了不一致或不完整的信息”
该怎么办?
Unassigned
都试试看,身边没有环境,不能帮你调试^_^
1.你那赋空值的字段是什么类型?
2.那字段设为可以为空了吗?
msn:[email protected]
这句str1 := 'select * from table1 where id=:id and Name=:Name and ...'
如果:Name参数为空,赋null值到Oracle好像是不能识别,在Oracle里null值表达为is null
所以select * from table1 where id=:id and Name=null' 或Name='null'执行会报错。
试试用字符串变量组合SQL语句,遇到Query1.FieldByName(FList_Flds[j]).IsNull 就连接字符串为:'select * from table1 where id=:id and Name '+' is null '+'...'