关于存储过程的问题

bala7229291 2011-06-24 04:39:05
我一直使用SQL Server,最近不得不更换oracle数据库,想把原有的SQL里的存储过程移植到oracle中,可是一开始就遇到问题
如在sql中,我有一个查询test1表的存储过程sp_test1

create proc sp_test1
as
begin
select *
from test1
end


我尝试着在oracle中写成

create or replace procedure sp_test1 is
begin
select *
from test1
end

在PL/SQL下就报错,想请问一下我该怎么才能达到目的
...全文
133 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
bala7229291 2011-06-27
  • 打赏
  • 举报
回复

create or replace procedure sp_test1 is
type t is table of sys.users%rowtype index by binary_integer;
test t;
begin
select * bulk collect into test from sys.users;
end;


我把语句这样运用了,可是编译都没过去

--PROCEDURE SYS.SP_TEST1
--PLS-00597: INTO列表中的表达式'TEST' 无效
--select * bulk collect into test from sys.users;

--ORA-00904: 无效列名
--select * bulk collect into test from sys.users;

--SQL Statement ignored
--select * bulk collect into test from sys.users;

到底是哪里不对啊
hanzs 2011-06-27
  • 打赏
  • 举报
回复
pl/sql中定义游标变量 可以接收sp_test1返回的结果

其它语言类似,具体怎么定义我不太清楚

我想你还是多baidu(google)下,这样对你有好处,这些都是些基本知识 资料还是很多的
bala7229291 2011-06-27
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 hanzs 的回复:]
SQL code

下面是返回表users的结果集,以游标返回
create or replace procedure sp_test1
(o_table out sys_refcursor) as
begin
open o_table for select * from sys.users;
end;
[/Quote]

在请教一下,我怎么调用才能获得结果集呢,或者怎么使用
wubing1111 2011-06-27
  • 打赏
  • 举报
回复
这个问题我来告诉你吧,在SQL中可以返回数据集,但在ORACLE中不能返回数据集,如果你返回的话就和SQL不一样了,要用游标返回游标值。
hanzs 2011-06-27
  • 打赏
  • 举报
回复

下面是返回表users的结果集,以游标返回
create or replace procedure sp_test1
(o_table out sys_refcursor) as
begin
open o_table for select * from sys.users;
end;
bala7229291 2011-06-27
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 hanzs 的回复:]
要看下小版本号,只有9.2.0.3以后才支持bulk collect
[/Quote]

9.0.1.0.0

其实我想实现的功能就很简单,就是向查询 select * from sys.users 一样,当执行存储过程名sp_test1的时候,存储过程返回查询的结果集就行

因为我是之前是使用sqlserver的,对于这样的运用在sqlserver里很简单

create proc sp_test1
as
begin
select * from sys.users
end

就行,可是在oracle里这样的语句显然是不行了,能帮我写一句嘛,谢谢
hanzs 2011-06-27
  • 打赏
  • 举报
回复
要看下小版本号,只有9.2.0.3以后才支持bulk collect
bala7229291 2011-06-27
  • 打赏
  • 举报
回复
是9i的啊
hanzs 2011-06-27
  • 打赏
  • 举报
回复
语句没问题

oracle版本是多少?是不是版本过低不支持bulk collect语句
bala7229291 2011-06-27
  • 打赏
  • 举报
回复

create or replace procedure sp_test1 is
type t is table of sys.users%rowtype index by binary_integer;
test t;
begin
select * bulk collect into test from sys.users;

end;


这是我的代码,我是在PL/SQL里编译的 提示的语句前面已经贴过了。
hanzs 2011-06-27
  • 打赏
  • 举报
回复
你把代码贴出来看下

create or replace procedure sp_test1 is
type t is table of sys.users%rowtype index by binary_integer;
test t;
begin
select * bulk collect into test from sys.users;
end;

这段代码没问题的 我编译是通过的
Rotel-刘志东 2011-06-24
  • 打赏
  • 举报
回复
create or replace procedure sp_test1
is
begin
select * from test1;
end;
tangren 2011-06-24
  • 打赏
  • 举报
回复
create or replace procedure sp_test1 is
type t is table of test1%rowtype;
v_t t;
begin
select * bulk collect into v_t
from test1;
end;
hanzs 2011-06-24
  • 打赏
  • 举报
回复

create or replace procedure sp_test1 is
type t is table of test1%rowtype index by binary_integer;
test t;
begin
select * bulk collect into test from test1;
end;
bala7229291 2011-06-24
  • 打赏
  • 举报
回复
提示赋值语句左边的表达式't'不正确
bala7229291 2011-06-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hanzs 的回复:]
SQL code

create or replace procedure sp_test1 is
type t is table of test1%rowtype;
begin
select * into t
from test1;
end;
[/Quote]

Compilation errors for PROCEDURE FXC.SP_TEST1

Error: PLS-00321: ¸³ÖµÓï¾ä×ó±ßµÄ±í´ïʽ 'T' ²»ÕýÈ·
Line: 4
Text: select * into t

Error: PL/SQL: ORA-00904: ÎÞЧÁÐÃû
Line: 6
Text: from tbs_code;

Error: PL/SQL: SQL Statement ignored
Line: 4
Text: select * into t
hanzs 2011-06-24
  • 打赏
  • 举报
回复

create or replace procedure sp_test1 is
type t is table of test1%rowtype;
begin
select * into t
from test1;
end;

17,377

社区成员

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

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