SlideShare a Scribd company logo
1 of 6
DBMS_SQL Revisited


       The DBMS_SQL package supplied by Oracle Corporation is a powerful tool to
evaluate and execute dynamic SQL expressions. The single most important component
of the dbms_sql package is the dbms_sql.parse procedure. In previous releases of the
RDBMS, the parse procedure was limited to 2000 characters. In the latest release of the
RDBMS, this procedure has been overloaded to a table of varchar2, thus setting no limit
on the size of the dynamic SQL. This article will demonstrate the use of this packaged
procedure and in addition will explain a method for executing anonymous PL/SQL blocks
and extracting the result from the block to the calling program.


New Overloaded Procedure dbms_sql.parse:

Description of dbms_sql.parse from Oracle RDBMS 7.3.3.3.0

SQL> desc dbms_sql.parse
PROCEDURE dbms_sql.parse
Argument                             NameType            In/Out Default?
------------------------------ -----------------------   ---------------
C                                   NUMBER(38)                    IN
STATEMENT                           VARCHAR2                      IN
LANGUAGE_FLAG                       NUMBER(38)                    IN
PROCEDURE dbms_sql.parse
Argument Name                     Type                            In/Out Default?
------------------------------ -----------------------            ------ --------
C                                   NUMBER(38)                    IN
STATEMENT                           TABLE OF VARCHAR2(256)        IN
LB                                  NUMBER(38)                    IN
UB                                  NUMBER(38)                    IN
LFFLG                               BOOLEAN                       IN
LANGUAGE_FLAG                      NUMBER(38)                     IN




Fig1: Description of dbms_sql.parse
Note that the table of varchar2 is actually a type of dbms_sql.varchar2s which is
defined in dbmssql.sql in $ORACLE_HOME/rdbms/admin as follows:
type varchar2s is table of varchar2(256) index by binary integer;
c is an integer and holds the familiar cursor handle.
LB denotes the lower bound of the PL/SQL table and UB denotes the upper bound of the
PL/SQL table. That is, a PL/SQL table of varchar2(256) could hold many lines of code,
but they can be selectively parsed and executed using these variables. That is one could
specify LB as 3 and UB as 7 and the SQL code in the PL/SQL table from 3 to 7 will be
parsed and executed. Conceptually, the SQL string is put together as follows:
string:=statement(lb) || statement (lb+1) ||…||statement(ub); .
If LFFLG which is a Boolean variable is set to true, then a new line is inserted after each
piece. This is strongly recommended due to the following reason. When building large
chunks of code, a line of PL/SQL code cannot exceed 2000 characters and the PL/SQL
compiler wraps it to accomplish it. The reason this happens is that all database source
code is stored in dba_source view and its underlying table and in the text column of that
table and is limited to 2000 characters.


       Now users can build a PL/SQL table and specify the upper and lower boundary of
the table as input arguments to dbms_sql.parse instead of the previous varchar2 thus
surpassing the 2000 characters limitation. This is particularly useful when evaluating
complicated business rules and expressions.


Use of the New version of dbms_sql.parse:


       The code that follows demonstrates the usage of the dbms_sql.parse procedure.
Before we demonstrate use of this procedure, we will create a packaged variable and a
function to retrieve that variable. This will be used to demonstrate the use of extracting
the value from an anonymous PL/SQL block using dbms_sql.
Create or Replace package myvar as
Pragma restrict_references(myvar,WNDS,WNPS,RNDS, RNPS);
result number;
end myvar;

Fig 2: Source Code for myvar package


       A discerning user will notice that the package has been subjected to a Pragma
restrict_reference restriction. This is due to the following reason. A function which
retrieves the value of the variable will otherwise return the familiar ORA-06571 error
which is, “Function %s does not guarantee not to update database”.


       Now we will demonstrate the function which will retrieve the value of the
packaged variable.


Create or replace function check_calc
return number is
begin
return myvar.result;
end;

Fig 3: Source Code for the check_calc package.


       Note that the function merely reads the myvar.result package variable and returns
it back to the calling program.


       In this example we will create a table of varchar2, and create an anonymous
