SQLForms FUNCTION Capability (followup)

From: <fgreene_at_hayes.com>
Date: 8 Apr 93 08:56:08 EDT
Message-ID: <7097.2bc3e8a8_at_hayes.com>


Recently I posted a query regarding the use of FUNCTIONS (as compared to procedures) in SQL*Forms. I received the following from Oracle which I am posting for the groups information. I have tested the function capability and it operates fine.

------------------reply follows ----------------------------

FRANK -- See the readme file shipped with Forms 3.0.16.8 or greater.
         For your convenience, I've appended that excerpt below. Until
         we had completely tested the mechanism, we did not want to 
         externalize the defintion of functions in our documentation.
         Since we were not sure if the doc sets for Forms 3.0 would be
         reprinted, we included it in the readme file to make sure
         everyone got it with the software where it first became
         supported. 

5.        DECLARING AND USING PL/SQL FUNCTIONS
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          In addition to (Form-Level) procedures, SQL*Forms also
          supports the use of PL/SQL functions. Any designer who has
          used both the built-in packaged procedures and packaged
          functions will understand the simple differences between
          the two. Below we compare the two:
 
             Procedure                       Function
             -------------------------       --------------------------

(+) Defined in the SQL*Forms (+) Defined in the SQL*Forms
'Procedure Definition' 'Procedure Definition' Screen Screen
(+) May be referenced or (+) May be referenced or
copied from "library" copied from "library" form form, as if it were a procedure
(+) Is a Named PL/SQL Block (+) Is a named PL/SQL Block
which performs a task which represents an expression when invoked of an explicit datatype when evaluated
(+) May accept parameters (+) May accept parameters

(+) Has no return value, but (+) MUST return one value, and
May pass back results May *not* pass back results in OUT parameters in OUT parameters.
(+) May have default values (+) May have default values
for formal parameters for formal parameters
(+) Allows trailing params (+) Allows trailing params
possessing deault values possessing default values to be omitted when to be omitted when invoking evaluating
(+) For exception handling, (+) For exception handling,
Behaves like embedded Behaves like embedded PL/SQL block when invoked PL/SQL block when evaluated The syntax for declaring a PL/SQL function parallels that of a Procedure, with the addition of the 'RETURN datatype' clause which identifies the type of value which the function returns: -------------------------------------------------------------- FUNCTION function_name ( parameter_name mode [:= value], ... ) ~~~~ ~~~~~ RETURN datatype ~~~~~~~~ IS BEGIN : RETURN( expression ); : ~~~~~~~~~~ EXCEPTION : RETURN( expression ); : ~~~~~~~~~~ END; -------------------------------------------------------------- MODE: Must be IN for function parameters. ~~~~ VALUE: Optional constant or expression of the same ~~~~~ datatype as the associated parameter to specify a default value for the formal parameter. An explicit value passed-in at evaluation time overrides the default value. DATATYPE: Any of the datatypes that are supported for PL/SQL ~~~~~~~~ formal parameters are supported for the RETURN value of a PL/SQL function. These include DATE, NUMBER, CHAR, and BOOLEAN. Recall that LONG is not a supported datatype of either parameters or return values. EXPRESSION: A constant or expression of the same datatype as ~~~~~~~~~~ declared in the Function Specification's 'RETURN datatype' clause. It is an *ERROR* if a PL/SQL function does *NOT* return a value on exit. The PL/SQL instruction 'RETURN( expr )' returns the given expression as the value of the function and exits with success. Since a function represents an expression of a given datatype, it may be used in precisely the same way that a constant or local variable of the same datatype would be used. One notable exception to this rule is the use of PL/SQL functions in DML statements. Only constants or local variables may be used in the role of 'bind variables' in DML statements. A function value can always be assigned to a local variable of appropriate datatype, and this local variable used in the DML statement, to accomplish the same result. An example of a function, FF_DEPT_SIZE, which returns the total number of employees in a given department follows: +-------------------------------------------------------- | Procedure Name: FF_DEPT_SIZE +-------------------------------------------------------- | ---- Procedure Text ---- | | /* FF_DEPT_SIZE | ** ~~~~~~~~~~~~~ | ** Returns the number of employees in | ** the department whose number is passed in as | ** a parameter. | */ | FUNCTION ff_dept_size ( pp_deptno IN NUMBER ) | | RETURN NUMBER /* Funct. Return Type */ | | IS | lv_temp_count NUMBER; /* Declaration Section */ | | BEGIN | SELECT COUNT(*) | INTO lv_temp_count | FROM EMP | WHERE DEPTNO = pp_deptno; | RETURN ( lv_temp_count ); /* Return Function Val */ | | EXCEPTION | WHEN OTHERS THEN | RETURN ( -1 ); /* Return minus one */ | END; /* if problems arise */ +-------------------------------------------------------- Then, a trigger could use this function in the following way: +-------------------------------------------------------- | Trigger Name: ON-VALIDATE-FIELD +-------------------------------------------------------- | /* | ** If the Total Number of Employees in | ** the current department exceeds 100, | ** then disallow this entry | */ | IF ( ff_dept_size(:DEPT.DEPTNO) > 100 ) THEN | ------------ | MESSAGE('WOW! Current Department is too big now...'); | RAISE FORM_TRIGGER_FAILURE; | ELSE | :CONTROL.TOTAL_EMPS := ff_dept_size( :DEPT.DEPTNO ); | END IF; +-------------------------------------------------------- While the above trigger is not the most efficient way of accomplishing the goal -- i.e. we call the function twice when we could have coded it such that we only used the function once -- it does demonstrate that functions can be used in IF...THEN and assignment statements. NOTE: Anything that can be done with a Function, can also be done with a Procedure which includes an OUT parameter to return the desired value. Anything that can be done with a Procedure, can also be performed with a Function whose RETURN value reports the status of the task performed. So, while the syntax of a Procedure and a Function differs slightly, a particular task can be done via a PL/SQL Procedure or Function based on the syntax which the designer deems most convenient.
Received on Thu Apr 08 1993 - 14:56:08 CEST

Original text of this message