1

我有以下查询,效果很好!

唯一的事情是我需要 DESC 顺序中的结果集,并且我附加到查询末尾的以下 ORDER BY 子句会生成以下 Oracle 错误:

ORA-00933:SQL 命令未正确结束 00933. 00000 -“SQL 命令未正确结束” *原因:
*操作:行错误:46 列:54

这是查询。同样,此查询有效。只是当我添加以下行时:

FINAL_SEARCH order by FINAL_SEARCH.DOC_HDR_ID desc

一切都破裂了。

有任何想法吗?我相信我的 ALIAS 与它有关,但我可能是错的。

询问 -

    Select * from ( select DISTINCT(DOC_HDR.DOC_HDR_ID), 
    DOC_HDR.INITR_PRNCPL_ID, 
    DOC_HDR.DOC_HDR_STAT_CD, 
    DOC_HDR.CRTE_DT, 
    DOC_HDR.TTL, 
    DOC_HDR.APP_DOC_STAT, 
    DOC1.DOC_TYP_NM, 
    DOC1.LBL, DOC1.DOC_HDLR_URL, 
    DOC1.ACTV_IND  
    from KREW_DOC_TYP_T DOC1, KREW_DOC_HDR_T DOC_HDR   
    where DOC_HDR.INITR_PRNCPL_ID IN ( '10000000001' )  and 
        ( DOC1.DOC_TYP_NM =  'PO' or  
          DOC1.DOC_TYP_NM = 'POA' or  
          DOC1.DOC_TYP_NM = 'POC' or  
          DOC1.DOC_TYP_NM= 'POPH' or  
          DOC1.DOC_TYP_NM ='PORH' or  
          DOC1.DOC_TYP_NM = 'POR' or  
          DOC1.DOC_TYP_NM = 'PORT' or  
          DOC1.DOC_TYP_NM = 'POSP' or 
          DOC1.DOC_TYP_NM = 'POV') and 
          DOC_HDR.DOC_HDR_STAT_CD!= 'I' and  
          DOC_HDR.DOC_TYP_ID = DOC1.DOC_TYP_ID ) 

    UNION 

    Select DISTINCT DOC_HDR.DOC_HDR_ID, 
            DOC_HDR.INITR_PRNCPL_ID, 
            DOC_HDR.DOC_HDR_STAT_CD, 
            DOC_HDR.CRTE_DT, 
            DOC_HDR.TTL, 
            DOC_HDR.APP_DOC_STAT, 
            DOC1.DOC_TYP_NM, 
            DOC1.LBL, 
            DOC1.DOC_HDLR_URL, 
            DOC1.ACTV_IND 
     FROM KREW_DOC_TYP_T DOC1, 
   KREW_DOC_HDR_T DOC_HDR 
   WHERE DOC1.DOC_TYP_NM = 'PO' AND CAST(DOC_HDR.DOC_HDR_ID AS VARCHAR(14)) = ANY ( 
      Select PPT.FDOC_NBR 
      FROM PUR_PO_T PPT, PL_PURCHASE_LOG_PO_T PPL 
      WHERE PPT.AP_PUR_DOC_LNK_ID = ANY ( 
         Select PRT.AP_PUR_DOC_LNK_ID 
         FROM PUR_REQS_T PRT, 
              KREW_DOC_HDR_T DOC_HDR 
         WHERE CAST(DOC_HDR.DOC_HDR_ID AS VARCHAR(14)) = PRT.FDOC_NBR AND 
           DOC_HDR.INITR_PRNCPL_ID IN ( '10000000001' ) )) FINAL_SEARCH order by FINAL_SEARCH.DOC_HDR_ID desc;

更新 - 我删除了 ALIAS 并在两个查询之后放置了“ORDER BY”语句,它在 SQL Developer 中工作;但是,当我通过使用 Java 生成此查询的 Web 应用程序执行此查询时,我收到一个关于无效符号的 ORACLE 错误。无效符号是用于终止第一个 ORDER BY 语句的分号。它不会在 SQL Developer 中以其他方式工作,但显然当我执行通过 Web 应用程序生成的相同语句时 - 它失败了。

4

4 回答 4

2

您需要计算括号 - 合并的两个查询应该在一个括号中 - 然后您的别名和 SELECT * FROM 将起作用。