PL/SQL block and declare two variables A and B. We will assign them some arbitrary
values and then pass an expression as an argument to the procedure. This expression
could be any valid PL/SQL expression not exceeding 256 characters.             The task of
evaluating expressions greater than 256 characters is left as an exercise to the discerning
reader and just reduces to breaking up a character string greater than 256 characters into
multiple pieces of 256 characters and then appending it to the PL/SQL table.
Note that A and B could be assigned values by any valid SQL function which can
be user defined and hence A and B can be assigned dynamically too. This is not done
here for the sake of simplicity. For example, A and B could well be the age and gender of
an individual stored in a database and very well could be as follows:
a:=getage(855855855);
b:=getgender(855855855);
where 855855855 uniquely identifies an individual in a database table.


        In fact, even the above two statements could be generated dynamically if the
information is stored in a table. It should also be noted that is these attributes are stored
in a database, once could potentially generate the statements in the anonymous PL/SQL
block. The following piece of code demonstrates the use of the new overloaded version
of dbms_sql.parse.


        Note that the anonymous PL/SQL block updates a package variable, which is
available throughout the session. Examples of dbms_sql.parse with the three arguments
abound in Oracle literature and accessing data through define_column procedure and
column_value procedure.       The limitation of dbms_sql.parse with three arguments is
already know. Define_column and column_value is fine when one has to access database
data and cursor data respectively. The packaged variable method does not require use of
these procedures and their limitations if any.


        Note that even though sqlstring is a table of varchar2(256), it should be casted as
dbms_sql.varchar2s.     The following section of code demonstrates the use of the new
dbms_sql.parse and the use of the packaged variable and its associated function which
retrieves it.


        The good thing about anonymous PL/SQL blocks is that, they don‟t need to be
wrapped in procedures/functions/packages thus causing DDL issues.
create or replace procedure democalc
(
calc_text in varchar2, //* The calculation string for example: “result:=A*B;”
return_result in out number,
retval in out number
)
IS


sqlstring dbms_sql.varchar2s; /* Created to hold the PL/SQL Block */
cursor1 integer;
i, j integer;
new_line_flag BOOLEAN:=TRUE;

BEGIN

sqlstring(1):='declare ';
sqlstring(2):='result number(38,2):=0.0;';
sqlstring(3):='a number(38,2):=2;';
sqlstring(4):='b number(38,2):=3;';
sqlstring(5):='begin';
sqlstring(6):=calc_text;
sqlstring(7):='myvar.result:=result;';
sqlstring(8):='end;';
 i=1;
j=8;

cursor1:=dbms_sql.open_cursor;
dbms_sql.parse(cursor1,sqlstring,i,j,new_line_flag,DBMS_SQL.V7);
retval:=dbms_sql.execute(cursor1);
dbms_sql.close_cursor(cursor1);
return_result:=check_calc;


END;
/
The following section demonstrates the execution of the democalc procedure.


 SQL> set serveroutput on
 SQL> declare
       temp1 number; temp2 number;
       begin
        democalc(„result:=A*P;‟, temp1,temp2);
        dbms_output.put_line(„The result is „||temp1);
       end;
       /
 The result is 6
 SQL>



Conclusions:
               The new overloaded version of dbms_sql.parse obviates the previous limit
of the dbms_sql and provides even more flexibility to the developer.   A system which
provides a framework to evaluate simple, dynamic PL/SQL expressions and which uses
the above version of dbms_sql.parse is in development at an Oracle client site by Oracle
consulting and is expected to go live soon.


Acknowledgements:
               The brilliant idea of using packaged variables to exchange values from
anonymous PL/SQL blocks was suggested by an Oracle Technical manager, Roger Raj.


About the Author:
               Mahesh Vallampati is a senior consultant with the Oracle National
Telecom practice, Oracle Corporation. Though he is a DBA by profession, occasionally
he is called on by Oracle customers for his PL/SQL expertise which he dabbles in. He
can be reached at his convenience at mvallamp@us.oracle.com.

More Related Content

What's hot

