查询以动态查找无效行

时间:2016-02-24 14:12:47

标签: sql oracle

我有一个table_lookup。这是主表,其中包含所有查找代码,如

LOOKUP_TYPE       LOOKUP_CODE   MEANING                             ENABLED_FLAG

EMP_CAT           3             Hourly with fixed hours per week    Y
EMP_CAT           4             Hourly                              Y
EMP_CAT           CAS           Casual                              Y
EMP_CAT           FR            Full-time regular                   Y
EMP_CAT           FR_01         Full-time                           Y
ABSENCE_CATEGORY  DLHM          Leave                               Y
ABSENCE_CATEGORY  DLHNM         Leave on the death of a husband     Y
ABSENCE_CATEGORY  DLR           Leave on the death of a relative    Y
ABSENCE_CATEGORY  GB_ADO        Adoption                            Y
ABSENCE_CATEGORY  GB_PAT_ADO    Paternity adoption                  Y
NATIONALITY       PY            Paraguayan                          Y
NATIONALITY       QA            Qatari                              Y
NATIONALITY       RO            Romanian                            Y
NATIONALITY       RS            Serbian                      

此表在不同的表格中引用,例如 table_assignment

emp no.    name                   Employee category              active/inactive

1          divya                  3                                A
2          abc                    FR                               A
3          XYZ                    4                                I
4          aMY                    100                              A  

table_nationality

Emp no.        nationality               

1              QA
2              RS
4              RO

这就是lookup_code f在table_Assignment employee_category列和table_nationality中的国籍列中翻译。

我有一个类似的查询:

select emp_no.,
       name,
       employee_category
  from table_assignment lookup_assignmen,
       table_lookup lookup_stg
 where lookup_stg.lookup_type = 'EMP_CAT'
   AND LOOKUP_STG.LOOKUP_CODE = lookup_assignmen.employee_category;

类似于table_nationality,类似于:

select emp_no.,
       nationality    
  from TABLE_NATIONALITY lookup_NATIONALITY,
       table_lookup lookup_stg
 where lookup_stg.lookup_type = 'NATIONALITY'
   AND LOOKUP_STG.LOOKUP_CODE = lookup_NATIONALITY.employee_category;

现在我想要一个动态查询/包来检测例如table_assignment是否存在table_lookup中没有的任何员工类别。即给出查找无效的行

例如:在table_assignment中,员工类别列中给出的值为100,而table_lookup中没有该值。应在查询中检索此类值,但查询应该是动态的,以便它应检索table_Assignment,table_nationality或任何其他表中的所有无效查找值。我可以在查询中输入lookup_type和表名,并且应该检索无效值。

我为同一个创建了一个packege,但如果我在列中传递p_tablename列,它就无法正常工作。我该如何修改这个包

CREATE OR REPLACE
PACKAGE body XXHCM_LOOKUP_VALIDATION
AS
PROCEDURE XX_VALIDATION_LOOKUP(
    ERRBUF OUT VARCHAR2,
    RETCODE OUT VARCHAR2,
    P_TABLE_NAME  VARCHAR2,
    P_LOOKUP_TYPE varchar2,
    p_column varchar2)
as
begin

select XXHCM_LOOKUP_EXT.*
from XXHCM_LOOKUP_EXT LOOKUP_STG,
P_TABLE_NAME table_STG
where table_STG.P_LOOKUP_TYPE<>LOOKUP_STG.LOOKUP_CODE
and LOOKUP_STG.LOOKUP_TYPE=P_LOOKUP_TYPE;

dbms_output.put_line('hdhd');
end; 
end;

1 个答案:

答案 0 :(得分:1)

您需要的功能是“dynamic SQL”。您需要为要检查的每个表生成一个SQL语句作为变量,然后执行该变量。这有点令人头疼,但迎合了这种例子。