SELECT *
    FROM ( (SELECT DISTINCT (doc_hdr.doc_hdr_id),
                            doc_hdr.initr_prncpl_id,
                            doc_hdr.doc_hdr_stat_cd,
                            doc_hdr.crte_dt,
                            doc_hdr.ttl,
                            doc_hdr.app_doc_stat,
                            doc1.doc_typ_nm,
                            doc1.lbl,
                            doc1.doc_hdlr_url,
                            doc1.actv_ind
              FROM krew_doc_typ_t doc1, krew_doc_hdr_t doc_hdr
             WHERE doc_hdr.initr_prncpl_id IN ('10000000001')
               AND (doc1.doc_typ_nm = 'PO'
                 OR doc1.doc_typ_nm = 'POA'
                 OR doc1.doc_typ_nm = 'POC'
                 OR doc1.doc_typ_nm = 'POPH'
                 OR doc1.doc_typ_nm = 'PORH'
                 OR doc1.doc_typ_nm = 'POR'
                 OR doc1.doc_typ_nm = 'PORT'
                 OR doc1.doc_typ_nm = 'POSP'
                 OR doc1.doc_typ_nm = 'POV')
               AND doc_hdr.doc_hdr_stat_cd != 'I'
               AND doc_hdr.doc_typ_id = doc1.doc_typ_id)
          UNION
          SELECT DISTINCT doc_hdr.doc_hdr_id,
                          doc_hdr.initr_prncpl_id,
                          doc_hdr.doc_hdr_stat_cd,
                          doc_hdr.crte_dt,
                          doc_hdr.ttl,
                          doc_hdr.app_doc_stat,
                          doc1.doc_typ_nm,
                          doc1.lbl,
                          doc1.doc_hdlr_url,
                          doc1.actv_ind
            FROM krew_doc_typ_t doc1, krew_doc_hdr_t doc_hdr
           WHERE doc1.doc_typ_nm = 'PO'
             AND CAST (doc_hdr.doc_hdr_id AS VARCHAR (14)) =
                     ANY (SELECT ppt.fdoc_nbr
                            FROM pur_po_t ppt, pl_purchase_log_po_t ppl
                           WHERE ppt.ap_pur_doc_lnk_id =
                                     ANY (SELECT prt.ap_pur_doc_lnk_id
                                            FROM pur_reqs_t prt,
                                                 krew_doc_hdr_t doc_hdr
                                           WHERE CAST (
                                                     doc_hdr.doc_hdr_id AS VARCHAR (14)
                                                 ) = prt.fdoc_nbr
                                             AND doc_hdr.initr_prncpl_id IN
                                                         ('10000000001'))))
         final_search
ORDER BY final_search.doc_hdr_id DESC;
于 2012-08-09T18:25:03.683 回答
1

我认为您正在使用额外的括号')'

 Select * from ( select DISTINCT(DOC_HDR.DOC_HDR_ID), 
    DOC_HDR.INITR_PRNCPL_ID, 
    DOC_HDR.DOC_HDR_STAT_CD, 
    DOC_HDR.CRTE_DT, 
    DOC_HDR.TTL, 
    DOC_HDR.APP_DOC_STAT, 
    DOC1.DOC_TYP_NM, 
    DOC1.LBL, DOC1.DOC_HDLR_URL, 
    DOC1.ACTV_IND  
    from KREW_DOC_TYP_T DOC1, KREW_DOC_HDR_T DOC_HDR   
    where DOC_HDR.INITR_PRNCPL_ID IN ( '10000000001' )  and 
        ( DOC1.DOC_TYP_NM =  'PO' or  
          DOC1.DOC_TYP_NM = 'POA' or  
          DOC1.DOC_TYP_NM = 'POC' or  
          DOC1.DOC_TYP_NM= 'POPH' or  
          DOC1.DOC_TYP_NM ='PORH' or  
          DOC1.DOC_TYP_NM = 'POR' or  
          DOC1.DOC_TYP_NM = 'PORT' or  
          DOC1.DOC_TYP_NM = 'POSP' or 
          DOC1.DOC_TYP_NM = 'POV') and 
          DOC_HDR.DOC_HDR_STAT_CD!= 'I' and  
          DOC_HDR.DOC_TYP_ID = DOC1.DOC_TYP_ID  

    UNION 

    Select DISTINCT DOC_HDR.DOC_HDR_ID, 
            DOC_HDR.INITR_PRNCPL_ID, 
            DOC_HDR.DOC_HDR_STAT_CD, 
            DOC_HDR.CRTE_DT, 
            DOC_HDR.TTL, 
            DOC_HDR.APP_DOC_STAT, 
            DOC1.DOC_TYP_NM, 
            DOC1.LBL, 
            DOC1.DOC_HDLR_URL, 
            DOC1.ACTV_IND 
     FROM KREW_DOC_TYP_T DOC1, 
   KREW_DOC_HDR_T DOC_HDR 
   WHERE DOC1.DOC_TYP_NM = 'PO' AND CAST(DOC_HDR.DOC_HDR_ID AS VARCHAR(14)) = ANY ( 
      Select PPT.FDOC_NBR 
      FROM PUR_PO_T PPT, PL_PURCHASE_LOG_PO_T PPL 
      WHERE PPT.AP_PUR_DOC_LNK_ID = ANY ( 
         Select PRT.AP_PUR_DOC_LNK_ID 
         FROM PUR_REQS_T PRT, 
              KREW_DOC_HDR_T DOC_HDR 
         WHERE CAST(DOC_HDR.DOC_HDR_ID AS VARCHAR(14)) = PRT.FDOC_NBR AND 
           DOC_HDR.INITR_PRNCPL_ID IN ( '10000000001' ) ))) FINAL_SEARCH order by FINAL_SEARCH.DOC_HDR_ID desc;

