创建存储过程出错了,求解答

dslpk 2011-09-15 04:48:22
我用的是oracle10g ,打下面的代码时出错

SQL> create procedure emp_select_pro (id number) as
2 begin
3 select job from emp where empno = id ;
4 end;
5 /

Warning: Procedure created with compilation errors


我打了show error ,它提示
Errors for PROCEDURE SCOTT.EMP_SELECT_PRO:

LINE/COL ERROR
-------- -------------------------------------------
3/1 PLS-00428: 在此 SELECT 语句中缺少 INTO 子句
这是怎么一回事,正确的应该怎么写
...全文
84 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jdsnhan 2011-09-15
  • 打赏
  • 举报
回复
select 的结果放在哪里啊,需要有个into


格式化语句的时候,语句要放在
中间
cnwz 2011-09-15
  • 打赏
  • 举报
回复
存储过程中不能仅仅的SELECT语句,需要使用DML语句对表操作或者INSERT 到变量
这个在回复的格式中可以选择(插入源代码,再指定格式)
dslpk 2011-09-15
  • 打赏
  • 举报
回复
2楼,你的代码是怎么打的,怎么我打出的不是这种格式呢,就是说SQL-CODE是怎么弄的
dslpk 2011-09-15
  • 打赏
  • 举报
回复

SQL> create procedure emp_select_pro (id number) as
2 begin
3 select job from emp where empno = id ;
4 end;
5 /
luoyoumou 2011-09-15
  • 打赏
  • 举报
回复
C:\Documents and Settings\Administrator>sqlplus /nolog

SQL*Plus: Release 10.2.0.1.0 - Production on 星期四 9月 15 17:01:25 2011

Copyright (c) 1982, 2005, Oracle. All rights reserved.

idle> conn scott/bee56915
已连接。
scott@TBWORA> CREATE table userinfo(id number(18,0), name varchar2(30), sex varchar2(10), age number(3,0), address varchar2(100));

表已创建。

scott@TBWORA>
scott@TBWORA> insert into userinfo(id,name,sex,age,address) values(1,'luoyoumou','男',33,'湖南省衡阳市');

已创建 1 行。

scott@TBWORA> insert into userinfo(id,name,sex,age,address) values(2,'miaoxiaoming','男',32,'江西省吉安市');

已创建 1 行。

scott@TBWORA> insert into userinfo(id,name,sex,age,address) values(3,'hanqiguang','男',28,'江西省赣州市');

已创建 1 行。

scott@TBWORA>
scott@TBWORA> commit;

提交完成。

scott@TBWORA>
scott@TBWORA> -- create table userinfo2 as select * from userinfo where 1=2;
scott@TBWORA>
scott@TBWORA> -- i_address 是存储过程的输入参数,o_cur是存储过程的输出游标参数,用以获取返回的结果集!
scott@TBWORA> CREATE OR REPLACE PROCEDURE userinfo_proc(i_address VARCHAR2, o_cur OUT SYS_REFCURSOR)
2 IS
3 sqlstr VARCHAR2(200); -- 定义变量,用以存放SQL语句
4 BEGIN
5 sqlstr := 'SELECT Id, Name, Sex, Age, Address FROM userinfo WHERE Address = :i_address'; -- 给SQL变量赋值,其中 :i_address 是绑定变量
,以提高执行效率!
6 OPEN o_cur FOR sqlstr USING i_address; -- 给游标变量赋值
7 END;
8 /

过程已创建。

scott@TBWORA>
scott@TBWORA> set serveroutput on;
scott@TBWORA> var c_cur refcursor;
scott@TBWORA> exec userinfo_proc('湖南省衡阳市',:c_cur);

PL/SQL 过程已成功完成。

scott@TBWORA> print c_cur;

ID NAME SEX AGE
---------- ------------------------------------------------------------ -------------------- ----------
ADDRESS
------------------------------------------------------------------------------------------------------------------------
1 luoyoumou 男 33
湖南省衡阳市
luoyoumou 2011-09-15
  • 打赏
  • 举报
回复
-- Oracle 不像 SQL Server,能够直接在存储过程里使用select 语句,
-- 当你必须返回结果集时,必须借助游标!
-- 例如:
-- 实例:Oracle存储过程返回数据集

CREATE table userinfo(id number(18,0), name varchar2(30), sex varchar2(10), age number(3,0), address varchar2(100));

insert into userinfo(id,name,sex,age,address) values(1,'luoyoumou','男',33,'湖南省衡阳市');
insert into userinfo(id,name,sex,age,address) values(2,'miaoxiaoming','男',32,'江西省吉安市');
insert into userinfo(id,name,sex,age,address) values(3,'hanqiguang','男',28,'江西省赣州市');

commit;

-- create table userinfo2 as select * from userinfo where 1=2;

-- i_address 是存储过程的输入参数,o_cur是存储过程的输出游标参数,用以获取返回的结果集!
CREATE OR REPLACE PROCEDURE userinfo_proc(i_address VARCHAR2, o_cur OUT SYS_REFCURSOR)
IS
sqlstr VARCHAR2(200); -- 定义变量,用以存放SQL语句
BEGIN
sqlstr := 'SELECT Id, Name, Sex, Age, Address FROM userinfo WHERE Address = :i_address'; -- 给SQL变量赋值,其中 :i_address 是绑定变量,以提高执行效率!
OPEN o_cur FOR sqlstr USING i_address; -- 给游标变量赋值
END;
/
dslpk 2011-09-15
  • 打赏
  • 举报
回复
还有我怎么点插入代码的时候,它不以代码的形式插入,这是为什么

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