This section provides some useful guidelines when developing business logic using DBMS_SQL.
The purpose of this section is to offer some programming guidelines when developing business logic, including the usage of DBMS_SQL. Please note that Dynamic PL/SQL is quite complex and often requires input and advice from a senior software engineer when using the interfaces:
Below are some examples on how to use Oracle package DBMS_SQL to run business logic within the server environment of Foundation1.
This first example includes a dynamic SELECT-statement and the second example includes a dynamic stored procedure call.
CREATE OR REPLACE FUNCTION Get_Number_Of_Customers ( company_id_ IN VARCHAR2 ) RETURN NUMBER IS temp_ NUMBER; c1_ NUMBER; stmt_ VARCHAR2(2000); dummy_ NUMBER; BEGIN stmt_ := 'SELECT COUNT(*) FROM DEMO_CUSTOMER '|| 'WHERE COMPANY_ID = :comp'; c1_ := dbms_sql.open_cursor; dbms_sql.parse(c1_, stmt_, dbms_sql.native); dbms_sql.define_column(c1_, 1, temp_); dbms_sql.bind_variable(c1_, 'comp', company_id_); dummy_ := dbms_sql.execute(c1_); dummy_ := dbms_sql.fetch_rows(c1_); dbms_sql.column_value(c1_, 1, temp_); dbms_sql.close_cursor(c1_); RETURN(temp_); EXCEPTION WHEN OTHERS THEN IF (dbms_sql.is_open(c1_)) THEN dbms_sql.close_cursor(c1_); END IF; RETURN(0); END Get_Number_Of_Customers; / SHOW ERROR CREATE OR REPLACE FUNCTION Get_Number_Of_Customers_2 ( company_id_ IN VARCHAR2 ) RETURN NUMBER IS temp_ NUMBER; c1_ NUMBER; stmt_ VARCHAR2(2000); dummy_ NUMBER; BEGIN stmt_ := 'BEGIN :count := Get_Number_Of_Customers(:comp); END;'; c1_ := dbms_sql.open_cursor; dbms_sql.parse(c1_, stmt_, dbms_sql.native); dbms_sql.bind_variable(c1_, 'count', temp_); dbms_sql.bind_variable(c1_, 'comp', company_id_); dummy_ := dbms_sql.execute(c1_); dbms_sql.variable_value(c1_, 'count', temp_); dbms_sql.close_cursor(c1_); RETURN(temp_); EXCEPTION WHEN OTHERS THEN IF (dbms_sql.is_open(c1_)) THEN dbms_sql.close_cursor(c1_); END IF; RETURN(0); END Get_Number_Of_Customers_2; / SHOW ERROR