Oracle PLSQL Step By Step Guide
Oracle PLSQL Step By Step GuideOracle PLSQL Step By Step Guide
Oracle PLSQL Step By Step GuideSrinimf-Slides
 
Mainframe refresher-part-1
Mainframe refresher-part-1Mainframe refresher-part-1
Mainframe refresher-part-1vishwas17
 
Oracle PL/SQL online training | PL/SQL online Training
Oracle PL/SQL online training | PL/SQL online TrainingOracle PL/SQL online training | PL/SQL online Training
Oracle PL/SQL online training | PL/SQL online Trainingsuresh
 
10g plsql slide
10g plsql slide10g plsql slide
10g plsql slideTanu_Manu
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQLPooja Dixit
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals INick Buytaert
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts Bharat Kalia
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sqlÑirmal Tatiwal
 
SQL/MX 3.6 Select for update feature
SQL/MX 3.6 Select for update featureSQL/MX 3.6 Select for update feature
SQL/MX 3.6 Select for update featureFrans Jongma
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management Systemsweetysweety8
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-onlyAshwin Kumar
 
Advanced plsql mock_assessment
Advanced plsql mock_assessmentAdvanced plsql mock_assessment
Advanced plsql mock_assessmentSaurabh K. Gupta
 
Oracle db subprograms
Oracle db subprogramsOracle db subprograms
Oracle db subprogramsSimon Huang
 

What's hot (20)

Oracle etl openworld
Oracle etl openworldOracle etl openworld
Oracle etl openworld
 
Oracle PLSQL Step By Step Guide
Oracle PLSQL Step By Step GuideOracle PLSQL Step By Step Guide
Oracle PLSQL Step By Step Guide
 
ORACLE PL SQL
ORACLE PL SQLORACLE PL SQL
ORACLE PL SQL
 
Mainframe refresher-part-1
Mainframe refresher-part-1Mainframe refresher-part-1
Mainframe refresher-part-1
 
Oracle PL/SQL online training | PL/SQL online Training
Oracle PL/SQL online training | PL/SQL online TrainingOracle PL/SQL online training | PL/SQL online Training
Oracle PL/SQL online training | PL/SQL online Training
 
10g plsql slide
10g plsql slide10g plsql slide
10g plsql slide
 
PLSQL
PLSQLPLSQL
PLSQL
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQL
 
Pl sql content
Pl sql contentPl sql content
Pl sql content
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 
PLSQL Tutorial
PLSQL TutorialPLSQL Tutorial
PLSQL Tutorial
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
4. plsql
4. plsql4. plsql
4. plsql
 
Basic sql Commands
Basic sql CommandsBasic sql Commands
Basic sql Commands
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
SQL/MX 3.6 Select for update feature
SQL/MX 3.6 Select for update featureSQL/MX 3.6 Select for update feature
SQL/MX 3.6 Select for update feature
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
 
Advanced plsql mock_assessment
Advanced plsql mock_assessmentAdvanced plsql mock_assessment
Advanced plsql mock_assessment
 
Oracle db subprograms
Oracle db subprogramsOracle db subprograms
Oracle db subprograms
 

Viewers also liked

Database Programming Techniques
Database Programming TechniquesDatabase Programming Techniques
Database Programming TechniquesRaji Ghawi
 
SQL, Embedded SQL, Dynamic SQL and SQLJ
SQL, Embedded SQL, Dynamic SQL and SQLJSQL, Embedded SQL, Dynamic SQL and SQLJ
SQL, Embedded SQL, Dynamic SQL and SQLJDharita Chokshi
 
Introduction to Business Statistics
Introduction to Business StatisticsIntroduction to Business Statistics
Introduction to Business StatisticsAtiq Rehman
 
Introduction to Business Statistics
Introduction to Business StatisticsIntroduction to Business Statistics
Introduction to Business StatisticsMegha Mishra
 
Business Statistics
Business StatisticsBusiness Statistics
Business Statisticsshorab
 
Dynamic SQL in doobie
Dynamic SQL in doobieDynamic SQL in doobie
Dynamic SQL in doobiechibochibo
 
