L
L
likilix2016-06-17 13:25:12
Oracle
likilix, 2016-06-17 13:25:12

Why does the update statement not work?

CREATE OR REPLACE FUNCTION SALES.GetCertificateSeq( id in number ) 
  RETURN NUMBER AS PRAGMA autonomous_transaction;

  currentSequence NUMBER(10,0);
  
BEGIN
  currentSequence := 0;
 
  SELECT 
    max(vr.CERTIFICATE) INTO currentSequence
  FROM 
    VAGON_REGISTR vr
  WHERE 
    vr.ID = id;

  IF currentSequence is not null THEN
     return currentSequence;  
  END IF;
  
  currentSequence := CERTIFICATE_SEQ.NEXTVAL;

  UPDATE 
    VAGON_REGISTR vr 
  SET vr.CERTIFICATE = currentSequence WHERE vr.ID = id; 
  
  return currentSequence;

END;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
likilix, 2016-06-17
@likilix

Understood. The problem was in id in number. Replaced id with curId

A
Anton, 2016-06-30
@Oraclist

I recommend that you redo this code.
In the presence of neglect of some development standards.
one.

id in number

The DBMS gives you the ability to create variable names up to 32 characters long. So use it. Come up with a normal name, for example, id_certificate.
2.
currentSequence NUMBER(10,0);
BEGIN
currentSequence := 0;
I believe that you are not paid for lines of code. This should be combined into
currentSequence NUMBER(10,0) := 0 ;

3. A field named ID is evil. When there are 100 entities in the system. You will get confused in these IDs like a child in letters.
4.
currentSequence := 0;
SELECT
max(vr.CERTIFICATE) INTO currentSequence
FROM
VAGON_REGISTR vr
WHERE
vr.ID = id;

The compiler had to swear at this code with WARNING. Type, the value is assigned but not used and re-assigned in the select command.
5. There must be only one exit command from the return block. For branching algorithms, there are If & Case etc.
6. We already wrote about commit (or rollback) above.
7. And yes, Oracle doesn't know camelcase'a GetCertificateSeq. It's better to do this Get_Certificate_Seq

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question