动态执行 SQL 查询结果集

【问题】

I have the following query:

SELECT****DISTINCT ORIGFROM VIEW_AWHERE $P{P_Info_Type}= 1

UNIONSELECTDISTINCT ORIGFROM VIEW_BWHERE $P{P_Info_Type}= 2

UNIONSELECTDISTINCT ORIGFROM VIEW_CWHERE $P{P_Info_Type}= 3

UNIONSELECTDISTINCT ORIGFROM VIEW_DWHERE $P{P_Info_Type}= 4

UNIONSELECTDISTINCT ORIGFROM VIEW_EWHERE $P{P_Info_Type}= 5

UNIONSELECTDISTINCT ORIGFROM VIEW_FWHERE $P{P_Info_Type}= 6

UNIONSELECTDISTINCT ORIGFROM VIEW_GWHERE $P{P_Info_Type}= 7

UNIONSELECTDISTINCT ORIGFROM VIEW_HWHERE $P{P_Info_Type}= 8

How can I make it as a dynamic query?

Update: Each query would be executed according to the value of the parameter P_Info_Type.

Thank you for the help,

【回答】

将编号和表名存在变量里(比如二维表、哈希表、临时表),通过查询编号来取表名,并动态执行 SQL,建议用 SPL 实现:

A
1 =[1,“VIEW_A”,2,“VIEW_B”,3,“VIEW_C”,4,“VIEW_D”]
2 =create(key,value).record(A1)
3 =connect(“demo”)
4 =A3.query("select distint ORIG FROM"+A2.select(key==argkey).value)

A2 运行结果 :

A1:将变量存入序列

A2:创建序表并写入记录

A3:链接数据库

A4:更加参数实现动态查询 argkey 是参数。