如何获取变量值并将其用于查询Oracle?

时间:2019-02-20 18:23:37

标签: sql oracle plsql oracle11g

我正在开发一个查询,该查询需要使用此声明中返回的值:

DECLARE
    V_CD_AM NUMBER;

    cursor cd_am_cursor IS
       SELECT CD_AMOST
       FROM   L_CHEG
       WHERE  SQ_CHEG = 4153839;
BEGIN
    OPEN cd_am_cursor;
    LOOP
        FETCH cd_am_cursor into V_CD_AM;
        EXIT WHEN cd_am_cursor%NOTFOUND;                 
    END LOOP;
END;

上面的此查询返回两行。 我想获得这两行并在另一个查询上运行它。第二个查询更复杂(有一些联接)。确实,我需要的只是第二个查询的结果。

SELECT AMAN.CD_AMOST_ANA CodAmAnalise,
             AN.DESCR_ANA         DescrAna,
             r.result              Result,
             ch.data_sist          DataCheg,
             AMAN.ORDEM               Ordem,
             aman.limite_atraso       LimiteAtraso,
             aman.idc_const       IdcConst,
             r.cod_obser         CodObser,
             r.tag_equip        Equip,
             SYSDATE                  DataAtual,
             r.seq_result          SeqResult,
             aman.cod_ana         CodAna,
             aman.idc_calc       IdcCalc,
             aman.cd_amost         CdAmost,
             aman.tempo_bloq      TempoBloq,
             r.data_sistema           DataSistema,
             r.idc_status             IdcStatus
        FROM AMOST_ANA AMAN
        JOIN ANALISE AN ON AMAN.CD_ANA = AN.CD_ANA
        LEFT JOIN CHEG_AMAN CHAMAN ON aman.cd_amost_ana =
                                                   chaman.cd_amost_ana
                                               AND aman.cd_amost =
                                                   chaman.cd_amost
                                               AND CHAMAN.seq_cheg =
                                                   4153839
        JOIN CHEG ch ON chaman.sq_cheg = ch.sq_cheg
                                 AND aman.cd_amost = ch.cd_amost
                                 AND ch.data_sistema =
                                     (SELECT MAX(data_sistema)
                                        FROM cheg
                                       WHERE sq_cheg = 4153839)
        JOIN RESULTADO R ON ch.SQ_CHEG = R.SQ_CHEG
                                  AND CHAMAN.CD_AMOST_ANA =
                                      R.COD_AMOST_ANA
                                  AND r.idc_status NOT IN ('T')
                                  AND (r.data_sistema =
                                      (SELECT MAX(data_sistema)
                                          FROM resultado
                                         WHERE sq_cheg = 4153839
                                           AND cd_amost_ana =
                                               CHAMAN.COD_AMOST_ANA
                                           AND idc_status NOT IN ('T')) OR
                                      r.data_sistema IS NULL)
       WHERE AN.IDC_ATIVO = 'True'
         AND aman.cd_amost = //Here in this line I would like use V_CD_AM
         AND AMAN.IDC_ATIVO = 'True'
       ORDER BY AMAN.ORDEM;

还有另一种方法可以执行第二个查询?该查询有效,如果我输入:

AND aman.cd_amost = someRealValue

但是someRealValue必须是动态的,因此我创建了声明V_CD_AM,如何获取此值并正确运行此查询?

1 个答案:

答案 0 :(得分:2)

布拉德的意思是你可以做到:

AND aman.cd_amost in (SELECT CD_AMOST FROM L_CHEG WHERE SQ_CHEG = 4153839)

或者您可以这样做,并删除WHERE子句中的行。

JOIN L_CHEG ON l_cheg.SQ_CHEG = 4153839
  AND aman.cd_amost = l_cheg.cd_amost