Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

oracle - Can I perform update inside PL/SQL function?

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...