Oracle where子句仅在条件存在时

时间:2013-12-20 13:05:53

标签: oracle11g

您好我想创建一个只在某些情况下会包含WHERE子句的查询

 SELECT  q.job_queue_id    ID_1,
         q.job_id          ID_2,
         j.job_name,
         q.queue_date,
         q.username,
         q.status,
         q.processed_date,
         q.comments,
         q.rowid
    FROM RS_NEW_JOB_QUEUE q
     INNER JOIN RS_NEW_JOB j
      on q.JOB_ID = j.JOB_ID
     /* 
     CASE
      WHEN l_role_id>0 then where q.username=l_name
      ELSE --I don't want to have a where clause
      END
    */ 
   order by q.processed_date desc;

我尝试了不同的方法,但总是出错。 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

根据条件思考。 SQL不是命令式语言。

SELECT  q.job_queue_id    ID_1,
     q.job_id          ID_2,
     j.job_name,
     q.queue_date,
     q.username,
     q.status,
     q.processed_date,
     q.comments,
     q.rowid
FROM RS_NEW_JOB_QUEUE q
INNER JOIN RS_NEW_JOB j ON q.JOB_ID = j.JOB_ID
WHERE (l_role_id>0 AND q.username = l_name) OR l_role_id <= 0
ORDER BY q.processed_date desc;