如果找不到某个列,则Oracle Select with返回该列的null值

时间:2015-02-26 06:12:22

标签: sql oracle

如果未找到select列,则我需要在oracle中使用select查询返回null并显示错误消息。

从tbl中选择A,B,C; 但是表只有A,C列

2 个答案:

答案 0 :(得分:0)

以下是您的问题的查询:

SELECT Decode (a, 0, NULL,
                  a) a,
       Decode (b, 0, NULL,
                  b) b,
       Decode (c, 0, NULL,
                  c) c
FROM   (SELECT (SELECT Count (*)
                FROM   user_tab_cols
                WHERE  table_name = 'ABC'
                       AND column_name = 'A') a,
               (SELECT Count (*)
                FROM   user_tab_cols
                WHERE  table_name = 'ABC'
                       AND column_name = 'B') b,
               (SELECT Count (*)
                FROM   user_tab_cols
                WHERE  table_name = 'ABC'
                       AND column_name = 'C') c
        FROM   abc) 

答案 1 :(得分:0)

检查一下:

SELECT b.*, DECODE (a.a, 0, NULL, decode(a.b,0,null, decode(a.c,0,null))) missing_col
FROM (SELECT (SELECT COUNT ( * )
              FROM user_tab_cols
             WHERE table_name = 'ABC' AND column_name = 'A')
              a, (SELECT COUNT ( * )
                    FROM user_tab_cols
                   WHERE table_name = 'ABC' AND column_name = 'B')
                    b, (SELECT COUNT ( * )
                          FROM user_tab_cols
                         WHERE table_name = 'ABC' AND column_name = 'C')
                          c
      FROM abc) a, abc b
      where a.rowid = b.rowid;

显示如下结果:

A          C          MISSING_COL
---------- ---------- -----------
a1         c1                    
a2         c2  

选择了2行。

您可以将MISSING_COL视为不可用的列。