0

我正在寻找如下所示的更新语句:

update table set
comments = NVL (null, acknowledgement_status),
acknowledgement_status = 'Acknowledge',
alert_updated_time = sysdate,
acknowledged_by = 'Allen'
where alert_id = 8;

实际上,需要从 JSP 页面更新这些值。如果用户未能发表评论,则acknowledgement_status用户给出的对应应更新为comments。但是从上面的查询中,前一个acknowledgement_status被设置为comments. 如何解决这个问题?

考虑如下表格内容:

    Alert_ID   Acknowledgement_status   Comments   Alert_updated_time   Acknowledged_by
    --------   ----------------------   --------   ------------------   ---------------
8               OPEN                    None                              AUTO

现在上面是表格内容。JSP 具有注释字段、文本框和acknowledgement_status下拉菜单。当用户将Acknowlegement_status评论更改为空白时,我希望将确认状态更新为评论。IE:

update table set
comments = NVL (textbox.value, acknowledgement_status),
acknowledgement_status = dropdown.value,
alert_updated_time = sysdate,
acknowledged_by = sessionid.value;
where alert_id = 8;

textbox.value = null, dropdown.value = 'Acknowledge', sessionid.value = 'Allen'表格更新如下:

  Alert_ID   Acknowledgement_status   Comments   Alert_updated_time   Acknowledged_by
  --------   ----------------------   --------   ------------------   ---------------
    8            Acknowledge            OPEN          sysdate                 Allen

但我想要的是:

  Alert_ID   Acknowledgement_status    Comments       Alert_updated_time   Acknowledged_by
  --------   ----------------------    --------       ------------------   ---------------
    8            Acknowledge           Acknowledge           sysdate                 Allen

我宁可写,

update table set
comments = NVL (textbox.value, dropdown.value),
acknowledgement_status = dropdown.value,
alert_updated_time = sysdate,
acknowledged_by = sessionid.value;
where alert_id = 8;

但同样,我有计划decode基于dropdown.value,我认为如果可以用当前值更新会更容易。

帮助表示赞赏。

4

4 回答 4

2

这是一种方法,如果您只想传递一次值:

UPDATE tableX t
SET 
  (comments, acknowledgement_status, alert_updated_time, acknowledged_by)
=
  ( SELECT 
      COALESCE(com, ack_st), ack_st, sd, ack_by
    FROM
    ( SELECT 
        textbox.value    AS com,
        dropdown.value   AS ack_st,
        sysdate          AS sd,
        sessionid.value  AS ack_by
      FROM dual
    ) d
  )              
WHERE t.alert_id = 8 ;

SQL-Fiddle中测试

于 2013-06-18T08:30:41.623 回答
1

Try the following

 update table set comments  =   
 case when (comments  is null) then acknowledgement_status else  comments   end,
 acknowledgement_status = 'Acknowledge',
 alert_updated_time = sysdate,
 acknowledged_by = 'Allen'
 where alert_id = 8;

Trigger approach

CREATE OR REPLACE TRIGGER test
    BEFORE UPDATE
    ON table     FOR EACH ROW
DECLARE

begin

    if (:new.comments is null) then

    :new.comments := :new.acknowledgement_status;

    end if;

END;
/
于 2013-06-18T07:15:06.767 回答
0
UPDATE table
SET comments = COALESCE(comment, acknowledgement_status),
    acknowledgement_status = 'Acknowledge',
    alert_updated_time = SYSDATE,
    acknowledged_by = 'Allen'
WHERE alert_id = 8;
于 2013-06-18T07:05:33.380 回答
0
update table set
comments = decode(comment, null, 'Acknowledge', comment),
acknowledgement_status = 'Acknowledge',
alert_updated_time = sysdate,
acknowledged_by = 'Allen'
where alert_id = 8;

如果值为 ,这会将comment字段更新为。不确定您提到的“以前”的事情。如果您需要其他内容,那么您应该使用更清晰的描述更新您的问题。Acknowledgenull

由于您还想根据其他状态进行更新,因此您可以堆叠它,decode因为它本质上就像一个if...then..else

于 2013-06-18T07:02:19.710 回答