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があり、現在の値で更新できればもっと簡単だと思いました。

助けていただければ幸いです。

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フィドルでテスト済み

于 2013-06-18T08:30:41.623 に答える
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;
/
于 2013-06-18T07:15:06.767 に答える
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 に答える
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 に答える