Oracle SQL动态查询

时间:2014-05-08 07:26:15

标签: sql oracle dynamic

我的数据库中有2个表:

enter image description here

enter image description here

我正在尝试创建一个应该作为变量参数接受的查询" id3 "例如 -

Q(z0)应该产生

enter image description here

提前致谢!

3 个答案:

答案 0 :(得分:0)

Oracle查询可以采用2种形式的参数,一种是ANSI sql绑定[inform of?],另一种是Oracle样式绑定[形式为:1]。

所以你的查询可以是:

    Select * from table1 a, table2 b where a.id1 = b.id1 
and a.id2 = b.id2 and id3 = :1

答案 1 :(得分:0)

如果我理解正确,那么

SELECT * FROM TAB1 I
         JOIN TAB2 II ON I.id1 = II.id1 and I.id2 = II.id2
WHERE I.id3 = 'z0' 

如果产生您需要的结果,您可能需要" LEFT OUTER JOIN"如果TAB2不完整。

答案 2 :(得分:0)

假设' A' B'' C'已知查询是

    With extra_cols as(
                    SELECT * FROM (
                                    SELECT * FROM Table2 WHERE id1 = (
                                                                      SELECT id1 FROM TABLE1 WHERE id3 = 'z0'
                                                                      ) 
                                                          AND  id2 = (
                                                                      SELECT id2 FROM TABLE1 WHERE id3 = 'z0'
                                                                      )
                                  )
                  PIVOT (STR(VALUE_1) for FIXED_COL in ('A','B','C')) 
                )
SELECT * from table1 left join extra_cols on table1.id1 = extra_cols.id1 AND table1.id2 = extra_cols.id2
WHERE TABLE1.id3 = 'z0'

' A' ,' B' ,' C'字符串可以从

中提取
SELECT FIXED_COL AS Names FROM Table2 WHERE Table2.id1=(SELECT id1 FROM TABLE1 WHERE id3 = 'z0') AND Table2.id2=(SELECT id2 FROM TABLE1 WHERE id3 = 'z0')