例子2: SQL> declare 2 v_name varchar2(20); 3 begin 4 v_name:=‘myname‘; 5 dbms_output.put_line(v_name); 6 end; 7 / myname 例子3: SQL> declare 2 v_num number:=0; 3 begin 4 v_num:=2/v_num; 5 dbms_output.put_line(v_num); 6 end; 7 / declare * ERROR 位于第 1 行: ORA-01476: 除数为 0 ORA-06512: 在line 4 例子4: 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.每一行只能声明一个变量 常用变量类型 1. binary_interger,整数,主要用来计数,而不是用来表示字段类型
2. number 数字类型
3. char 定长字符串
4. varchar2 变长字符串 5. date 日期 6.long 长字符串,最长2GB 7.boolean 布尔类型,可以取true false 和null的值 例5: 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; 用--可以注释一行 例6: declare v_empno number(4);
v_empno2 emp.empno%type;
v_empno3 v_empno2%type;
begin dbms_output.put_line(‘test‘);
end;
例7 table变量类型 set serveroutput on; declare type type_table_emp_empno is table of emp.empno%type index by binary_integer;
v_empnos type_table_emp_empno;
begin v_empnos(0):=7369;
v_empnos(2):=7869; v_empnos(-1):=9999; dbms_output.put_line(v_empnos(-1));
end;
例8 Record 变量类型 set serveroutput on; declare type type_record_dept is record ( deptno dept.deptno%type, dname dept.dname%type, loc dept.loc%type ); v_temp type_record_dept;
begin v_temp.deptno:=50;
v_temp.loc:=‘aaaa‘; v_temp.loc:=‘bj‘; dbms_output.put_line(v_temp.deptno||‘ ‘||v_temp.dname);
end;
例9: 使用%rowtype声明record变量(表结构的变化同时也能代理储存过程的变化)
set serveroutput on;
declare v_temp dept%rowtype;
begin
v_temp.deptno:=50;
v_temp.loc:=‘aaaa‘;
v_temp.loc:=‘bj‘;
dbms_output.put_line(v_temp.deptno||‘ ‘||v_temp.dname);
end;
例10; declare v_name emp.ename%type;
v_sal emp.sal%type;
begin
select ename,sal into v_name,v_sal from emp where empno=7369;(将ename和sal的值放在v_name和v_sal里面)
end;
例11: declare v_name emp.ename%type;
v_sal emp.sal%type; begin select ename,sal into v_name,v_sal from emp where empno=7369; dbms_output.put_line(v_name||‘ ‘||v_sal);
end; dbms_output.put_line(v_name||‘ ‘||v_sal);
end;
例12: 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;
例13: declare v_deptno emp2.deptno%type:=50;
v_count number; begin update emp2 set sal=sal/2 where deptno=v_deptno; dbms_output.put_line(sql%rowcount ||‘条记录被影响‘);
(sql为关键字,代表上一条语句 commit;
end;
/ 例14: declare v_deptno emp2.deptno%type:=50; v_count number; begin --update emp2 set sal=sal/2 where deptno=v_deptno; select deptno into v_deptno from emp2 where empno=7369; dbms_output.put_line(sql%rowcount ||‘条记录被影响‘);(sql为关键字,代表上一条语句 commit;
end;
/ 例15 declare v_deptno emp2.deptno%type:=50;
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;
(select必须和into一起使用) dbms_output.put_line(sql%rowcount ||‘条记录被影响‘);
commit; end; /
PL/SQL里面执行DDL语句 begin execute immediate ‘create table T(nnn varchar2(20) default ‘‘aaa‘‘)‘;
例11: declare v_name emp.ename%type; v_sal emp.sal%type; begin select ename,sal into v_name,v_sal from emp where empno=7369; dbms_output.put_line(v_name||‘ ‘||v_sal);
end;
dbms_output.put_line(v_name||‘ ‘||v_sal);
end;
例12:
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;
例13: declare v_deptno emp2.deptno%type:=50; v_count number; begin update emp2 set sal=sal/2 where deptno=v_deptno; dbms_output.put_line(sql%rowcount ||‘条记录被影响‘);
(sql为关键字,代表上一条语句 commit;
end;
/ 例14: declare v_deptno emp2.deptno%type:=50;
v_count number; begin --update emp2 set sal=sal/2 where deptno=v_deptno; select deptno into v_deptno from emp2 where empno=7369; dbms_output.put_line(sql%rowcount ||‘条记录被影响‘);
(sql为关键字,代表上一条语句 commit;
end;
/ 例15 declare v_deptno emp2.deptno%type:=50;
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;
(select必须和into一起使用) dbms_output.put_line(sql%rowcount ||‘条记录被影响‘);
commit; end; /
PL/SQL里面执行DDL语句 begin execute immediate ‘create table T(nnn varchar2(20) default ‘‘aaa‘‘)‘;
例11: declare v_name emp.ename%type; v_sal emp.sal%type; begin select ename,sal into v_name,v_sal from emp where empno=7369; dbms_output.put_line(v_name||‘ ‘||v_sal);
end; dbms_output.put_line(v_name||‘ ‘||v_sal);
end;
例12: 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;
例13: declare v_deptno emp2.deptno%type:=50; v_count number; begin update emp2 set sal=sal/2 where deptno=v_deptno; dbms_output.put_line(sql%rowcount ||‘条记录被影响‘);
(sql为关键字,代表上一条语句 commit;
end;
/ 例14: declare v_deptno emp2.deptno%type:=50;
v_count number; begin --update emp2 set sal=sal/2 where deptno=v_deptno; select deptno into v_deptno from emp2 where empno=7369; dbms_output.put_line(sql%rowcount ||‘条记录被影响‘);
(sql为关键字,代表上一条语句 commit;
end;
/ 例15 declare v_deptno emp2.deptno%type:=50;
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;
(select必须和into一起使用) dbms_output.put_line(sql%rowcount ||‘条记录被影响‘);
commit; end; /
原文:http://www.cnblogs.com/ruishuang208/p/4095397.html