Seniorforsker Uffe Jørgensen; Aarhus Universitet
Seniorforsker Uffe Jørgensen; Aarhus UniversitetSeniorforsker Uffe Jørgensen; Aarhus Universitet
Seniorforsker Uffe Jørgensen; Aarhus UniversitetBertel Bolt-Jørgensen
 
Roll Over Power Point Advanced Edu Safety Gew 6 10 09
Roll Over Power Point Advanced Edu Safety Gew 6 10 09Roll Over Power Point Advanced Edu Safety Gew 6 10 09
Roll Over Power Point Advanced Edu Safety Gew 6 10 09George Wendleton
 
Presentacio Ciutats
Presentacio CiutatsPresentacio Ciutats
Presentacio CiutatsReckonerr
 
Buscadores (Fodehum)
Buscadores (Fodehum)Buscadores (Fodehum)
Buscadores (Fodehum)grupo3fodehum
 
RSC & RIRG
RSC & RIRGRSC & RIRG
RSC & RIRGrockspot
 

Viewers also liked (20)

Sql
SqlSql
Sql
 
Database Programming Techniques
Database Programming TechniquesDatabase Programming Techniques
Database Programming Techniques
 
PL/SQL
PL/SQLPL/SQL
PL/SQL
 
Jena Programming
Jena ProgrammingJena Programming
Jena Programming
 
SQL, Embedded SQL, Dynamic SQL and SQLJ
SQL, Embedded SQL, Dynamic SQL and SQLJSQL, Embedded SQL, Dynamic SQL and SQLJ
SQL, Embedded SQL, Dynamic SQL and SQLJ
 
Introduction to Business Statistics
Introduction to Business StatisticsIntroduction to Business Statistics
Introduction to Business Statistics
 
Introduction to Business Statistics
Introduction to Business StatisticsIntroduction to Business Statistics
Introduction to Business Statistics
 
Business Statistics
Business StatisticsBusiness Statistics
Business Statistics
 
Dynamic SQL in doobie
Dynamic SQL in doobieDynamic SQL in doobie
Dynamic SQL in doobie
 
Selecting best NoSQL
Selecting best NoSQL Selecting best NoSQL
Selecting best NoSQL
 
Seismic Waves
Seismic WavesSeismic Waves
Seismic Waves
 
Passivhus nordvest
Passivhus nordvestPassivhus nordvest
Passivhus nordvest
 
Seniorforsker Uffe Jørgensen; Aarhus Universitet
Seniorforsker Uffe Jørgensen; Aarhus UniversitetSeniorforsker Uffe Jørgensen; Aarhus Universitet
Seniorforsker Uffe Jørgensen; Aarhus Universitet
 
Roll Over Power Point Advanced Edu Safety Gew 6 10 09
Roll Over Power Point Advanced Edu Safety Gew 6 10 09Roll Over Power Point Advanced Edu Safety Gew 6 10 09
Roll Over Power Point Advanced Edu Safety Gew 6 10 09
 
Presentacio Ciutats
Presentacio CiutatsPresentacio Ciutats
Presentacio Ciutats
 
Buscadores (Fodehum)
Buscadores (Fodehum)Buscadores (Fodehum)
Buscadores (Fodehum)
 
RSC & RIRG
RSC & RIRGRSC & RIRG
RSC & RIRG
 
Lets Think
Lets ThinkLets Think
Lets Think
 
Recovery Act Slides
Recovery Act SlidesRecovery Act Slides
Recovery Act Slides
 
House Hunting fun
House Hunting funHouse Hunting fun
House Hunting fun
 

Similar to DBMS_SQL

Deep Dive of ADBMS Migration to Apache Spark—Use Cases Sharing
Deep Dive of ADBMS Migration to Apache Spark—Use Cases SharingDeep Dive of ADBMS Migration to Apache Spark—Use Cases Sharing
Deep Dive of ADBMS Migration to Apache Spark—Use Cases SharingDatabricks
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programmingRushdi Shams
 
02 Writing Executable Statments
02 Writing Executable Statments02 Writing Executable Statments
02 Writing Executable Statmentsrehaniltifat
 
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Alex Zaballa
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...Alex Zaballa
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...Alex Zaballa
 
