oracle 临时表操作

r_x_y 2009-04-02 09:02:08
步骤一:建立临时表
create global temporary table t_temp
(COL varchar2(30))
on commit preserve rows;

执行该语句;

步骤二:建procedure(在包体里面,定义就不写了),调用若满足function check_st_cursor_status,则给临时表赋值
procedure init_temp_table as
cursor st_cursor is
select * from st_table;
begin
for cur in st_cursor loop
if (check_st_cursor_status(cur.stid)) then
insert into t_temp (COL) values (cur.stid);
commit;
end if;
end loop;

EXCEPTION
when others THEN
dbms_output.put_line(Sqlerrm);
rollback;
end init_temp_table;

步骤三:
通过调用function,返回临时表里的数据集

在执行到加红语句时,cur.stid确实有值,可是,读临时表,里面却没有数据???
请大家帮忙,如何在临时表里赋值。

我的分比较少,不好意思。
谢谢:)
...全文
51 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
r_x_y 2009-04-02
  • 打赏
  • 举报
回复
谢谢lwmonster,解决了。
以前没有搞懂session的具体含义。
同样谢谢zcs_1
谢谢两位啊。
lwmonster 2009-04-02
  • 打赏
  • 举报
回复
[Quote=引用楼主 r_x_y 的帖子:]

在执行到加红语句时,cur.stid确实有值,可是,读临时表,里面却没有数据???
[/Quote]

你应该是在存储过程执行完,在sql命令窗口 select * 吧???
这样的话是不能查到的,因为你的session已经断掉了,临时表的数据就被清空了。
可以这样:

procedure init_temp_table
(
p_cursor out cursor;
)

as
cursor st_cursor is
select * from st_table;
begin
for cur in st_cursor loop
if (check_st_cursor_status(cur.stid)) then
insert into t_temp (COL) values (cur.stid);
commit;
end if;
end loop;

open p_cursor for select * from t_temp ;

EXCEPTION
when others THEN
dbms_output.put_line(Sqlerrm);
rollback;
end init_temp_table;


这样在执行完存储过程后,打开返回的游标就能看到结果了。
r_x_y 2009-04-02
  • 打赏
  • 举报
回复
先谢谢你。

没有数据,
我是想把符合条件的数据(而不是所有数据)放在一个表里,然后返回它们(通过cursor),
难道使用临时表这样行不通??
如果行不通,给个其他方法。谢谢
zcs_1 2009-04-02
  • 打赏
  • 举报
回复
在SQLPLUS中利用SET SERVEROUTPUT ON看存储过程的执行情况

procedure init_temp_table as
cursor st_cursor is
select * from st_table;
begin
for cur in st_cursor loop
if (check_st_cursor_status(cur.stid)) then
insert into t_temp (COL) values (cur.stid);
dbms_output.put_line(cur.stid);
commit;
end if;
end loop;

EXCEPTION
when others THEN
dbms_output.put_line(Sqlerrm);
rollback;
end init_temp_table;

执行上面的存储过程后,查询一个临时表

SELECT * FROM t_temp;

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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