/*cppdemo2.pc*/// Pro*C/C++ sample program demonstrating a simple use of Cursor Variables
// implemented within a C++ class framework.  Build this program as follows
//
//   1. Execute the cppdemo2.sql script within SQL*Plus
//   2. Precompile the empclass.pc program as follows
//      > proc code=cpp sqlcheck=full user=scott/tiger lines=yes empclass
//   3. Precompile the cppdemo2.pc program as follows
//      > proc code=cpp lines=yes cppdemo2
//   4. Compile and Link
//
// Note that you may have to specify various include directories using the
// include option when precompiling.#include <stdio.h>
#include <stdlib.h>
#include <sqlca.h>static void sql_error()
{
  printf("%.*s\n", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc);
  EXEC SQL WHENEVER SQLERROR CONTINUE;
  EXEC SQL ROLLBACK WORK RELEASE;
  exit(1);  
}// Physically include the emp class definition in this module.
EXEC SQL INCLUDE empclass.h;int main()
{
  EXEC SQL BEGIN DECLARE SECTION;
    char *uid = "scott/tiger";
  EXEC SQL END DECLARE SECTION;  EXEC SQL WHENEVER SQLERROR DO sql_error();
  EXEC SQL CONNECT :uid;  emp *e = new emp(); // Invoke Constructor - ALLOCATE Cursor Variable.  e->open();          // Open the Cursor.  while (1)
    {
      // Fetch from the Cursor, catching the NOT FOUND condition
      // thrown by the fetch() member function.
      try { e->fetch(); } catch (int code)  
        { if (code == 1403) break; }
      printf("Employee:  %s[%d]\n", e->ename, e->empno);
    }  e->close();         // Close the Cursor.  delete e;           // Invoke Destructor - FREE Cursor Variable.  EXEC SQL ROLLBACK WORK RELEASE;
  return (0);
}
/*empclass.pc*/#include <stdio.h>
#include <stdlib.h>// This example uses a single (global) SQLCA that is shared by the
// emp class implementation as well as the main program for this
// application.
#define SQLCA_STORAGE_CLASS extern
#include <sqlca.h>// Include the emp class specification in the implementation of the
// class body as well as the application program that makes use of it.
EXEC SQL INCLUDE empclass.h;emp::emp()
{
  // The scope of this WHENEVER statement spans the entire module.
  // Note that the error handler function is really a member function
  // of the emp class.
  EXEC SQL WHENEVER SQLERROR DO emp_error();
  EXEC SQL ALLOCATE :emp_cursor;  // Constructor - ALLOCATE Cursor.
}emp::~emp()
{
  EXEC SQL FREE :emp_cursor;      // Destructor - FREE Cursor.
}void emp::open()
{
  EXEC SQL EXECUTE
    BEGIN
      emp_package.open_cursor(:emp_cursor);
    END;
  END-EXEC;
}void emp::close()
{
  EXEC SQL CLOSE :emp_cursor;
}void emp::fetch() throw (int)
{
  EXEC SQL FETCH :emp_cursor INTO :ename, :empno;
  if (sqlca.sqlcode == 1403)
    throw sqlca.sqlcode;     // Like a WHENEVER NOT FOUND statement.
}void emp::emp_error()
{
  printf("%.*s\n", sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc);
  EXEC SQL WHENEVER SQLERROR CONTINUE;
  EXEC SQL ROLLBACK WORK RELEASE;
  exit(1);
}/*cppdemo.sql*/
Rem  This is the SQL script that accompanies the cppdemo2 C++ Demo
Rem  Program.  Run this prior to Precompiling the empclass.pc file.
/
CONNECT SCOTT/TIGER
/
CREATE OR REPLACE VIEW emp_view AS SELECT ename, empno FROM EMP
/
CREATE OR REPLACE PACKAGE emp_package AS
  TYPE emp_cursor_type IS REF CURSOR RETURN emp_view%ROWTYPE;
  PROCEDURE open_cursor(curs IN OUT emp_cursor_type);
END emp_package;
/
CREATE OR REPLACE PACKAGE BODY emp_package AS
  PROCEDURE open_cursor(curs IN OUT emp_cursor_type) IS
  BEGIN
    OPEN curs FOR SELECT ename, empno FROM EMP ORDER BY ename ASC;
  END;
END emp_package;
/
EXIT
/

解决方案 »

  1.   

    有人知道吗?我需要的是unix 下proc的例子
      

  2.   

    同意,上面的例子写得很好,将PROC中如何调用PROCEDURE的要点写的很清楚。
    请你好好看一下。
    如果有不明白的地方,请给我写信。
    [email protected]
      

  3.   

    我仔细看了看,上面的例子好像没有动态调用PROCEDURE,我说的动态调用是指PROCEDURE的名称是不确定的,PROCEDURE名称是个参数,不知如何实现
      

  4.   

    楼主,不好意思借用一下地方,各位知道怎么用pro*c将数据插入oracle中类型为blob的列中吗?我找了好多资料都没有结果,也在csdn上发了贴,可是没人回答,看楼主您这个帖子人多,想问一下大家,实在太急用了,抱歉!!