0

我有以下情况:

CREATE OR REPLACE PROCEDURE GETINBOX
(
    inHasAttachments        IN int
)
AS

BEGIN

    SELECT M.MailId,
           M.SenderId,
           E.Emp_Name As "Sender",
           MI.RecipientId,
           M.Subject

    FROM MAIL M INNER JOIN MAILINBOX MI ON M.MailId = MI.MailId


    WHERE MI.RecipientId = '547' AND
          M.NotificationSelected = 'Y'

    IF inHasAttachments = '1' THEN
       AND M.Attachments = 'Y'
    END IF;


END GETINBOX;

是否可以根据参数的值向 where 子句添加条件?

    WHERE MI.RecipientId = '547' AND
          M.NotificationSelected = 'Y'

    IF inHasAttachments = '1' THEN
       AND M.Attachments = 'Y'
    END IF;

显然这是不允许的,但是否可以在 PL/SQL 中以某种方式做到这一点?我知道一种方法是复制查询并根据参数的值执行不同的查询,但我不想复制我的代码。

4

1 回答 1

3

据我了解您的要求:如果参数的值inHasAttachments是,1那么您想进一步过滤M.Attachments = 'Y',如果它的值不是,1那么您不关心M.Attachments。这是附加条件MI.RecipientId = '547' AND M.NotificationSelected = 'Y'

你可以这样做:

SELECT M.MailId,
   M.SenderId,
   E.Emp_Name As "Sender",
   MI.RecipientId,
   M.Subject
FROM MAIL M INNER JOIN MAILINBOX MI ON M.MailId = MI.MailId
WHERE MI.RecipientId = '547' AND M.NotificationSelected = 'Y'
  AND (inHasAttachments <> '1' OR M.Attachments = 'Y')
于 2013-04-23T19:48:43.977 回答