MySQL Day Roma - MySQL Shell and Visual Studio Code Extension
MySQL Day Roma - MySQL Shell and Visual Studio Code ExtensionMySQL Day Roma - MySQL Shell and Visual Studio Code Extension
MySQL Day Roma - MySQL Shell and Visual Studio Code ExtensionFrederic Descamps
 
Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Prosanta Ghosh
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowAlex Zaballa
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowAlex Zaballa
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowAlex Zaballa
 
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...Ontico
 
Rails DB migrations
Rails DB migrationsRails DB migrations
Rails DB migrationsDenys Kurets
 

Similar to DBMS_SQL (20)

Deep Dive of ADBMS Migration to Apache Spark—Use Cases Sharing
Deep Dive of ADBMS Migration to Apache Spark—Use Cases SharingDeep Dive of ADBMS Migration to Apache Spark—Use Cases Sharing
Deep Dive of ADBMS Migration to Apache Spark—Use Cases Sharing
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
 
plsql.ppt
plsql.pptplsql.ppt
plsql.ppt
 
02 Writing Executable Statments
02 Writing Executable Statments02 Writing Executable Statments
02 Writing Executable Statments
 
Plsql
PlsqlPlsql
Plsql
 
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
 
pl_sql.ppt
pl_sql.pptpl_sql.ppt
pl_sql.ppt
 
Awr report error
Awr report errorAwr report error
Awr report error
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
 
MySQL Day Roma - MySQL Shell and Visual Studio Code Extension
MySQL Day Roma - MySQL Shell and Visual Studio Code ExtensionMySQL Day Roma - MySQL Shell and Visual Studio Code Extension
MySQL Day Roma - MySQL Shell and Visual Studio Code Extension
 
Anything SQL: Lightning Talks
Anything SQL: Lightning TalksAnything SQL: Lightning Talks
Anything SQL: Lightning Talks
 
Securefile LOBs
Securefile LOBsSecurefile LOBs
Securefile LOBs
 
Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013
 
PL/SQL 3 DML
PL/SQL 3 DMLPL/SQL 3 DML
PL/SQL 3 DML
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
 
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...
 
Rails DB migrations
Rails DB migrationsRails DB migrations
Rails DB migrations
 

More from Mahesh Vallampati

Operating a payables shared service organization in oracle cloud oow 2019_v4
Operating a payables shared service organization in oracle cloud oow 2019_v4Operating a payables shared service organization in oracle cloud oow 2019_v4
Operating a payables shared service organization in oracle cloud oow 2019_v4Mahesh Vallampati
 
Oracle BI Publisher to Transform Cloud ERP Reports
Oracle BI Publisher to Transform Cloud ERP ReportsOracle BI Publisher to Transform Cloud ERP Reports
Oracle BI Publisher to Transform Cloud ERP ReportsMahesh Vallampati
 
Cloudy with a chance of 1099
Cloudy with a chance of 1099Cloudy with a chance of 1099
Cloudy with a chance of 1099Mahesh Vallampati
 
Statistical Accounts and Data in Oracle Cloud General Ledger
Statistical Accounts and Data in Oracle Cloud General LedgerStatistical Accounts and Data in Oracle Cloud General Ledger
Statistical Accounts and Data in Oracle Cloud General LedgerMahesh Vallampati
 
Sparse Matrix Manipulation Made easy in an Oracle RDBMS
Sparse Matrix Manipulation Made easy in an Oracle RDBMSSparse Matrix Manipulation Made easy in an Oracle RDBMS
Sparse Matrix Manipulation Made easy in an Oracle RDBMSMahesh Vallampati
 
The Data Architect Manifesto
The Data Architect ManifestoThe Data Architect Manifesto
The Data Architect ManifestoMahesh Vallampati
 
Oracle EBS Change Projects Process Flows
Oracle EBS Change Projects Process FlowsOracle EBS Change Projects Process Flows
Oracle EBS Change Projects Process FlowsMahesh Vallampati
 
