2010-06-01 3 views
7

Généralement, lorsque vous spécifiez une fonction, l'échelle/la précision/la taille du type de données de retour n'est pas définie. Par exemple, vous dites FUNCTION show_price RETURN NUMBER ou FUNCTION show_name RETURN VARCHAR2.Précision de l'intervalle pour la valeur de fonction PL/SQL

Vous n'êtes pas autorisé à avoir FUNCTION show_price RETURN NUMBER(10,2) ou FUNCTION show_name RETURN VARCHAR2(20) et la valeur de retour de la fonction n'est pas restreinte. Maintenant, j'obtiens une erreur de précision (ORA-01873) si je pousse 9999 heures (environ 400 jours) dans ce qui suit. La limite est parce que the default days precision is 2

DECLARE 
    v_int INTERVAL DAY (4) TO SECOND(0); 
    FUNCTION hhmm_to_interval return INTERVAL DAY TO SECOND IS 
    v_hhmm INTERVAL DAY (4) TO SECOND(0); 
    BEGIN 
    v_hhmm := to_dsinterval('PT9999H'); 
    RETURN v_hhmm; 
    -- 
    END hhmm_to_interval; 
BEGIN 
    v_int := hhmm_to_interval; 
end; 
/

et il ne permettra pas la précision à spécifier directement dans le cadre du type de données retourné par la fonction.

DECLARE 
    v_int INTERVAL DAY (4) TO SECOND(0); 
    FUNCTION hhmm_to_interval return INTERVAL DAY (4) TO SECOND IS 
    v_hhmm INTERVAL DAY (4) TO SECOND(0); 
    BEGIN 
    v_hhmm := to_dsinterval('PT9999H'); 
    RETURN v_hhmm; 
    -- 
    END hhmm_to_interval; 
BEGIN 
    v_int := hhmm_to_interval; 
end; 
/

Je peux utiliser une SUBTYPE

DECLARE 
    subtype t_int is INTERVAL DAY (4) TO SECOND(0); 
    v_int INTERVAL DAY (4) TO SECOND(0); 
    FUNCTION hhmm_to_interval return t_int IS 
    v_hhmm INTERVAL DAY (4) TO SECOND(0); 
    BEGIN 
    v_hhmm := to_dsinterval('PT9999H'); 
    RETURN v_hhmm; 
    -- 
    END hhmm_to_interval; 
BEGIN 
    v_int := hhmm_to_interval; 
end; 
/

des inconvénients à l'approche du sous-type?

Des alternatives (par exemple un endroit pour changer la précision par défaut)?

Utilisation de 10gR2.

Répondre

4

Pas de vrais inconvénients auxquels je peux penser. Je pense que ce serait un peu plus clair si les variables de travail ont été declarred comme des instances du sous-type, par exemple .:

DECLARE 
    subtype t_int is INTERVAL DAY (4) TO SECOND(0); 

    v_int t_int; 

    FUNCTION hhmm_to_interval return t_int IS 
    v_hhmm t_int; 
    BEGIN 
    v_hhmm := to_dsinterval('PT9999H'); 
    RETURN v_hhmm; 
    END hhmm_to_interval; 

BEGIN 
    v_int := hhmm_to_interval; 
    DBMS_OUTPUT.PUT_LINE('v_int=' || v_int); 
end; 

Share and Enjoy.

+0

Oui, ce code semble plus propre et avec un seul endroit pour changer la précision. –

Questions connexes