【问题标题】:In DB2, i need to select rows from a single column based on the value of a host variable在 DB2 中,我需要根据主机变量的值从单个列中选择行
【发布时间】:2016-12-10 16:12:51
【问题描述】:

在 DB2 中,我需要根据主变量的值从单个列中选择行。

db 列只能包含“D”或“P”。

主变量可以包含值“D”或“P”或“B”(所有行)。

当主变量是“D”时,我只想选择带有“D”的行。 当主变量是“P”时,我只想选择带有“P”的行。 当宿主变量是“B”时,我想选择所有行。

我正在使用嵌入式 SQL 在 RPG 中执行此操作。

提前致谢

【问题讨论】:

  • 从 dbColumn = :hostvar 的表中选择 *。适用于 'D' 和 'P',但如果 hostvar = 'B' 则坚持如何编码。我不喜欢使用动态 SQL。
  • 去试试... select * from table where (dbColumn = :hostvar or dbColumn in ('D', 'P'))。

标签: ibm-midrange db2-400 rpgle


【解决方案1】:

给你。

select * from table                 
where dbColumn in (                           
case when :hostvar  ='B' then  'D'                           
     when :hostvar  ='D' then  'D'   
    when :hostvar  ='P' then  'P'                                    
end ,                                              
case when :hostvar  ='B' then  'P'                           
end
)                                               

【讨论】:

    【解决方案2】:

    这会起作用...谢谢大家

    去试试... select * from table where (dbColumn = :hostvar or dbColumn in ('D', 'P'))

    【讨论】:

    • 不应该工作,因为or dbColumn in ('D', 'P') 应该始终选择所有行。请改用or :hostvar = 'B'
    猜你喜欢
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多