首先删除最后一个括号形式 where 条件,然后尝试。最后还要加一个括号) FINAL_SEARCH order by

于 2012-08-09T18:26:03.717 回答
1

您忘记了一些括号:

Select * from ( select DISTINCT(DOC_HDR.DOC_HDR_ID), 
    DOC_HDR.INITR_PRNCPL_ID, 
    DOC_HDR.DOC_HDR_STAT_CD, 
    DOC_HDR.CRTE_DT, 
    DOC_HDR.TTL, 
    DOC_HDR.APP_DOC_STAT, 
    DOC1.DOC_TYP_NM, 
    DOC1.LBL, DOC1.DOC_HDLR_URL, 
    DOC1.ACTV_IND  
    from KREW_DOC_TYP_T DOC1, KREW_DOC_HDR_T DOC_HDR   
    where DOC_HDR.INITR_PRNCPL_ID IN ( '10000000001' )  and 
        ( DOC1.DOC_TYP_NM =  'PO' or  
          DOC1.DOC_TYP_NM = 'POA' or  
          DOC1.DOC_TYP_NM = 'POC' or  
          DOC1.DOC_TYP_NM= 'POPH' or  
          DOC1.DOC_TYP_NM ='PORH' or  
          DOC1.DOC_TYP_NM = 'POR' or  
          DOC1.DOC_TYP_NM = 'PORT' or  
          DOC1.DOC_TYP_NM = 'POSP' or 
          DOC1.DOC_TYP_NM = 'POV') and 
          DOC_HDR.DOC_HDR_STAT_CD!= 'I' and  
          DOC_HDR.DOC_TYP_ID = DOC1.DOC_TYP_ID ) 

    UNION 

    (Select DISTINCT DOC_HDR.DOC_HDR_ID, 
            DOC_HDR.INITR_PRNCPL_ID, 
            DOC_HDR.DOC_HDR_STAT_CD, 
            DOC_HDR.CRTE_DT, 
            DOC_HDR.TTL, 
            DOC_HDR.APP_DOC_STAT, 
            DOC1.DOC_TYP_NM, 
            DOC1.LBL, 
            DOC1.DOC_HDLR_URL, 
            DOC1.ACTV_IND 
     FROM KREW_DOC_TYP_T DOC1, 
   KREW_DOC_HDR_T DOC_HDR 
   WHERE DOC1.DOC_TYP_NM = 'PO' AND CAST(DOC_HDR.DOC_HDR_ID AS VARCHAR(14)) = ANY ( 
      Select PPT.FDOC_NBR 
      FROM PUR_PO_T PPT, PL_PURCHASE_LOG_PO_T PPL 
      WHERE PPT.AP_PUR_DOC_LNK_ID = ANY ( 
         Select PRT.AP_PUR_DOC_LNK_ID 
         FROM PUR_REQS_T PRT, 
              KREW_DOC_HDR_T DOC_HDR 
         WHERE CAST(DOC_HDR.DOC_HDR_ID AS VARCHAR(14)) = PRT.FDOC_NBR AND 
           DOC_HDR.INITR_PRNCPL_ID IN ( '10000000001' ) ))) FINAL_SEARCH order by FINAL_SEARCH.DOC_HDR_ID desc;
于 2012-08-09T18:27:12.973 回答
0

消除 ”;” Web 应用程序中用于填充数据集/数据表的符号。

您还可以按块顺序使用列顺序作为别名,例如:

选择.....按 1 asc, 4 desc 排序

于 2012-08-09T18:34:15.020 回答