今天在网上闲逛,发现一种没见过的匿名块书写方式.谁有这方面的资料?
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> 

解决方案 »

  1.   

    刚看了下PL/SQL User's Guide and Reference,是有相关资料的,以前没注意到
      

  2.   

    Connected to:
    Oracle Database 10g Release 10.1.0.2.0 - ProductionSQL> 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 2PL/SQL procedure successfully completed.SQL> 
      

  3.   

    由于是匿名块 declare begin end 是成对出现
    当然也有作用域的问题,不同的块是访问不到别的块的过程或者方法的
      

  4.   

    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 2PL/SQL procedure successfully completed.SQL> 
      

  5.   

    是啊.
    这个是正确的
    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;
      

  6.   

    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.
      

  7.   

    好像精通oracle 10g pl/sql编程里 过程那节 就有介绍这个的
      

  8.   

    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;
      

  9.   


    ---你过程声明都的放在匿名快的声明部分
    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 2PL/SQL 过程已成功完成。
      

  10.   

    根据文档整理了下思路.
    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.