如何使用另一列的当前值动态更新列?

时间:2013-06-18 06:52:16

标签: sql oracle oracle10g

我正在寻找类似下面的更新声明:

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更改为空白时,我希望将确认状态更新为注释。即:

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 个答案:

答案 0 :(得分: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

中进行测试

答案 1 :(得分:1)

尝试以下

 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;

触发方法

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;
/

答案 2 :(得分: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,则会将Acknowledge字段更新为null。不确定你提到的那个“prevoius”的东西。如果您还需要其他内容,那么您应该以更清晰的描述更新您的问题。

由于您希望根据其他状态进行更新,因此您可以堆叠decode,因为它基本上就像if...then..else

答案 3 :(得分:0)

UPDATE table
SET comments = COALESCE(comment, acknowledgement_status),
    acknowledgement_status = 'Acknowledge',
    alert_updated_time = SYSDATE,
    acknowledged_by = 'Allen'
WHERE alert_id = 8;