Delphi: how to pass a list as a parameter to a SQL query?

前端 未结 7 750
你的背包
你的背包 2020-12-17 16:44

I have a list of integers or of strings and need to pass it as a parameter for a Delphi DataSet. How to do it?

Here is an example. MyQuery is something like:

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 16:49

    I use some "IN" replacement. Here is the query I use:

    SELECT * FROM MyTable WHERE CHARINDEX(','+cast(intKey as varchar(10))+',', :listParam) > 0
    

    the code to send parameter:

    MyQuery.ParamByName('listParam').AsString := ',1,2,3,';  
    

    The array item value can partially match some other values. For instance, "1" can be part of "100". To protect against it, I use comma as delimiter

提交回复
热议问题