2013-06-18 57 views
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的评论更改为空白时,我希望将确认状态更新为评论。即:

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,我认为如果可以用当前值进行更新会更容易。

帮助表示赞赏。

+0

测试你由以前的'acknowledgement_status'究竟意味着什么? – user75ponic

+0

用户给出的价值是什么?这是“确认”吗? –

+0

@Polppan:由以前的acknowledgement_status我的意思是,这已经是这条update语句之前存在的价值。 – zephyrus

回答

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

+0

这正是我所需要的。工作很美丽。 – zephyrus

+0

为此+1,很好的解决方案。 – user75ponic

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

这将更新commentAcknowledge如果值为null。不确定你提到的那个“prevoius”事情。如果你需要别的东西,那么你应该用更清楚的描述来更新你的问题。

既然你要根据其他国家以及更新,可以叠加的decode,因为它本质上就像一个if...then..else

+0

工作相同'更新表中设置 注释= NVL(textbox.value,acknowledgement_status) acknowledgement_status = dropdown.value, alert_updated_time = SYSDATE, acknowledged_by = sessionid.value; where alert_id = 8;' – zephyrus

+0

我根据你的更新更新了我的答案。 – Devolus

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

与'comments = NVL(textbox.value,acknowledgement_status)一样工作' – zephyrus

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

与'comments = NVL(textbox.value,acknowledgement_status)'一样工作' – zephyrus

+0

@zephyrus你能展示你的JSP代码吗? – user75ponic

+0

@zephyrus正如你已经提到该列的评论不是null,那么'nvl','case when null'语句将不起作用,因为它检查值是否存在于你的表中,在这种情况下它不是null,它不会更新'评论'栏。因此,即使在更新之后,您总是会获得以前的值。在这种情况下,最好的办法是在你的桌子上使用“更新触发器”。 – user75ponic

相关问题