PLS-00201: 必须声明标识符 'PKG_CONST.R_CURSOR'
2017-10-10 16:48
1226 查看
一、sql语句:
CREATE TABLE person (
id NUMBER(11) NOT NULL ,
username VARCHAR2(255 ) NULL ,
age NUMBER(11) NULL ,
password VARCHAR2(255) NULL ,
PRIMARY KEY (id)
)
自行插入一些数据;
二、创建查询所有记录(列表)的存储过程如下:
create or replace procedure pro_person_findall(p_cursor out pkg_const.r_cursor) is
begin
open p_cursor for select * from person;
exception
when others then
dbms_output.put_line('获取信息发生错误');
end pro_person_findall;
报错:
PROCEDURE NEB.PRO_PERSON_FINDALL 编译错误
错误:PLS-00201: 必须声明标识符 'PKG_CONST.R_CURSOR'
行:1
文本:create or replace procedure pro_person_findall(p_cursor out pkg_const.r_cursor) is
错误:PL/SQL: Compilation unit analysis terminated
行:0
文本:create or replace procedure pro_person_findall(p_cursor out pkg_const.r_cursor) is
解决方案:
由于oracle存储过程没有返回值,它的所有返回值都是通过out参数来替代的,列表同样也不例外,但由于是集合,所以不能用一般的参数,必须要用pagkage了.所以要分两部分,
1, 建一个程序包。如下:
create or replace package testpackage as
type test_cursor is ref cursor;
end testpackage;
2,建立存储过程,存储过程为:
create or replace procedure pro_person_findall(p_cursor out testpackage.test_cursor) is
begin
open p_cursor for select * from person;
exception
when others then
dbms_output.put_line('获取信息发生错误');
end pro_person_findall;
JAVA代码:
import java.sql.*;
public class TestProcedureThreeFive {
public TestProcedureThreeFive() {}
public static void main(String[] args ){
String driver = "oracle.jdbc.driver.OracleDriver";
String strUrl = "jdbc:oracle:thin:@10.10.1.29:1521:testdb";
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
//CallableStatement cstmt = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(strUrl, "neb", "testneb");
CallableStatement proc = null; //创建执行存储过程的对象
proc = conn.prepareCall("{call pro_person_findall(?) }"); //设置存储过程 call为关键字.
//设置输出参数是一个游标.第一个参数,游标类型
proc.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
proc.execute();
rs = (ResultSet) proc.getObject(1);
while(rs.next()) {
System.out.println(rs.getString(1) + "," + rs.getString(2) + "," + rs.getInt(3) + "," + rs.getString(4));
}
}catch (SQLException ex2) {
ex2.printStackTrace();
}catch (Exception ex2) {
ex2.printStackTrace();
}finally{
try {
if(rs != null){
rs.close();
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
}
catch (SQLException ex1) {
}
}
}
}
结果如下:
1,张三,22,123456
2,李四,23,123456
3,王五,24,123456
CREATE TABLE person (
id NUMBER(11) NOT NULL ,
username VARCHAR2(255 ) NULL ,
age NUMBER(11) NULL ,
password VARCHAR2(255) NULL ,
PRIMARY KEY (id)
)
自行插入一些数据;
二、创建查询所有记录(列表)的存储过程如下:
create or replace procedure pro_person_findall(p_cursor out pkg_const.r_cursor) is
begin
open p_cursor for select * from person;
exception
when others then
dbms_output.put_line('获取信息发生错误');
end pro_person_findall;
报错:
PROCEDURE NEB.PRO_PERSON_FINDALL 编译错误
错误:PLS-00201: 必须声明标识符 'PKG_CONST.R_CURSOR'
行:1
文本:create or replace procedure pro_person_findall(p_cursor out pkg_const.r_cursor) is
错误:PL/SQL: Compilation unit analysis terminated
行:0
文本:create or replace procedure pro_person_findall(p_cursor out pkg_const.r_cursor) is
解决方案:
由于oracle存储过程没有返回值,它的所有返回值都是通过out参数来替代的,列表同样也不例外,但由于是集合,所以不能用一般的参数,必须要用pagkage了.所以要分两部分,
1, 建一个程序包。如下:
create or replace package testpackage as
type test_cursor is ref cursor;
end testpackage;
2,建立存储过程,存储过程为:
create or replace procedure pro_person_findall(p_cursor out testpackage.test_cursor) is
begin
open p_cursor for select * from person;
exception
when others then
dbms_output.put_line('获取信息发生错误');
end pro_person_findall;
JAVA代码:
import java.sql.*;
public class TestProcedureThreeFive {
public TestProcedureThreeFive() {}
public static void main(String[] args ){
String driver = "oracle.jdbc.driver.OracleDriver";
String strUrl = "jdbc:oracle:thin:@10.10.1.29:1521:testdb";
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
//CallableStatement cstmt = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(strUrl, "neb", "testneb");
CallableStatement proc = null; //创建执行存储过程的对象
proc = conn.prepareCall("{call pro_person_findall(?) }"); //设置存储过程 call为关键字.
//设置输出参数是一个游标.第一个参数,游标类型
proc.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
proc.execute();
rs = (ResultSet) proc.getObject(1);
while(rs.next()) {
System.out.println(rs.getString(1) + "," + rs.getString(2) + "," + rs.getInt(3) + "," + rs.getString(4));
}
}catch (SQLException ex2) {
ex2.printStackTrace();
}catch (Exception ex2) {
ex2.printStackTrace();
}finally{
try {
if(rs != null){
rs.close();
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
}
catch (SQLException ex1) {
}
}
}
}
结果如下:
1,张三,22,123456
2,李四,23,123456
3,王五,24,123456
相关文章推荐
- 错误(28,9): PLS-00201: 必须声明标识符 'PKG_IFACE_FOR_FLOW'
- Error: PLS-00201: 必须声明标识符 'EVEN'
- Oracle 存储过程错误之PLS-00201: 必须声明标识符
- 写存储过程时出的错误:PLS-00201: 必须声明标识符
- PLS-00201: 必须声明标识符 'UTL_FILE'
- Oracle 报错:PLS-00201: 必须声明标识符
- (原) PLS-00201: 必须声明标识符 'SYS.DBMS_REPCAT_UTL2@WHORACLE.ORACLE10G.MYNET'
- Oracle报存储过程中调用DBLink同义词出现错误:PLS-00201: 必须声明标识符
- Oracle 存储过程错误之PLS-00201: 必须声明标识符
- PLS-00201:必须说明标识符‘SHOW_SPACE'
- 引用,const声明时必须初始化
- OracleClient.OracleException: ORA-06550: 第 1 行, 第 30 列: PLS-00302: 必须声明 'GET_AGE' 组件
- 必须声明标识符 'DBMS_SYSTEM.SET_EV'
- Error:PLS-00323: 子程序或游标 已在程序包说明中声明, 必须在程序包体中对其进行定义。
- c语言变量和函数声明的修饰符static,extern,#define,const
- error C2065: “IActiveDesktop” : 未声明的标识符
- SimpleCursorAdapter的构造函数被声明为废除,不能用了吗?那该用什么代替呢
- error C2065: “vector”: 未声明的标识符
- C++学习之const成员函数(函数声明后加const,或称常量成员函数)用法详解
- const常量用extern声明定义的问题