sql-server - 如何从查询中获取列列表?
问题描述
PowerBuilder 数据窗口有一些参数,这些参数显然与数据窗口查询中的列相匹配。查询太复杂。在我的脚本中,我在字符串变量中获取查询语法。我还得到了数据窗口中定义的参数列表。现在我想从查询语法中获取列列表,其中列与检索参数进行比较。我不需要那些未将列与检索参数进行比较的列。
例如,查询字符串在其文本中包含“product_price >= :prod_price AND SupplierID is null AND store_id = :storeID”
我想获取除供应商ID 之外的所有列名称,因为供应商ID 未与任何检索参数进行比较。Query 过于复杂,包含许多 case 语句和运算符。
有没有简单的方法从查询字符串中获取列列表而不涉及复杂的字符串解析脚本?
解决方案
简化问题的一种方法是不使用参数。
代替
select col1, col2, col3 from table where id = :id
尝试
select col1, col2, col3 from table where 1 = 2
在您的代码中,您可以找到并替换1 = 2
为id = 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
推荐阅读
- javascript - 对我的表单的 value 属性的 React 引用没有更新
- kibana - Kibana:如何获取折线图以获取使用百分比?
- c# - 如何在瀑布对话框中调用 QNA 机器人?
- python - 有没有办法将类型强制到类型列表中的值上?
- python - Tkinter 锚留在网格框上
- javascript - 将过渡应用到 jquery .change 元素
- laravel - Laravel Collection - Returning a total with sum() on nested arrays
- c# - 将字典中的特定键删除到另一个字典中 - C#
- java - 快速排序方法给了我一个stackoverflow错误?
- wpf - ContentControl 可见性绑定不起作用