case内部选择查询检查数据

时间:2014-08-01 13:37:28

标签: oracle

我正在插入表中,插入时我正在检查表是否有数据。查询错了我需要有人来解决它。

insert into tab1 values (code , message)
values (1 , (select count(1)
             from user_errors
             where name ='PROC1' case If count(1) = 1 message ='error'
                                      else message='clean')

我想编写一个内部选择来检查过程PROC1是否有错误,如果过程存在于用户错误中那么我应该添加列消息错误

修改:这是代码:

INSERT INTO tab1 (message, user)
 VALUES( 
    (select case when count(1) = 1 then 'error' else 'clean' end 
     from user_errors
     where name = 'P_EMPLOYEE' and sequence = 1),
    (SELECT TEXT
     FROM USER_ERRORS
     WHERE NAME = 'P_EMPLOYEE' AND SEQUENCE = 1),
    (SELECT SYS_CONTEXT('USERENV', 'SESSION_USER') || '-' ||
     SYS_CONTEXT('USERENV', 'OS_USER') || '-' ||
     SYS_CONTEXT('USERENV', 'TERMINAL')
     FROM DUAL)
 )

1 个答案:

答案 0 :(得分:1)

我重写了你的查询:

INSERT INTO tab1 (code, message, usr)
values(
  case
    when nvl((select count(*) from user_errors where name = 'P_EMPLOYEE' and sequence = 1 group by text), 0) = 0  then 'clean'
    else 'error'
  end
, (select text txt from user_errors where name = 'P_EMPLOYEE' and sequence = 1 group by text)
, SYS_CONTEXT('USERENV', 'SESSION_USER') || '-' || SYS_CONTEXT('USERENV', 'OS_USER') || '-' || SYS_CONTEXT('USERENV', 'TERMINAL')
)

如果user_errors视图包含P_EMPLOYEE的记录(无关紧要),则必须将其标记为error,否则请将其标记为clean

但如果我有这项任务,我不会将这些数据存储在任何表格中,我会直接从user_errors视图中获取此信息。