Oracle
選擇返回鎖定的使用者或空行的預設值
我對 Oracle 數據庫知之甚少,並從這裡使用了一些問題來獲取此查詢:
select case when ((sysdate - timestamp)*24 < 1 and returncode <> 0) then (username || '@' || userhost|| ' ON ' || to_char(timestamp,'DD-MON-YYYY HH24:MI:SS')|| ' FAILED AT ' || action_name || ' BECAUSE ' || returncode || decode(returncode,'28000',':Maximum Login Attempt','1004',':Wrong Connection','1005',':NULL Password','1017',':Wrong Password','1045',':Insufficient Priviledge','0',':Login Accepted',':Unkown Code')) else 'none' end as "locked users" from sys.dba_audit_session where (sysdate - timestamp)*24 < 1 and returncode <> 0 order by timestamp;
使用此查詢,我想檢索鎖定在哪個日期的使用者以及導致鎖定的原因,如果沒有使用者,則僅返回“無”而不是“未選擇行”。對於鎖定的使用者,它可以工作,但 else 語句不能。有人可以解釋我為什麼嗎?我需要用 ODBC 修復我的 zabbix 數據庫監控。
如果原始查詢不返回任何行,只需選擇查詢的並集和“無”常量:
with s as ( select case when ((sysdate - timestamp)*24 < 1 and returncode <> 0) then (username || '@' || userhost|| ' ON ' || to_char(timestamp,'DD-MON-YYYY HH24:MI:SS')|| ' FAILED AT ' || action_name || ' BECAUSE ' || returncode || decode(returncode,'28000',':Maximum Login Attempt','1004',':Wrong Connection','1005',':NULL Password','1017',':Wrong Password','1045',':Insufficient Priviledge','0',':Login Accepted',':Unkown Code')) else 'none' end as "locked users" from sys.dba_audit_session where (sysdate - timestamp)*24 < 1 and returncode <> 0 order by timestamp ) select 'none' as "locked_users" from dual where not exists (select * from s) union all select * from s;