Executing Queries and use DBMS_SQL.COLUMN_VALUE to map value : dbms_sql « System Packages « Oracle PL / SQL






Executing Queries and use DBMS_SQL.COLUMN_VALUE to map value

  
SQL> CREATE TABLE lecturer (
  2    id               NUMBER(5) PRIMARY KEY,
  3    first_name       VARCHAR2(20),
  4    last_name        VARCHAR2(20),
  5    major            VARCHAR2(30),
  6    current_credits  NUMBER(3)
  7    );

Table created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10001, 'Scott', 'Lawson','Computer Science', 11);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major, current_credits)
  2                VALUES (10002, 'Mar', 'Wells','History', 4);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10003, 'Jone', 'Bliss','Computer Science', 8);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10004, 'Man', 'Kyte','Economics', 8);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10005, 'Pat', 'Poll','History', 4);

1 row created.

SQL>
SQL> INSERT INTO lecturer (id, first_name, last_name, major,current_credits)
  2                VALUES (10006, 'Tim', 'Viper','History', 4);

1 row created.

SQL>
SQL>
SQL> CREATE TABLE MyTable (
  2    num_col    NUMBER,
  3    char_col   VARCHAR2(60)
  4    );

Table created.

SQL>
SQL>
SQL>
SQL> CREATE OR REPLACE PROCEDURE DynamicQuery (
  2    p_Major1 IN lecturer.major%TYPE DEFAULT NULL,
  3    p_Major2 IN lecturer.major%TYPE DEFAULT NULL) AS
  4
  5    v_CursorID   INTEGER;
  6    v_SelectStmt VARCHAR2(500);
  7    myFirstName  lecturer.first_name%TYPE;
  8    v_LastName   lecturer.last_name%TYPE;
  9    v_Major      lecturer.major%TYPE;
 10    v_Dummy      INTEGER;
 11
 12  BEGIN
 13    v_CursorID := DBMS_SQL.OPEN_CURSOR;
 14
 15    v_SelectStmt := 'SELECT first_name, last_name, major
 16                       FROM lecturer
 17                       WHERE major IN (:m1, :m2)
 18                       ORDER BY major, last_name';
 19
 20    DBMS_SQL.PARSE(v_CursorID, v_SelectStmt, DBMS_SQL.V7);
 21
 22    DBMS_SQL.BIND_VARIABLE(v_CursorID, ':m1', p_Major1);
 23    DBMS_SQL.BIND_VARIABLE(v_CursorID, ':m2', p_Major2);
 24
 25    DBMS_SQL.DEFINE_COLUMN(v_CursorID, 1, myFirstName, 20);
 26    DBMS_SQL.DEFINE_COLUMN(v_CursorID, 2, v_LastName, 20);
 27    DBMS_SQL.DEFINE_COLUMN(v_CursorID, 3, v_Major, 30);
 28
 29    v_Dummy := DBMS_SQL.EXECUTE(v_CursorID);
 30
 31    LOOP
 32      IF DBMS_SQL.FETCH_ROWS(v_CursorID) = 0 THEN
 33        EXIT;
 34      END IF;
 35
 36      DBMS_SQL.COLUMN_VALUE(v_CursorID, 1, myFirstName);
 37      DBMS_SQL.COLUMN_VALUE(v_CursorID, 2, v_LastName);
 38      DBMS_SQL.COLUMN_VALUE(v_CursorID, 3, v_Major);
 39
 40      INSERT INTO MyTable (char_col)VALUES (myFirstName || ' ' || v_LastName || ' is a ' ||v_Major || ' major.');
 41    END LOOP;
 42
 43    DBMS_SQL.CLOSE_CURSOR(v_CursorID);
 44
 45    COMMIT;
 46  EXCEPTION
 47    WHEN OTHERS THEN
 48      DBMS_SQL.CLOSE_CURSOR(v_CursorID);
 49      RAISE;
 50  END DynamicQuery;
 51  /

Procedure created.

SQL>
SQL> show error
No errors.
SQL>
SQL>
SQL> drop table lecturer;

Table dropped.

SQL>
SQL> drop table mytable;

Table dropped.

   
  








Related examples in the same category

1.First DBMS_SQL Example
2.Pass a query statement to a stored procedure
3.Use dbms_sql.open_cursor create a cursor
4.Use dbms_sql.bind_variable, dbms_sql.execute to insert value to a table
5.Use dbms_sql.bind_array to bind array to a cursor
6.Use dbms_sql.parse to bind a select statement to a cursor
7.Call dbms_sql.describe_columns2 to get info for a column
8.Non-Query DML and DDL Statements
9.Use DBMS_SQL with the RETURNING clause.
10.DBMS_SQL.NATIVE;
11.DBMS_SQL.PARSE and DBMS_SQL.EXECUTE
12.Dynamic sql statement with variable binding
13.Dynamic select statement and row fetch
14.Execute PL/SQL block
15.DBMS_SQL package: dynamic SQL
16.Pass where clause to a stored procedure
17.Executing PL/SQL Blocks and use BIND_VARIABLE to bind variable
18.drop the supplied table using dynamic SQL.
19.DBMS_SQL.BIND_VARIABLE_CHAR
20.illustrate the importance of setting out_value_size.
21.Calling a function which uses dynamic SQL from within an SQL statement.
22.Illustrates the interaction of roles and dynamic SQL.
23.illustrates a DDL statement which is built dynamically from the procedure parameters.
24.Use dbms_sql to process query, cursor and value
25.Use dbms_sql.bind_variable to bind variable
26.dbms_sql.varchar2_table type variable