PL / SQL function learning

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u012414590/article/details/68559690

grammar

CREATE [OR REPLACE] FUNCTION function_name [(
   parameter_1 [IN] [OUT] data_type,
   parameter_2 [IN] [OUT] data_type,
   parameter_N [IN] [OUT] data_type]
    RETURN return_data_type IS
--the declaration statements
BEGIN
   -- the executable statements
   return return_data_type;
   EXCEPTION
    -- the exception-handling statements
END;

1.FUNCTION FUNCTION_NAME function name keyword is specified, by convention, the function name should start with a verb, e.g. convert_to_num
2. function may have zero or more parameters, in parameter_1, parameter_2 specified parameter name, and the like, must be clear in the data_type specify the data type of each parameter, each parameter has one of three modes: iN, OUT and OUT iN
- iN parameter mode parameter is read, if the attempt to change the function of the iN parameter values, the compiler will issue an error message , can speak constants, literals, variables, or expressions to initialize passed as a parameter to the function iN
- OUT mode parameters prior write-only parameter, OUT parameter used to return values to the calling program, passed to the function when the function begins OUT parameter the default value is initialized to its type, regardless of its original value
- iN OUT parameters are read and write parameters, which means that the function values read from the iN OUT function, change its value and returns it to the calling program
3. the function must be at least partially performed in a rETURN statement, the function header rrturn clause specifies the data type of the return value

Examples

Write a function that meets the following requirements :
1. Input parameters
- starting value input is no default value
- end value is no default input value
2. Return Value: value
3. Calls
execute the procedure, the value obtained from the starting to the end value of the accumulation result (eg: function input parameters are 1 and 50, 1 to 50 the results of the accumulated value = 1275)

create or replace function custom_sum(startnum in number, endnum in number)
    return number is
    i  number;
    re number;
begin
    i  := startnum;
    re := 0;
    while i <= endnum loop
        re := re + i;
        i  := i + 1;
    end loop;
    return re;
end;

Call PL / SQL function

PL / SQL function returns a value, so it can be used on the right or the SELECT statement in assignment
to create an anonymous block using the above custom_sum () function

declare
    re number;
begin
    re := custom_sum(1, 50);
    dbms_output.put_line(re);
end;

Get results for the 1275

Guess you like

Origin blog.csdn.net/u012414590/article/details/68559690
Recommended