首页 > 解决方案 > 如何从查询中获取列列表?

问题描述

PowerBuilder 数据窗口有一些参数,这些参数显然与数据窗口查询中的列相匹配。查询太复杂。在我的脚本中,我在字符串变量中获取查询语法。我还得到了数据窗口中定义的参数列表。现在我想从查询语法中获取列列表,其中列与检索参数进行比较。我不需要那些未将列与检索参数进行比较的列。

例如,查询字符串在其文本中包含“product_price >= :prod_price AND SupplierID is null AND store_id = :storeID”

我想获取除供应商ID 之外的所有列名称,因为供应商ID 未与任何检索参数进行比较。Query 过于复杂,包含许多 case 语句和运算符。

有没有简单的方法从查询字符串中获取列列表而不涉及复杂的字符串解析脚本?

标签: sql-serverpowerbuilder

解决方案


简化问题的一种方法是不使用参数。

代替

select col1, col2, col3 from table where id = :id

尝试

select col1, col2, col3 from table where 1 = 2

在您的代码中,您可以找到并替换1 = 2id = 8675309

例子

long ll_id = 8675309
long ll_start_pos
string ls_needle
string ls_replace
string ls_sql

ls_sql = dw_products.Getsqlselect()
//ls_sql = select col1, col2, col3 from table where 1 = 2

ls_needle = "1 = 2"
ls_replace = "id = " + string( ll_id )

ll_start_pos = Pos(ls_sql, ls_needle)

// Only enter the loop if you find ls_needle.
DO WHILE ll_start_pos > 0
    // Replace ls_needle with ls_replace.
    ls_sql = Replace(ls_sql, ll_start_pos,Len(ls_needle), ls_replace)

    // Find the next occurrence of ls_needle.
    ll_start_pos= Pos(ls_sql, ls_needle, ll_start_pos + Len(ls_replace))
LOOP

//Replace SQL
if dw_products.Setsqlselect(ls_sql) <> 1 then
    Messagebox("Error setting SQL select",sqlca.sqlerrtext)
return 1

推荐阅读