PL / SQL变量

Read previous tutorial: PL/SQL Data Types 

In the last tutorial, we learnt few things about variables in PL/SQL. Now, we shall look at the constraint definitions and other related operations.

PL/SQL Variables

Constraints are associated with the variables defined in the code block. A constraint is a condition that is placed on the variable.

Two frequently used constraints in PL/SQL are:


Constant – This constraint will cause Oracle Engine to ensure the value is not changed after a value is initially assigned to a variable. If a statement tries to change the variable value, an error will be displayed.

Not Null – This constraint will cause Oracle Engine to ensure that the variable always contains a value. If the statement attempts to assign an empty or a null value to that particular variable, the program will be error prone and will get abnormal termination of the program or the exception section will execute, if included in the program code.

Example

PI constant number(9,8) := 3.14159265;
dob not null date := '12-DEC-94';

How to Assign Values to Variables

A PL/SQL procedure is of no use if there isn’t any method to associate values with the variables. But, PL/SQL provides us with the following ways to achieve this.

:=
This sign includes a ‘colon’ with a succeeding ‘equal to’ sign. This particular sign assigns the parameter on the right hand side of the sign to the parameter or the variable on the left hand side of the sign.

Into
The ‘Into’ keyword is used in a ‘Fetch’ or a ‘Select’ statement. For a Select statement, it assigns the value in the Select section to the variables following the ‘Into’ keyword. When used with the Fetch statement, it assigns the cursor values to the variables that follow the ‘Into’ keyword.

Lets see a program now.


Output
Value of Var3: 90 
Value of Var4: 23.333333333333333333 
PL/SQL procedure successfully completed.

Here, we have declared and defined the 4 variables in the declaration block. The declaration block just holds the variable to be declared in the the current program code. The execution and calculation is done in the BEGIN block. We can access the variables declared in the DECLARE block here.

Scope of Variables

Scope defines the visibility of a particular variable in a program code. PL/SQL gives us the option to develop nesting of blocks which means that every block can contain another block within itself. However, the outer block won’t be accessible.

There are two types of variable scope.

1. Local Variables
2. Global Variables

The next example will help you understand the terms local variables and global variables clearly.

DECLARE 
-- Global variables 
num1 number := 100; 
num2 number := 200; 
BEGIN 
dbms_output.put_line('Outer Block Variable num1: ' || num1); 
dbms_output.put_line('Outer Block Variable num2: ' || num2); 
DECLARE 
-- Local variables 
num1 number := 300; 
num2 number := 400; 
BEGIN 
dbms_output.put_line('Inner Block Variable num1: ' || num1); 
dbms_output.put_line('Inner Block Variable num2: ' || num2); 
END; 
END; 
/


Output
Outer Block Variable num1: 100 
Outer Block Variable num2: 200
Inner Block Variable num1: 300 
Inner Block Variable num2: 400 
PL/SQL procedure successfully completed.

This program properly shows that a BEGIN block can contain other blocks as well and can get the correct output. Here we have inner blocks and outer blocks which have limited scope according to their visibility.

翻译自: https://www.thecrazyprogrammer.com/2015/05/plsql-variables.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值