Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Delphi SQL参数中插入字段名,而不是字符串_Sql_Delphi_Parameters - Fatal编程技术网

在Delphi SQL参数中插入字段名,而不是字符串

在Delphi SQL参数中插入字段名,而不是字符串,sql,delphi,parameters,Sql,Delphi,Parameters,我在delphi中编写了一个SQL查询,其中包含一个参数 WHERE L1.IdListino = :IdListino 根据用户的一些输入,我希望此参数要么是表单的字段(tParsIdListinoExport是字段的名称),要么是另一个表的列(类似于L1.IdListino=fat.IdListino) 如果tParsIdListinoExport.AsString为“”,则 qSel.ParamByName('IdListino').AsString:=tParsIdListinoExp

我在delphi中编写了一个SQL查询,其中包含一个参数

WHERE L1.IdListino = :IdListino
根据用户的一些输入,我希望此参数要么是表单的字段(tParsIdListinoExport是字段的名称),要么是另一个表的列(类似于L1.IdListino=fat.IdListino)

如果tParsIdListinoExport.AsString为“”,则
qSel.ParamByName('IdListino').AsString:=tParsIdListinoExport.AsString
其他的
qSel.ParamByName('IdListino')。值:='fat.IdListino';
结束;
遗憾的是,看起来我无法将列名作为参数插入,因为它在列名周围添加了“”,从而将其视为纯文本。 是否可以从参数中删除“”? 多谢各位


Fabio

您需要在运行时创建SQL指令,如:

with qSel do
begin
    Close;
    SQL.Clear;
    SQL.Add(addYourSqlHere, without Where clause);
    if Condition1 then
        SQL.Add('where FIELD1 = :PARAM01')
    else
        SQL.Add('where FIELD2 = :PARAM01');
    ParamByName('PARAM01').Value := UserFilter;
end;

您可能能够使用sql实现所需的功能。细节有点依赖于RDBMS,但与

其中(:param1='use_field'和:param2=OtherTable.field)或 (:param1='use_param'和Table.field=:param3)

这假设
Table
OtherTable
连接在一起。
它还假设可以多次提到
Param1
——并非所有数据库都允许这样做。

参数不是这样工作的。如果需要对字段名进行字符串替换,请使用
格式
。通常,在SQL查询中,参数是列中值的占位符,而不是实体名称的占位符,如列、表、视图等。。。这不是参数的工作方式。如果客户端有要使用的值,那么参数是将其与服务器通信的最佳方式。参数值不会烧录到SQL代码中,而是单独传递。如果希望服务器加入并使用另一个表中的值,则必须生成正确的SQL语句(不带参数)。应将UserFilter更改为参数,以避免SQL注入。
with qSel do
begin
    Close;
    SQL.Clear;
    SQL.Add(addYourSqlHere, without Where clause);
    if Condition1 then
        SQL.Add('where FIELD1 = :PARAM01')
    else
        SQL.Add('where FIELD2 = :PARAM01');
    ParamByName('PARAM01').Value := UserFilter;
end;