匿名块中的procedure

minitoy 2010-12-07 05:41:50
今天在网上闲逛,发现一种没见过的匿名块书写方式.谁有这方面的资料?
SQL> set serveroutput on
SQL>
SQL> declare
2 procedure proc_test is
3 begin
4 dbms_output.put_line('just for test');
5 end;
6 begin
7 proc_test;
8 end;
9 /

just for test

PL/SQL procedure successfully completed

SQL>
...全文
209 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
心中的彩虹 2010-12-07
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 minitoy 的回复:]
根据文档整理了下思路.
plsql block 分为两种,一种是匿名块(anonymous blocks).一种是命名块(named blocks ),命名块一般称为子程序(以前也很疑惑到底有没有和匿名块对应的命名块),一般来说包含函数和过程.包应该算是两者的组合.
匿名块和命名块最大的区别就是命名块有名字(废话!嘿嘿),所以命名块可以使用其名称进行调用,实现代码重用.
The basic ……
[/Quote]
只要理解跟包的思路类似 就明白
但是不能全相同
minitoy 2010-12-07
  • 打赏
  • 举报
回复
根据文档整理了下思路.
plsql block 分为两种,一种是匿名块(anonymous blocks).一种是命名块(named blocks ),命名块一般称为子程序(以前也很疑惑到底有没有和匿名块对应的命名块),一般来说包含函数和过程.包应该算是两者的组合.
匿名块和命名块最大的区别就是命名块有名字(废话!嘿嘿),所以命名块可以使用其名称进行调用,实现代码重用.
The basic units (procedures, functions, and anonymous blocks) that make up a PL/SQL program are logical blocks, which can be nested inside one another.Subprograms are named PL/SQL blocks that can be called with a set of parameters. PL/SQL has two types of subprograms: procedures and functions.
心中的彩虹 2010-12-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 minitoy 的回复:]
引用 5 楼 gelyon 的回复:
由于是匿名块 declare begin end 是成对出现
当然也有作用域的问题,不同的块是访问不到别的块的过程或者方法的

是啊.
这个是正确的

SQL code
declare
procedure proc_test1 is
begin
dbms_output.put_line('just for test 1')……
[/Quote]





---你过程声明都的放在匿名快的声明部分
scott@ORCL> ed
已写入 file afiedt.buf

1 declare
2 procedure proc_test1 is
3 begin
4 dbms_output.put_line('just for test 1');
5 end;
6 procedure proc_test2 is
7 begin
8 dbms_output.put_line('just for test 2');
9 end;
10 begin
11 begin
12 proc_test1;
13 end;
14 begin
15 proc_test2;
16 end;
17 proc_test2;
18* end;
scott@ORCL> /
just for test 1
just for test 2
just for test 2

PL/SQL 过程已成功完成。







心中的彩虹 2010-12-07
  • 打赏
  • 举报
回复
[Quote=引用楼主 minitoy 的回复:]
今天在网上闲逛,发现一种没见过的匿名块书写方式.谁有这方面的资料?

SQL code
SQL> set serveroutput on
SQL>
SQL> declare
2 procedure proc_test is
3 begin
4 dbms_output.put_line('just for test');
5 end;
……
[/Quote]

这东西 作用于 做调试过程最好
minitoy 2010-12-07
  • 打赏
  • 举报
回复
21世纪的好男人啊.[Quote=引用 11 楼 gelyon 的回复:]
好了 ,不陪你们了,我回家了,还回去买菜,哎,昨晚做的黑木耳+青笋炒肉,今晚做啥吃好呢?还没想好,去菜市看哈呢
[/Quote]
minitoy 2010-12-07
  • 打赏
  • 举报
回复
恩,意义大概只在于代码重用[Quote=引用 12 楼 java3344520 的回复:]
很少这样用吧,匿名的存储过程外部无法访问
本身存在就是没有多大的意义
[/Quote]
iqlife 2010-12-07
  • 打赏
  • 举报
回复
很少这样用吧,匿名的存储过程外部无法访问
本身存在就是没有多大的意义
gelyon 2010-12-07
  • 打赏
  • 举报
