2010-12-03 13 views
0
FUNCTION encounter_for_dataset(p_check_answer_master_id IN check_list_answer_master.check_answer_master_id%TYPE) RETURN NUMBER 
    IS 

    l_key_type  check_list_answer_master.key_type%TYPE; 
    l_key   check_list_answer_master.key%TYPE; 
    l_encounter_id NUMBER := 0; 

    BEGIN 

    IF p_check_answer_master_id IS NOT NULL THEN 

     SELECT clam.key_type, NVL(clam.key,'0') 
     INTO l_key_type, l_key 
     FROM check_list_answer_master clam 
     WHERE clam.check_answer_master_id = p_check_answer_master_id; 

     IF l_key_type = 'E' THEN 

     BEGIN 
      l_encounter_id := TO_NUMBER(l_key); 
     EXCEPTION 
      WHEN OTHERS THEN 
      l_encounter_id := 0; 
     END; 

     END IF; -- l_key_type = 'E' 

    END IF; -- p_check_answer_master_id is not null 

    RETURN l_encounter_id; 

    END encounter_for_dataset; 

Antwort

4

Hier ist eine line-by-line Zusammenfassung

FUNCTION encounter_for_dataset(
    p_check_answer_master_id IN check_list_answer_master.check_answer_master_id%TYPE 
    --this is the input variable (note the table.column%type this forces the variable to adhere that the column type if it changes 
    ) RETURN NUMBER --what type to return 
    IS 
    /** declaration section, note it is using the table.column%type --this is good practice in case they change 
    */ 
    l_key_type  check_list_answer_master.key_type%TYPE; 
    l_key   check_list_answer_master.key%TYPE; 
    l_encounter_id NUMBER := 0; 

    BEGIN 
    --if the passed in value is NOT null do the logic within the IF statement 
    IF p_check_answer_master_id IS NOT NULL THEN 


     --insert key_type into l_key_type, 
     --and insert the key (if null then 0) into l_key 
     --where the check_answer_master_id is equal to the passed in variable 
     --DO NOTE, IF THERE is NO DATA FOUND it will throw a NO_DATA_FOUND exception which is not handled 
     SELECT clam.key_type, NVL(clam.key,'0') 
     INTO l_key_type, l_key 
     FROM check_list_answer_master clam 
     WHERE clam.check_answer_master_id = p_check_answer_master_id; 

     -- if the key type is e, then 'cast' the l_key into a number 
     --when any exception happens during the 'cast' just set it to 0 
     IF l_key_type = 'E' THEN 

     /**this begin..end block allows encapsulation of exception logic as it is used, pretty much a nested try/catch within the function -- this error will not bubble up to the calling program, whereas if the p-check_answer_master_id is not in clam, then that error will bubble up*/ 
     BEGIN 
      l_encounter_id := TO_NUMBER(l_key); 
     EXCEPTION 
      WHEN OTHERS THEN 
      l_encounter_id := 0; 
     END; 

     END IF; -- l_key_type = 'E' 

    END IF; -- p_check_answer_master_id is not null 

     --retrun the value (note it defaults to 0) 
    RETURN l_encounter_id; 

    END encounter_for_dataset; 
3

jemand in einer Reihe passiert tut,

dann, wenn diese Zahl nicht Null ist,

Abfrage aus der check_list_answer_master Tabelle für den Schlüsseltyp

wenn dieser Typ ist 'E' kehrt dann den Schlüssel als 'encounter_id'

andernfalls zurückkehren a 0

+0

können Sie bitte etwas genauer erklären, was jede Zeile tut. ich bin neu tro plsql – code511788465541441

4

Die Oracle documentation alle aus dem Internet frei verfügbar ist. Es ist ziemlich lesbar und nützlich für einen unerfahrenen Benutzer. Die PL/SQL user guide und die SQL Reference wären ein guter Anfang, wenn Randy die Frage nicht zu Ihrer Zufriedenheit beantwortet hätte.

Dies erstellt eine Funktion, die in einem Wert übergeben wird und eine Zahl zurückgibt. Der übergebene Wert wird verwendet, um eine Tabelle nachzuschlagen, basierend auf dem in der Tabelle gefundenen Wert werden verschiedene Werte zurückgegeben. Wenn l_key_type (was in der Tabelle gefunden wird), dann wird der Wert von l_key zurückgegeben, sonst 0.

Verwandte Themen