忽略“ case”语句中的某些条件

时间:2018-11-09 13:31:47

标签: oracle

我正在尝试创建一个脚本,该脚本将从oracle中的系统表中自动获取约束,我只想要主键(P)和外键(R),而我不得不忽略其他约束,例如C,U,V,O

SELECT
    B.COLUMN_NAME,
    CASE
      WHEN A.CONSTRAINT_TYPE = 'P' THEN ', PRIMARY KEY('||B.COLUMN_NAME||')'
      WHEN A.CONSTRAINT_TYPE = 'R' THEN ', FOREIGN KEY('||B.COLUMN_NAME||') 
      REFERENCES'||' '||(SELECT TABLE_NAME FROM all_cons_columns WHERE OWNER 
      ='XXXXXXX' AND COLUMN_NAME = B.COLUMN_NAME AND CONSTRAINT_NAME != 
      A.CONSTRAINT_NAME) ||'('||B.COLUMN_NAME||')'
    ELSE ' '
    END AS CONSTRAINTS
    FROM ALL_CONSTRAINTS A JOIN TABLE_SCRIPT2 B
    ON B.CONSTRAINT_NAME = A.CONSTRAINT_NAME
    WHERE OWNER= 'XXXXXXX' 
    AND TABLE_NAME = 'XXXXXXX'  )

运行脚本时,我得到这样的输出

   , PRIMARY KEY(XXXXXXX)














, FOREIGN KEY(XXXXXX) REFERENCES YYYYY(XXXXX)

对于其他约束,我有空格,如果我删除else条件,我将得到(null)而不是空格,如何只获取P,K而没有null或空格。

1 个答案:

答案 0 :(得分:1)

在WHERE Caluse中添加以下条件

A.CONSTRAINT_TYPE IN ('P','R')