Home > Database > Mysql Tutorial > PL/SQL编程中变量的声明

PL/SQL编程中变量的声明

WBOY
Release: 2016-06-07 17:29:38
Original
1319 people have browsed it

PL/SQL编程中变量的声明 创建第一个程序hello world begin dbms_output.put_line(

PL/SQL编程中变量的声明

创建第一个程序hello world

begin

dbms_output.put_line('Hello world');

end;

/

 

set serveroutput on;(输出语句到dos窗口的命令)

 

begin

dbms_output.put_line('Hello world');

end;

/

就可以在dos窗口中看到Hello world;

 

(简单变量的声明)

声明变量:

declare

v_name varchar2(20);(变量名在前,类型在后)

begin

v_name := 'myname';(给变量进行赋值)

dbms_output.put_line(v_name);

end;

/

(异常处理)

declare

v_num number := 0;

begin

v_num := 2/v_num;

dbms_output.put_line(v_num);

exception

when others then

dbms_output.put_line('error');

end;

变量声明的规则:

1 变量名不能使用保留字:(from,select)

2 第一个字符必须是字母

3 变量名最多包含30个字符

4 不要与数据库的表或者列同名

5 每一行只能声明一个变量

 

常用类型变量:

binary_integer :整数,主要用来计数而不是用来表示字段类型,(提高效率)

number;数字类型

char:定长字符串;

date:日期;

long:长字符串,最长2G;

常量的练习;

declare

v_temp number(1);

v_count binary_integer :=0;

v_sal number(7,2) :=4000.00;

v_date date := sysdate;

v_pi constant number(3,2) := 3.14;

v_valid boolean :=false;

v_name varchar2(20) not null :='MyName';

begin

dbms_output.put_line('v_temp value' || v_temp);(||连接符)

end;

/

注释一句用'--' 注释一段/* */

dbms_output.put_line()不能打印boolean 变量

 

使用%type属性:

declare (变量声明类型依据 表的类型变化)

v_empno number(4);

v_empno2emp.empno%type;

v_empno3v_empno2%type;

begin

dbms_output.put_line('Test');

end;

 

复合变量的声明:

 

第一种:

Table变量类型(相当于java的数组)

declare

typetype_table_emp_empnois table of emp.empno%type index by binary_integer;

v_empnostype_table_emp_empno;

begin

v_empnos(0) := 7369;

v_empnos(2) := 7839;

v_empnos(-1) := 9999;--(下标可以是小于0的)(注释)

dbms_output.put_line(v_empnos(-1));

end;

 

Record变量类型:(相当于java的类)

declare

typetype_record_dept isrecord

(

deptno dept.deptno%type,

dname dept.dname%type,

loc dept.loc%type

);

v_temp type_record_dept;

begin

v_temp.deptno :=50;(相当于引用去访问成员变量)

v_temp.dname :='aaaa';

v_temp.loc := 'bj';

dbms_output.put_line(v_temp.deptno || ' '|| v_temp.dname);

end;

 

使用%rowtype声明record 变量;

declare

v_tempdept%rowtype;

begin

v_temp.deptno := 50;

v_temp.dname := 'aaaa';

v_temp.loc := 'bj';

dbms_output.put_line(v_temp.deptno || ' ' || v_temp.dname);

end;

 

SQL(select)语句在PlSQL中的应用:

declare

v_ename emp.ename%type;

v_sal emp.sal%type;

begin --plsql语句中select语句中必须有into ,必须保证返回记录

dbms_output.put_line(v_ename || ' ' || v_sal);

end;

 

declare

v_emp emp%rowtype;

begin

select *into v_emp from emp where empno = 7369;

dbms_output.put_line(v_emp.empno);

end;

 

其他SQL 语句在plsql中的应用:

declare

v_deptno dept.deptno%type := 50;

v_dname dept.dname%type :='aaaa';

v_loc dept.loc%type := 'bj';

begin --插入数值;

insert into dept2 values(v_deptno,v_dname,v_loc);

commit;

end;

 

 

declare

v_deptnoemp2.deptno%type:= 10;

v_count number;

begin

update emp2 set sal=sal/2 where deptno =v_deptno;

--select deptno into v_deptno from emp2 where empno = 7369;

-- select count(*) into v_count from emp2;

-- sql表示刚刚执行完的那条语句,(执行完这条语句后,,有多少记录被 影响)

dbms_output.put_line(sql%rowcount || '条记录被影响');

commit;

end;

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template