Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

is it possible, to update table within function? Because mine does not do anything..

CREATE OR REPLACE FUNCTION rep_ort(id INT, status VARCHAR2, end_date DATE, explanation VARCHAR2) 
RETURN VARCHAR2
IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
UPDATE reports
SET 
id = id,
status = status,
end_date = end_date,
explanation = explanation;
commit;
RETURN 'Updated';
END;

select rep_ort('5','Closed','2021-01-12 17:30','Client fault') from dual;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
4.4k views
Welcome To Ask or Share your Answers For Others

1 Answer

Yours is doing nothing because you named parameters with the same name as columns, so you're just updating the whole table to the same values.

Rename parameters and - maybe - include WHERE clause.

Although you can do it, procedures are meant to be used for such a purpose.

CREATE OR REPLACE FUNCTION rep_ort
  (p_id INT, p_status VARCHAR2, p_end_date DATE, p_explanation VARCHAR2)
RETURN VARCHAR2
IS
  PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
  UPDATE reports
  SET
   id          = p_id,
   status      = p_status,
   end_date    = p_end_date,
   explanation = p_explanation;

  commit;
  RETURN 'Updated';
END;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...