Oracle无效标识符错误

时间:2014-03-26 15:41:33

标签: sql oracle

我在内部查询的where子句中获取了t.ev_no的oracle无效标识符错误

首先,让我解释一下我的表结构和查询的目的。我有一个名为vendor的表,其中包含以下列

  1. ev_no
  2. ev_name
  3. ev_address1
  4. ev_address2
  5. ev_address3
  6. ev_city
  7. ev_state
  8. ev_zip
  9. payment_address
  10. ev_mwbe
  11. 我将在gridview中显示此信息,但地址是一列,包含每行的address1,address2,address3,city,state和zip。如果这些列中的任何一个为NULL,那么我必须跳过换行符。因此内部查询用case来处理这个问题。

    问题是某种程度上oracle无法识别内部查询的where子句中的t.ev_no。我的查询如下

    select t.ev_no,
           t.ev_type,
           t.ev_name,
           (select ADD1 || ADD2 || ADD3 || CITY || STATE || ZIP
              from (select CASE
                             WHEN ev_address1 is NULL then
                              ''
                             ELSE
                              ev_address1 || '<br>'
                           END AS "ADD1",
                           CASE
                             WHEN ev_address2 is NULL then
                              ''
                             ELSE
                              ev_address2 || '<br>'
                           END AS "ADD2",
                           CASE
                             WHEN ev_address3 is NULL then
                              ''
                             ELSE
                              ev_address3 || '<br>'
                           END AS "ADD3",
                           CASE
                             WHEN ev_city is NULL then
                              ''
                             ELSE
                              ev_city || '<br>'
                           END AS "CITY",
                           CASE
                             WHEN ev_state is NULL then
                              ''
                             ELSE
                              ev_state || '<br>'
                           END AS "STATE",
                           CASE
                             WHEN ev_zip is NULL then
                              ''
                             ELSE
                              ev_zip || '<br>'
                           END AS "ZIP"
                           from vendor where ev_no = t.ev_no)) as "ADDRESS",
           t.ev_state,
           t.ev_city,
           t.payment_address,
           t.ev_mwbe
      from vendor t
     where UPPER(ev_city) like :city
     order by t.ev_state, t.ev_city, t.ev_name
    

    请告诉我如何纠正错误。

    由于

2 个答案:

答案 0 :(得分:2)

不记得确切的问题但是在oracle中,嵌套查询的内部数量有限,你可以使用别名。要解决此问题,您可以使用WITH子句重写。

WITH addressquery 
     AS (SELECT ev_no, 
                CASE 
                  WHEN ev_address1 IS NULL THEN '' 
                  ELSE ev_address1 
                       || '<br>' 
                END AS "ADD1", 
                CASE 
                  WHEN ev_address2 IS NULL THEN '' 
                  ELSE ev_address2 
                       || '<br>' 
                END AS "ADD2", 
                CASE 
                  WHEN ev_address3 IS NULL THEN '' 
                  ELSE ev_address3 
                       || '<br>' 
                END AS "ADD3", 
                CASE 
                  WHEN ev_city IS NULL THEN '' 
                  ELSE ev_city 
                       || '<br>' 
                END AS "CITY", 
                CASE 
                  WHEN ev_state IS NULL THEN '' 
                  ELSE ev_state 
                       || '<br>' 
                END AS "STATE", 
                CASE 
                  WHEN ev_zip IS NULL THEN '' 
                  ELSE ev_zip 
                       || '<br>' 
                END AS "ZIP" 
         FROM   vendor) 
SELECT t.ev_no, 
       t.ev_type, 
       t.ev_name, 
       a.add1 || a.add2 || a.add3 || a.city || a.state || a.zip AS "ADDRESS", 
       t.ev_state, 
       t.ev_city, 
       t.payment_address, 
       t.ev_mwbe 
FROM   vendor t 
       join addressquery a 
         ON t.ev_no = a.ev_no 
WHERE  1 = 1 
       AND Upper(ev_city) LIKE :city 
ORDER  BY t.ev_state, 
          t.ev_city, 
          t.ev_name 

答案 1 :(得分:0)

所以我发现查询的嵌套导致了这个问题。以下查询可以成功运行,但查询的可读性受到重大影响。

select t.ev_no,
       t.ev_type,
       t.ev_name,
       (select CASE
                 WHEN ev_address1 is NULL then
                  ''
                 ELSE
                  ev_address1 || '<br>'
               END || CASE
                 WHEN ev_address2 is NULL then
                  ''
                 ELSE
                  ev_address2 || '<br>'
               END || CASE
                 WHEN ev_address3 is NULL then
                  ''
                 ELSE
                  ev_address3 || '<br>'
               END || CASE
                 WHEN ev_city is NULL then
                  ''
                 ELSE
                  ev_city || '<br>'
               END || CASE
                 WHEN ev_state is NULL then
                  ''
                 ELSE
                  ev_state || '<br>'
               END || CASE
                 WHEN ev_zip is NULL then
                  ''
                 ELSE
                  ev_zip || '<br>'
               END
          from LMS_APPL.tlsev_external_vendor
         where ev_no = t.ev_no) as "ADDRESS",
       t.ev_state,
       t.ev_city,
       t.payment_address,
       t.ev_mwbe
  from vendor t
 where UPPER(ev_city) like :city
 order by t.ev_state, t.ev_city, t.ev_name