PL/SQL 学习笔记3

本文介绍了PL/SQL中变量的初始化,包括如何在SQL*Plus中使用`var`命令和`begin-end`块进行赋值。讨论了%type属性的应用,特别是当与数据库列结合时,NOT NULL约束的限制。还探讨了绑定变量的概念,如何在PL/SQL中使用它们,以及如何通过`exec`命令和块结构进行赋值。文章通过示例展示了`set verify off|on`对替代变量输出的影响,并解释了`define`命令声明的用户变量的行为。此外,文章讲解了嵌套块的可见性和作用域规则,并提供了一个实践示例来演示如何在PL/SQL块中使用iSQL*Plus的子代变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

%type 类型应用的话 如果前面用的是database column的话 那么不可以加not null,前面引用的是声明的变量可以应用not null

 

A NOT NULLdatabase column constraint does not apply to variables that are declared using %TYPE.  
Therefore, if you declare a variable using the %TYPEattribute that uses a database column defined as NOT  
NULL, you can assign the NULLvalue to the variable.  

eg:

 

 

绑定变量

A bind variable is a variable that you declare in a host environment.

 Bind variables can be used to pass run-time values, either number or character, into or out of one or more PL/SQL programs. The PL/SQL  
programs use bind variables as they would use any other variable. You can reference variables declared in the  
host or calling environment in PL/SQL statements, unless the statement is in a procedure, function, or  
package. This includes host language variables declared in precompiler programs, screen fields in Oracle   Developer Forms applications, and iSQL*Plus bind variables.

 

绑定变量可以在主机环境下声明 可以在sqlplus 这种应用程序下声明 然后pl/sql程序来调用 调用需要在该变量前加colon

赋值的时候可以使用 exec :var_name := 10000; or begin :var_name := 1000 end; 或者是在PL/SQL中赋值

print(sqlplus command)可以打印出这个值

   

 

 

注意 set verify off|on 的命令是对 替代变量造成输出显示的新旧值 的控制 on 是显示

 off 是关闭

 

define命令声明的用户变量会把你初始化的值转化为char类型 50000转化为 5,0,0,0,0 5个char类型的字符


 

 

 

 

nested block

 

An identifier is visible in the block in which it is declared and in all nested subblocks, procedures, and  
functions. If the block does not find the identifier declared locally,

it looks up to the declarative section of  the enclosing (or parent) blocks.

The block never looks down to enclosed (or child) blocks or sideways to sibling blocks.  

 

 

 

practise:

 

Create and execute a PL/SQL block that accepts two numbers through iSQL*Plus substitution
variables. Use the DEFINE command to provide the two values. Pass these two values to the PL/SQL
block through iSQL*Plus substitution variables. The first number should be divided by the second
number and have the second number added to the result. The result should be stored in a PL/SQL
variable and printed on the screen.
Note: SET VERIFY OFF in the PL/SQL block.
DEFINE p_num1 = 2
DEFINE p_num2 = 4

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值