Enough Blame for System Performance Issues
Enough Blame for System Performance IssuesEnough Blame for System Performance Issues
Enough Blame for System Performance IssuesMahesh Vallampati
 
Oracle R12 12.1.3 Legal Entity Data Gathering Template
Oracle R12 12.1.3 Legal Entity Data Gathering TemplateOracle R12 12.1.3 Legal Entity Data Gathering Template
Oracle R12 12.1.3 Legal Entity Data Gathering TemplateMahesh Vallampati
 
ERP Manager meets SDLC and CMMI
ERP Manager meets SDLC and CMMIERP Manager meets SDLC and CMMI
ERP Manager meets SDLC and CMMIMahesh Vallampati
 
Oracle 11i OID AD Integration
Oracle 11i OID AD IntegrationOracle 11i OID AD Integration
Oracle 11i OID AD IntegrationMahesh Vallampati
 
Generic Backup and Restore Process
Generic Backup and Restore ProcessGeneric Backup and Restore Process
Generic Backup and Restore ProcessMahesh Vallampati
 
ITP Instance Management Process V2
ITP Instance Management Process V2ITP Instance Management Process V2
ITP Instance Management Process V2Mahesh Vallampati
 

More from Mahesh Vallampati (20)

Operating a payables shared service organization in oracle cloud oow 2019_v4
Operating a payables shared service organization in oracle cloud oow 2019_v4Operating a payables shared service organization in oracle cloud oow 2019_v4
Operating a payables shared service organization in oracle cloud oow 2019_v4
 
Oracle BI Publisher to Transform Cloud ERP Reports
Oracle BI Publisher to Transform Cloud ERP ReportsOracle BI Publisher to Transform Cloud ERP Reports
Oracle BI Publisher to Transform Cloud ERP Reports
 
Cloudy with a chance of 1099
Cloudy with a chance of 1099Cloudy with a chance of 1099
Cloudy with a chance of 1099
 
Banking on the Cloud
Banking on the CloudBanking on the Cloud
Banking on the Cloud
 
Statistical Accounts and Data in Oracle Cloud General Ledger
Statistical Accounts and Data in Oracle Cloud General LedgerStatistical Accounts and Data in Oracle Cloud General Ledger
Statistical Accounts and Data in Oracle Cloud General Ledger
 
Sparse Matrix Manipulation Made easy in an Oracle RDBMS
Sparse Matrix Manipulation Made easy in an Oracle RDBMSSparse Matrix Manipulation Made easy in an Oracle RDBMS
Sparse Matrix Manipulation Made easy in an Oracle RDBMS
 
The Data Architect Manifesto
The Data Architect ManifestoThe Data Architect Manifesto
The Data Architect Manifesto
 
Five pillars of competency
Five pillars of competencyFive pillars of competency
Five pillars of competency
 
Oracle EBS Change Projects Process Flows
Oracle EBS Change Projects Process FlowsOracle EBS Change Projects Process Flows
Oracle EBS Change Projects Process Flows
 
Cutover plan template Tool
Cutover plan template ToolCutover plan template Tool
Cutover plan template Tool
 
CRM Lead Lifecycle Process
CRM Lead Lifecycle ProcessCRM Lead Lifecycle Process
CRM Lead Lifecycle Process
 
Enough Blame for System Performance Issues
Enough Blame for System Performance IssuesEnough Blame for System Performance Issues
Enough Blame for System Performance Issues
 
Oracle R12 12.1.3 Legal Entity Data Gathering Template
Oracle R12 12.1.3 Legal Entity Data Gathering TemplateOracle R12 12.1.3 Legal Entity Data Gathering Template
Oracle R12 12.1.3 Legal Entity Data Gathering Template
 
ERP Manager meets SDLC and CMMI
ERP Manager meets SDLC and CMMIERP Manager meets SDLC and CMMI
ERP Manager meets SDLC and CMMI
 
Oracle 11i OID AD Integration
Oracle 11i OID AD IntegrationOracle 11i OID AD Integration
Oracle 11i OID AD Integration
 
Generic Backup and Restore Process
Generic Backup and Restore ProcessGeneric Backup and Restore Process
Generic Backup and Restore Process
 
