Oracle使用手册(一)---声明变量
/**//*
--建表
create table student(
recordid number(38),
sid int not null ,
sname varchar2(50),
sdate date,
sage number(3)
);
*/
--删除表
--drop table student;
--插入数据
/**//*
set serveroutput on --允许服务器输出
declare
maxrecords constant int:=100;
i int:=1;
begin
for i in 1..maxrecords
loop
insert into student(sid,sdate)values(i,sysdate);
end loop
--dbms_output.put_line('成功录入数据!');
commit;
end;
*/
--select * from student;
--声明一个变量
/**//*
declare
pi constant number(9):=3.1415926;
begin
commit;
end;
*/
--复合数据类型(常见的五种)
--1 .使用 %type 定义变量
--为了让PL/SQL中变量的类型和数据表中的字段的数据类型一致,Oracle 9i提供了%type定义方法。
--这样当数据表的字段类型修改后,PL/SQL程序中相应变量的类型也自动修改.
/**//*
Declare
mydate student.sdate%type;
begin
commit;
end;
*/
--2. 定义记录类型变量
--将多个基本数据类型捆绑在一起的记录数据类型。
/**//*
set serveroutput on
declare
type myrecord is record(
sid int,
sdate date);
srecord myrecord; --声明一个自定义记录类型变量的实例
begin
select sid,sdate into srecord from student where sid=68;
dbms_output.put_line('ID: '|| srecord.sid ||'Date:'|| srecord.sdate); --'||': 它是字符串连接符.
end;
*/
--3.使用 %rowtype 变量
--使用%type可以使变量获得字段的数据类型,使用%rowtype可以使变量获得整个记录的数据类型。
--比较两者定义的不同:变量名 数据表.列名%type,变量名 数据表%rowtype。
/**//*
set serveroutput on
Declare
mytableRow student%rowtype;
begin
select * into mytableRow
from student
where sid=88;
dbms_output.put_line(mytableRow.sid || mytableRow.sdate);
end;
*/
--4.定义一维表类型变量
--表类型变量和数据表是有区别的,定义表类型变量的语法如下:
-- ―――――――――――――――――――――――――――――――――――――
-- type 表类型 is table of 类型 index by binary_integer;
-- 表变量名 表类型;
-- ―――――――――――――――――――――――――――――――――――――
-- 类型可以是前面的类型定义,index by binary_integer子句代表以符号整数为索引,
-- 这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”。
/**//*
Declare
type tabletype1 is table of varchar2(4) index by binary_integer; --定义一个字符型的一维数组
type tabletype2 is table of student.sid%type index by binary_integer;--定义了一个整数数型的数组
table1 tabletype1; --实例声明
table2 tabletype2; --实例声明
begin
table1(1):='学生';
table1(2):='职员';
table2(1):=88;
table2(2):=89;
dbms_output.put_line(table1(1)||table2(1));
dbms_output.put_line(table1(2)||table2(2));
end;
*/
--5.定义多维类型表变量
--相当于定义多维数组.
--注意在运行下面的语句前要在数据库中插入数据.
Declare
type tabletype1 is table of student%rowtype index by binary_integer;
table1 tabletype1;
begin
select * into table1(60)
from student
where sid=60;
dbms_output.put_line(table1(60).sid ||table1(60).sdate);
end;
一、概述:
1、PLSQL 块结构:
DECLARE --- 可选
变量声明定义
BEGIN ---- 必选
SQL 和PLSQL 语句
EXCEPTION ---- 可选
错误处理
END;---- 必选
二、实例:
declare
vjob varchar(9);
v_count number:=0;
vtotal date:=sysdate +7;
c_tax constant number(3,2):=8.25;
v_valid boolean not null:=true;
begin
select sysdate into vtotal from dual;
end;
/
上例中,如果没有这个SELECT语句,会如何?
出错,说明必须有STATEMENTS
如果: select sysdate from dual into vtotal ;
同样,也不行。而且变量与赋值的类型要匹配。
-- "查询结果" into "变量" 是将查询出来的结果赋给 后面的变量
-- 另一种赋值方式:
/* 例:
Declare
emp_id char(4);
emp_name varchar2(20);
begin
emp_id:='1234';
emp_name:='Trybird';
end;
*/
三、%TYPE的属性
声明一个变量使之与数据库某个列的定义相同或与另一个已经定义过的变量相同
所以%TYPE要作为列名的后缀:如:
v_last_name s_emp.last_name%TYPE;
v_first_name s_emp.first_name%TYPE; --这样做的好处是我们不必去知晓此列的类型与定义
或:v_balance NUMBER(7,2);
v_minimum_balance v_balance%TYPE := 10;
四、声明一个布尔类型的变量
1 只有TRUE、FALSE、NULL可以赋值给BOOLEAN变量
2 此变量可以接逻辑运算符NOT、AND、OR。
3、变量只能产生TRUE、FALSE、NULL。
实例:
VSAL1:=50000;
VSQL2:=60000;
VCOMMSAL BOOLEAN:=(VSAL1<VSQL2);
--其实是把TRUE赋值给此变量。
五、LOB 类型的变量
共有CLOB、BLOB、BFILE、NCLOB几种,这里不做为重点。
六:使用HOST VARIABLES
SQL> variable n number
SQL> print n
:n=v_sal /12;
:n这个加了:前缀的变量不是PLSQL变量,而是HOST。
Oracle使用手册(一)---声明变量
原创
©著作权归作者所有:来自51CTO博客作者mb646713a07a071的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:Log4j日志管理
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
合宙4G模组Air780E开发板使用手册
合宙Air780E开发板使用说明
开发板 物联网 嵌入式 硬件开发 硬件设计 -
Oracle BBED 简单使用手册
每天一个 DBA 小知识,助你更进一步!
oracle bbed 拷贝文件 技术交流 数据文件 -
zlog使用手册
zlog使用手册 1 zlog是什么? 1.1 兼容性说明 1.2 zlog 1.2 发布说明 Chapter 2 zlog不是什么? Chapter
配置文件 自定义 日志文件 #include 字段