《Oracle PL/SQL开发指南》学习笔记28——源码调试——PL/SQL基础知识(第一部分)

本文介绍了PL/SQL编程的基础,包括最简单的程序结构、执行脚本文件、使用注释、处理变量及会话变量的方法。通过具体示例,如Hello World程序,展示了如何在Oracle数据库环境中运行PL/SQL代码。

纸上得来终觉浅,绝知此事要躬行。计算机编程是工程,必须多动手实践!

1. 最简单的PL/SQL程序(Basic Block Structure)

SQL> BEGIN
  2  NULL;
  3  END;
  4  /

PL/SQL procedure successfully completed.

2. 还能再简单吗?不行!比如,去掉null语句,会怎么样呢?

SQL> BEGIN
  2
  3  END;
  4  /
END;
*
ERROR at line 3:
ORA-06550: line 3, column 1:
PLS-00103: Encountered the symbol "END" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge

3. 执行脚本文件

1)将以下语句保存为Hello_world.sql文件

SET	SERVEROUTPUT	ON	SIZE	UNLIMITED
BEGIN				
dbms_output.put_line('Hello	World.');			
END;				
/				

 2)执行该文件

SQL> @'D:\2.学无止境\DWBI\Oracle12cPLSQLCode\Chapter3\hello_world.sql'
Hello   World.

PL/SQL procedure successfully completed.

4. PL/SQL中可输入单行或多行注释

-- This is a single-line comment.

/* This is a multiple-line comment.
Style and indentation should follow your company standards. */

5. 替换变量默认为数字,如果输入字符,需使用单引号,否则报错。

SQL> BEGIN
  2  dbms_output.put_line('['||&input||']');
  3  END;
  4  /
Enter value for input: 123
old   2: dbms_output.put_line('['||&input||']');
new   2: dbms_output.put_line('['||123||']');
[123]

PL/SQL procedure successfully completed.

SQL> /
Enter value for input: abc
old   2: dbms_output.put_line('['||&input||']');
new   2: dbms_output.put_line('['||abc||']');
dbms_output.put_line('['||abc||']');
                          *
ERROR at line 2:
ORA-06550: line 2, column 27:
PLS-00201: identifier 'ABC' must be declared
ORA-06550: line 2, column 1:
PL/SQL: Statement ignored

6. Oracle支持使用会话变量(绑定变量),它们与匿名PL/SQL块中的替换变量相似。它们的不同之处在于:会话变量在数据库连接或数据库会话的上下文中有内存范围(占用内存空间)。

(Oracle also lets you use session (or bind) variables, which are similar to substitution variables in anonymous PL/SQL blocks. Session variables differ from substitution variables because they have a memory scope in the context of any connection or database session.)

SQL> VARIABLE bind_variable VARCHAR2(20)
SQL> BEGIN
  2  :bind_variable:='HelloPL/SQL world!';
  3  dbms_output.put_line('['||:bind_variable||']');
  4  END;
  5  /
[HelloPL/SQL world!]

PL/SQL procedure successfully completed.

SQL> col :bind_variable for a20
SQL> SELECT :bind_variable FROM dual;

:BIND_VARIABLE
--------------------
HelloPL/SQL world!

注意:在SQL中使用绑定变量时,其名称前要加冒号。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值