回复
好了 ,不陪你们了,我回家了,还回去买菜,哎,昨晚做的黑木耳+青笋炒肉,今晚做啥吃好呢?还没想好,去菜市看哈呢
gelyon 2010-12-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 minitoy 的回复:]
引用 5 楼 gelyon 的回复:
由于是匿名块 declare begin end 是成对出现
当然也有作用域的问题,不同的块是访问不到别的块的过程或者方法的

是啊.
这个是正确的

SQL code
declare
procedure proc_test1 is
begin
dbms_output.put_line('just for test 1')……
[/Quote]


declare
procedure proc_test1 is
begin
dbms_output.put_line('just for test 1');
end;
begin
proc_test1;
declare
--proc_test2作用域开始
procedure proc_test2 is
begin
dbms_output.put_line('just for test 2');
end;
begin
proc_test2;
end; --proc_test2作用域结束
proc_test2; --这个已经不在作用域,调用肯定会报错!
end;
  • 打赏
  • 举报
回复
好像精通oracle 10g pl/sql编程里 过程那节 就有介绍这个的
minitoy 2010-12-07
  • 打赏
  • 举报
回复
You can nest blocks in the executable and exception-handling parts of a PL/SQL block or subprogram but not in the declarative part. You can define local subprograms in the declarative part of any block. You can call local subprograms only from the block in which they are defined.
minitoy 2010-12-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 gelyon 的回复:]
由于是匿名块 declare begin end 是成对出现
当然也有作用域的问题,不同的块是访问不到别的块的过程或者方法的
[/Quote]
是啊.
这个是正确的
declare
procedure proc_test1 is
begin
dbms_output.put_line('just for test 1');
end;
begin
proc_test1;
declare
procedure proc_test2 is
begin
dbms_output.put_line('just for test 2');
end;
begin
proc_test2;
proc_test1;
end;
end;


这个是错误的.
declare
procedure proc_test1 is
begin
dbms_output.put_line('just for test 1');
end;
begin
proc_test1;
declare
procedure proc_test2 is
begin
dbms_output.put_line('just for test 2');
end;
begin
proc_test2;
end;
proc_test2;
end;
gelyon 2010-12-07
  • 打赏
  • 举报
回复


SQL> declare
2 procedure proc_test1 is
3 begin
4 dbms_output.put_line('just for test 1');
5 end;
6 begin
7 proc_test1;
8 declare
9 function proc_test2 return VARCHAR2 is
10 begin
11 proc_test1;
12 return 'just for test 2';
13 end;
14 begin
15 dbms_output.put_line('test3: '||proc_test2);
16 end;
17 end;
18 /
just for test 1
just for test 1
test3: just for test 2

PL/SQL procedure successfully completed.

SQL>
gelyon 2010-12-07
  • 打赏
  • 举报
回复
由于是匿名块 declare begin end 是成对出现
当然也有作用域的问题,不同的块是访问不到别的块的过程或者方法的
gelyon 2010-12-07
  • 打赏
  • 举报
回复


Connected to:
Oracle Database 10g Release 10.1.0.2.0 - Production

SQL> set serveroutput on
SQL> declare
2 procedure proc_test1 is
3 begin
4 dbms_output.put_line('just for test 1');
5 end;
6 begin
7 proc_test1;
8 declare
9 procedure proc_test2 is
10 begin
11 dbms_output.put_line('just for test 2');
12 end;
13 begin
14 proc_test2;
15 end;
16 end;
17 /
just for test 1
just for test 2

PL/SQL procedure successfully completed.

SQL>
minitoy 2010-12-07
  • 打赏
  • 举报
回复
恩,以前没用过,这回算是进城了.[Quote=引用 2 楼 gelyon 的回复:]
没什么啊
在申明部分除了一般申请变量,类型、游标,还可以申明函数、过程等
[/Quote]
gelyon 2010-12-07
  • 打赏
  • 举报
回复
没什么啊
在申明部分除了一般申请变量,类型、游标,还可以申明函数、过程等
minitoy 2010-12-07
  • 打赏
  • 举报
回复
刚看了下PL/SQL User's Guide and Reference,是有相关资料的,以前没注意到

17,377

社区成员

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

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