OIC Process Flow V7
OIC Process Flow V7OIC Process Flow V7
OIC Process Flow V7
 
XBRL in Oracle 11i and R12
XBRL in Oracle 11i and R12XBRL in Oracle 11i and R12
XBRL in Oracle 11i and R12
 
Sales Process Flow V4
Sales Process Flow V4Sales Process Flow V4
Sales Process Flow V4
 
ITP Instance Management Process V2
ITP Instance Management Process V2ITP Instance Management Process V2
ITP Instance Management Process V2
 

Recently uploaded

Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 

Recently uploaded (20)

Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 

DBMS_SQL

  • 1. DBMS_SQL Revisited The DBMS_SQL package supplied by Oracle Corporation is a powerful tool to evaluate and execute dynamic SQL expressions. The single most important component of the dbms_sql package is the dbms_sql.parse procedure. In previous releases of the RDBMS, the parse procedure was limited to 2000 characters. In the latest release of the RDBMS, this procedure has been overloaded to a table of varchar2, thus setting no limit on the size of the dynamic SQL. This article will demonstrate the use of this packaged procedure and in addition will explain a method for executing anonymous PL/SQL blocks and extracting the result from the block to the calling program. New Overloaded Procedure dbms_sql.parse: Description of dbms_sql.parse from Oracle RDBMS 7.3.3.3.0 SQL> desc dbms_sql.parse PROCEDURE dbms_sql.parse Argument NameType In/Out Default? ------------------------------ ----------------------- --------------- C NUMBER(38) IN STATEMENT VARCHAR2 IN LANGUAGE_FLAG NUMBER(38) IN PROCEDURE dbms_sql.parse Argument Name Type In/Out Default? ------------------------------ ----------------------- ------ -------- C NUMBER(38) IN STATEMENT TABLE OF VARCHAR2(256) IN LB NUMBER(38) IN UB NUMBER(38) IN LFFLG BOOLEAN IN LANGUAGE_FLAG NUMBER(38) IN Fig1: Description of dbms_sql.parse
  • 2. Note that the table of varchar2 is actually a type of dbms_sql.varchar2s which is defined in dbmssql.sql in $ORACLE_HOME/rdbms/admin as follows: type varchar2s is table of varchar2(256) index by binary integer; c is an integer and holds the familiar cursor handle. LB denotes the lower bound of the PL/SQL table and UB denotes the upper bound of the PL/SQL table. That is, a PL/SQL table of varchar2(256) could hold many lines of code, but they can be selectively parsed and executed using these variables. That is one could specify LB as 3 and UB as 7 and the SQL code in the PL/SQL table from 3 to 7 will be parsed and executed. Conceptually, the SQL string is put together as follows: string:=statement(lb) || statement (lb+1) ||…||statement(ub); . If LFFLG which is a Boolean variable is set to true, then a new line is inserted after each piece. This is strongly recommended due to the following reason. When building large chunks of code, a line of PL/SQL code cannot exceed 2000 characters and the PL/SQL compiler wraps it to accomplish it. The reason this happens is that all database source code is stored in dba_source view and its underlying table and in the text column of that table and is limited to 2000 characters. Now users can build a PL/SQL table and specify the upper and lower boundary of the table as input arguments to dbms_sql.parse instead of the previous varchar2 thus surpassing the 2000 characters limitation. This is particularly useful when evaluating complicated business rules and expressions. Use of the New version of dbms_sql.parse: The code that follows demonstrates the usage of the dbms_sql.parse procedure. Before we demonstrate use of this procedure, we will create a packaged variable and a function to retrieve that variable. This will be used to demonstrate the use of extracting the value from an anonymous PL/SQL block using dbms_sql.
  • 3. Create or Replace package myvar as Pragma restrict_references(myvar,WNDS,WNPS,RNDS, RNPS); result number; end myvar; Fig 2: Source Code for myvar package A discerning user will notice that the package has been subjected to a Pragma restrict_reference restriction. This is due to the following reason. A function which retrieves the value of the variable will otherwise return the familiar ORA-06571 error which is, “Function %s does not guarantee not to update database”. Now we will demonstrate the function which will retrieve the value of the packaged variable. Create or replace function check_calc return number is begin return myvar.result; end; Fig 3: Source Code for the check_calc package. Note that the function merely reads the myvar.result package variable and returns it back to the calling program. In this example we will create a table of varchar2, and create an anonymous PL/SQL block and declare two variables A and B. We will assign them some arbitrary values and then pass an expression as an argument to the procedure. This expression could be any valid PL/SQL expression not exceeding 256 characters. The task of evaluating expressions greater than 256 characters is left as an exercise to the discerning reader and just reduces to breaking up a character string greater than 256 characters into multiple pieces of 256 characters and then appending it to the PL/SQL table.
  • 4. Note that A and B could be assigned values by any valid SQL function which can be user defined and hence A and B can be assigned dynamically too. This is not done here for the sake of simplicity. For example, A and B could well be the age and gender of an individual stored in a database and very well could be as follows: a:=getage(855855855); b:=getgender(855855855); where 855855855 uniquely identifies an individual in a database table. In fact, even the above two statements could be generated dynamically if the information is stored in a table. It should also be noted that is these attributes are stored in a database, once could potentially generate the statements in the anonymous PL/SQL block. The following piece of code demonstrates the use of the new overloaded version of dbms_sql.parse. Note that the anonymous PL/SQL block updates a package variable, which is available throughout the session. Examples of dbms_sql.parse with the three arguments abound in Oracle literature and accessing data through define_column procedure and column_value procedure. The limitation of dbms_sql.parse with three arguments is already know. Define_column and column_value is fine when one has to access database data and cursor data respectively. The packaged variable method does not require use of these procedures and their limitations if any. Note that even though sqlstring is a table of varchar2(256), it should be casted as dbms_sql.varchar2s. The following section of code demonstrates the use of the new dbms_sql.parse and the use of the packaged variable and its associated function which retrieves it. The good thing about anonymous PL/SQL blocks is that, they don‟t need to be wrapped in procedures/functions/packages thus causing DDL issues.
  • 5. create or replace procedure democalc ( calc_text in varchar2, //* The calculation string for example: “result:=A*B;” return_result in out number, retval in out number ) IS sqlstring dbms_sql.varchar2s; /* Created to hold the PL/SQL Block */ cursor1 integer; i, j integer; new_line_flag BOOLEAN:=TRUE; BEGIN sqlstring(1):='declare '; sqlstring(2):='result number(38,2):=0.0;'; sqlstring(3):='a number(38,2):=2;'; sqlstring(4):='b number(38,2):=3;'; sqlstring(5):='begin'; sqlstring(6):=calc_text; sqlstring(7):='myvar.result:=result;'; sqlstring(8):='end;'; i=1; j=8; cursor1:=dbms_sql.open_cursor; dbms_sql.parse(cursor1,sqlstring,i,j,new_line_flag,DBMS_SQL.V7); retval:=dbms_sql.execute(cursor1); dbms_sql.close_cursor(cursor1); return_result:=check_calc; END; /
  • 6. The following section demonstrates the execution of the democalc procedure. SQL> set serveroutput on SQL> declare temp1 number; temp2 number; begin democalc(„result:=A*P;‟, temp1,temp2); dbms_output.put_line(„The result is „||temp1); end; / The result is 6 SQL> Conclusions: The new overloaded version of dbms_sql.parse obviates the previous limit of the dbms_sql and provides even more flexibility to the developer. A system which provides a framework to evaluate simple, dynamic PL/SQL expressions and which uses the above version of dbms_sql.parse is in development at an Oracle client site by Oracle consulting and is expected to go live soon. Acknowledgements: The brilliant idea of using packaged variables to exchange values from anonymous PL/SQL blocks was suggested by an Oracle Technical manager, Roger Raj. About the Author: Mahesh Vallampati is a senior consultant with the Oracle National Telecom practice, Oracle Corporation. Though he is a DBA by profession, occasionally he is called on by Oracle customers for his PL/SQL expertise which he dabbles in. He can be reached at his convenience at mvallamp@us.oracle.com.