Professional Documents
Culture Documents
Rem
Rem
Rem
Rem
Rem
Rem
Rem
Rem
Rem
Rem
Rem
the
Rem
Rem
Rem
Rem
Rem
Rem
Rem
Rem
Rem
Rem
Rem
Rem
Rem
Rem
Rem
Rem
Rem
Rem
Rem
ndsdemo.sql
Copyright (c) 1998 By Oracle Corporation
NAME
ndsdemo.sql - Demo Program For Native Dynamic SQL in PL/SQL
The Demo Program demonstrates Native Dynamic SQL in PL/SQL feature.
FUNCTION.
In Pre 8.1, support for Dynamic SQL in PL/SQL is available through
DBMS_SQL package's programmatic interfaces. The DBMS_SQL approach is
cumbersome to use and is much slower than static SQL in PL/SQL.
Native Dynamic SQL in PL/SQL makes dynamic SQL processing in PL/SQL
extremely simple and easy to use. Also it improves the performance of
Dynamic SQL execution in PL/SQL.
To demonstrate the feature, the demo program builds a simple employee
search tool.
NOTES ON EXECUTING THIS DEMO PROGRAM
1) Use svrmgrl for running this demo program.
2) Connect to oracle and run ndsdemo.sql
@ndsdemo.sql
3) Result of the test is printed in file ndsdemo.lis
/*
**
#########################################################################
#
**
**
DESCRIPTION OF DEMO EXAMPLE
**
**
#########################################################################
#
**
** The following example illustrates the Native Dynamic SQL in PL/SQL
** feature.
**
** In the demo example, a general purpose employee search tool is built
using
** Native Dynamic SQL in PL/SQL feature.
**
** Details of all employees is recorded in a table called Employee_desc.
** The columns of Employee_Desc table are
**
** a) Name
VARCHAR2(40) # Name Of Employee #
** b) Email_Id
VARCHAR2(40) # Email address
#
** c) Gender
VARCHAR2(10) # Gender 'MALE','FEMALE' #
** d) Job
VARCHAR2(30) # Job
#
** e) City
VARCHAR2(30) # City belongs to #
** f) Remarks
VARCHAR2(100) # Remarks
#
**
** The demo program takes as an input, a simple English statement with
** predefined keywords like Name, Job(all column names of Employee_desc
table)
** ,'IS', and ','.
**
** For example, the input may be a string 'Name is John, State Is
Newyork'.
**
** In the demo example, package employee_pack has two main procedures
namely
**
**
a) add_entry(ename Varchar2, emailid Varchar2, gender Varchar2,
**
job Varchar2, City Varchar2, remarks Varchar2 ) and
**
b) search(input_string VARCHAR2).
**
** a) Procedure add_entry expects as input, following six variables.
**
i) ename
- Employee Name
**
ii) emailid
- Email Id of the Employee
**
iii) Gender
- Gender of Employee
**
iv) Job
- Job of Employee
**
v) City
- City
**
vi) Remarks
- Special Remarks for Employee
**
**
The procedure then inserts the corresponding employee record into
the
**
Employee_Desc table.
**
** b) Procedure search expects as input, an English Statement for ex.
**
'Name is John, Gender is Male, City is New York'.
**
The procedure then parses the statement and displays the records of
**
all employees which satisfy the criteria given in input English
**
Statement.
**
** Search procedure make use of a procedure parse which parses the input
** statement and extracts the inputed values for each keyword(Name, job
etc).
**
*/
/*
**
#########################################################################
##
**
**
CODING STYLE USED
**
**
#########################################################################
#
**
**
1. Uppercase is used for PL/SQL keywords and lower case for defined
**
items.
**
**
2. Naming conventions followed for user defined types :
**
--------------------------------------------------------------------**
Data Structure Name
Prefixed with
**
--------------------------------------------------------------------**
Nested Table Types
nt_
**
Local Variables
l_
**
Reference Cursor Types
rct_
**
Reference Cursors
rc_
*/
SET ECHO OFF;
SET SERVEROUTPUT ON;
SPOOL ndsdemo.dbg
/*--------------------------------------------------------------------*/
/*
Table for Storing Employee related Data
*/
/*--------------------------------------------------------------------*/
CREATE TABLE employee_desc (
Name
VARCHAR2(40),
Email_Id
VARCHAR2(40),
Gender
VARCHAR2(10),
Job
VARCHAR2(30),
City
VARCHAR2(30),
Remarks
VARCHAR2(100));
/
/
*-----------------------------------------------------------------------*/
/* Declaring a package specification where procedures for adding employee
*/
/* record and searching employee records resides.
*/
/* The procedures declared are namely
*/
/*
a) Initialize_Keywords - Initialize nested table of all keywords
*/
/*
b) parse
- Parsing the input string and extracting
*/
/*
keyword values
*/
/*
c) Add_entry
- Procedure for adding Employee records
*/
/*
d) Search
- Procedure for searching employee
records*/
/*
*/
/* The package also defines two nested table types for the purpose of
*/
/* parsing input statement.
*/
/*
a) nt_keywords - To record all column names of Employee_desc
*/
/*
b) nt_keywords_values - To record values of each Column provided
*/
/*
in input_string
*/
/
*-----------------------------------------------------------------------*/
CREATE OR REPLACE PACKAGE employee_pack IS
TYPE nt_keywords IS TABLE OF VARCHAR2(20);
TYPE nt_keywords_values IS TABLE OF VARCHAR2(50);
keywords
nt_keywords; -- All keywords are stored here.
PROCEDURE initialize_keywords; -- Initialize keywords nested table
PROCEDURE parse(input_string VARCHAR2,
keyword_values OUT nt_keywords_values);
PROCEDURE add_entry(ename VARCHAR2, emailid VARCHAR2, gender
VARCHAR2,
job VARCHAR2, city VARCHAR2, remarks VARCHAR2);
PROCEDURE search(input_string VARCHAR2);
END employee_pack;
/
SHOW ERRORS
/
*-----------------------------------------------------------------------*/
/* Define Body of the package.
*/
/* Procedure parse expects as input a Englist statement. After parsing
the*/
/* Statement, it returns all the keyword's values provided in the input
*/
/* stmt as a nested table.
*/
/* Procedure add_entry expects as input the following six variables
*/
/*
i) ename ii) emailid iii) gender iv) job v) city vi) remarks
*/
/* and adds the corresponding Employee Record into Employee_Desc table.
*/
/* Procedure search expects as input a English Statement and displays the
*/
/* Employee records satisfying the provided criteria.
*/
/
*-----------------------------------------------------------------------*/
CREATE OR REPLACE PACKAGE BODY employee_pack IS
/* Store Keywords in a nested table */
PROCEDURE initialize_keywords
IS
BEGIN
keywords.EXTEND(6);
keywords(1) := 'Name';
keywords(2) := 'Email_id';
keywords(3) := 'gender';
keywords(4) := 'job';
keywords(5) := 'city';
keywords(6) := 'Remarks';
END initialize_keywords;
*/
to*/
its */
DBMS_OUTPUT.PUT('------------------------------------------');
DBMS_OUTPUT.PUT_LINE('------------');
DBMS_OUTPUT.PUT_LINE('******* '||RTRIM(input_string));
DBMS_OUTPUT.PUT('------------------------------------------');
DBMS_OUTPUT.PUT_LINE('------------');
DBMS_OUTPUT.PUT_LINE('Number of Records Matching Input Criteria
='||
' '||l_match_records);
DBMS_OUTPUT.NEW_LINE();
DBMS_OUTPUT.PUT_LINE('Your Employee Search Result Follows :');
'||
'||NVL(l_job,'N.A'));
'||
SPOOL OFF
SPOOL ndsdemo.lis
/*********** Execution Starts Here *************************************/
DECLARE
l_input_str VARCHAR2(500);-- Get user Input Here
BEGIN
/* Initialize Nested Table and Store all Keywords */
employee_pack.keywords := employee_pack.nt_keywords();
employee_pack.initialize_keywords;
/* Set the default buffer size of DBMS_OUTPUT package to 100000 */
DBMS_OUTPUT.ENABLE(100000);
DBMS_OUTPUT.NEW_LINE();
DBMS_OUTPUT.PUT_LINE('**** Adding Few Sample Employee Records ');
/* Add Employee Record For John */
DBMS_OUTPUT.NEW_LINE();
DBMS_OUTPUT.PUT_LINE('** Adding Employee Record Of John ');
employee_pack.add_entry('John','john@hotmail.com','Male','Programmer',
'||