iSelfSchooling.com - Since 1999  References  |  Job Openings
    Home  | Search more  | Oracle Syntax  | Computer Institute   | (Login or Register to access to VIDEOS)
 

Copyright & User Agreement

   Suggestions Email2aFriendHomepage us! |  Bookmark

Services

  Vision/Mission

  Services

  Biography

  Contact Us

 FREE Training

  Start...

  SQL

  PL/SQL

  Forms 

  Reports

  DBA Fundamentals

  Performance

  Prepare for OCP

  ShareUrNotes

...

  Acknowledgement

  Who is who

  University Directory

  Links...

 

 

 

FREE Online Oracle Training for beginners and advanced - The most comprehensive Oracle tutorial

The authors do not guarantee or take any responsibility for the accuracy, or completeness of the information.

BASICS

SQL | PL/SQL

DEVELOPERS

FORMS 2 | REPORTS | Other TOOLS

DBAs

FUNDAMENTALS 2 | PERFORMANCE | OEM

ADVANCE

APPLICATION SERVER | GRID CONTROL | ARTICLES 2 3 4

Advanced - Articles II

Lesson 01 | Lesson 02 | Lesson 03 | Lesson 04 | Lesson 05 | Lesson 06 | Lesson 07 | Lesson 08 | Lesson 09 | Lesson 10 | Lesson 11 | Lesson 12 | Lesson 13 | Lesson 14 | Lesson 15 | Lesson 16 | Lesson 17 | Lesson 18 | Lesson 19 | Lesson 20 | Lesson 21 | Lesson 22 | Lesson 23 | Lesson 24 | Lesson 25 | Lesson 26 | Lesson 27 | Lesson 28 | Lesson 29 | Lesson 30 | Lesson 31 | Lesson 32 | Lesson 33 | Lesson 34 | Lesson 35 |

Lesson 35

"I long to accomplish a great and noble task, but it is my chief duty to accomplish small tasks as if they were great and noble."

-Helen Keller (1880-1968)

This procedure is an example of how to use the DBMS_SQL package. You pass the table name and run the procedure. The procedure will return the number of rows in the table.

 

CREATE OR REPLACE PROCEDURE my_count (p_table in VARCHAR2)
is 
    v_cursor NUMBER;
    v_sql VARCHAR2(200);
    v_result NUMBER;
    v_count NUMBER;
BEGIN
    -- Build a query
    v_sql := 'SELECT count(*) numofrec FROM ' || p_table;

    -- Open the cursor.
    v_cursor := DBMS_SQL.OPEN_CURSOR;

    -- Parse the query.
    DBMS_SQL.PARSE(v_cursor, v_sql, DBMS_SQL.v7);

    -- Set up the columns
    DBMS_SQL.DEFINE_COLUMN(v_cursor, 1, v_count);

    -- Execute the query
    v_result := DBMS_SQL.EXECUTE(v_cursor);

    -- Print the output headers
    DBMS_OUTPUT.NEW_LINE;
    DBMS_OUTPUT.NEW_LINE;

    DBMS_OUTPUT.PUT ('Number of Rows in your ' || p_table || ' is: ');

    -- Fetch the rows
    LOOP
        EXIT WHEN DBMS_SQL.FETCH_ROWS (v_cursor) = 0;
        DBMS_SQL.COLUMN_VALUE(v_cursor, 1, v_count);
    END LOOP;

    -- Print the number of rows.
    DBMS_OUTPUT.PUT_LINE(v_count);

    -- Close the cursor.
    DBMS_SQL.CLOSE_CURSOR(v_cursor);

END my_count;
/

How to execute the procedure.

Before executing the procedure. Set the serveroutput parameter 
to ON, then execute the procedure.

SET SERVEROUTPUT ON
EXECUtE my_count('user_tables');

Or you can use binding variable to execute the procedure.

-- define bind variable. (this is an example of how to define a bind variable)
VARIABLE v_mytable VARCHAR2(100)

-- Assign the table name.
BEGIN
:    v_mytable := 'user_tables';
END;
/

-- Check your define bind variable.
PRINT v_mytable 
--or--
PRINT :v_mytable

-- Execute the my_count procedure using the bind variable.
EXECUTE my_count(:v_mytable);

 

 
 
Google
 
Web web site