0

我正在尝试根据存储过程中包含的列来识别表类型。我最初提出的查询如下:


SELECT CASE WHEN col_one. IS NOT NULL THEN 'COL1'
                    WHEN col_two IS NOT NULL THEN 'COL2'
               ELSE 'NEITHER'
               END
        INTO ls_table_type
        FROM (SELECT column_name col_one
          FROM sys.all_tab_cols
          WHERE upper ( owner ) = upper ( '|OWNER|' )
            AND hidden_column = 'NO'
            AND virtual_column = 'NO'
            AND column_id IS NOT NULL
            AND column_name = '|COL1_NAME|'
            AND table_name = upper(|TABLE_NAME|))
          ,(SELECT column_name as col_two
          FROM sys.all_tab_cols
          WHERE upper ( owner ) = upper ( '|OWNER|' )
            AND hidden_column = 'NO'
            AND virtual_column = 'NO'
            AND column_id IS NOT NULL
            AND column_name = '|COL2_NAME|'
            AND table_name = upper(|TABLE_NAME|))

除非两列都存在于表中,否则这不起作用。我确信我错过了一些明显的攻击方式。

4

2 回答 2

0

你可以试试这个:

SELECT CASE WHEN EXISTS (SELECT column_name col_one
      FROM sys.all_tab_cols
      WHERE upper ( owner ) = upper ( '|OWNER|' )
        AND hidden_column = 'NO'
        AND virtual_column = 'NO'
        AND column_id IS NOT NULL
        AND column_name = '|COL1_NAME|'
        AND table_name = upper('|TABLE_NAME|'))
    THEN 'COL1'
    WHEN EXISTS (SELECT column_name as col_two
      FROM sys.all_tab_cols
      WHERE upper ( owner ) = upper ( '|OWNER|' )
        AND hidden_column = 'NO'
        AND virtual_column = 'NO'
        AND column_id IS NOT NULL
        AND column_name = '|COL2_NAME|'
        AND table_name = upper('|TABLE_NAME|'))
    THEN 'COL2'
    ELSE 'NEITHER'
    END
FROM DUAL;        
于 2010-02-25T16:41:08.223 回答
0

此查询应该为您提供所需的信息。如果 col1 和 col2 不存在于同一个表中,那么您将不会得到任何重复的记录。

select table_name, column_name
from all_tab_cols
where owner = '?'
  and column_name in ('col1', 'col2')
  and table_name = '?'; -- this line optional
于 2010-02-25T16:39:26.097 回答