=========================Oracle数据库简单语句============
--%TYPE类型
--SQL>
declare
var_name varchar2(100);--ZM_TREE.Fname%type;
var_type varchar2(100);--ZM_TREE.Flinktype%type;
begin
SELECT t.fname,t.flinktype
into var_name,var_type
from zm_tree t
where flinktype='公司';
--单条记录输出
dbms_output.put_line(var_name||' 类型为: '||var_type);
end;
--record类型
--SQL>
declare
type emp_type is record
(
var_name varchar2(20),
var_type varchar2(20),
var_sort varchar2(200)
);
empinfo emp_type;
begin
--查询的顺序和record的定义的顺序是一样的
select t.fname,t.flinktype,t.fsort into empinfo from ZM_TREE t where t.flinktype='公司';
--单条记录输出
dbms_output.put_line('名称:'||empinfo.var_name||'——类型:'||empinfo.var_type||'——排序:'||empinfo.var_sort);
end;
--%rowtype类型
--SQL>
declare
rowVar_emp ZM_TREE%rowtype;
begin
SELECT * into rowVar_emp FROM ZM_TREE t where t.flinktype='公司';
/*输出信息*/--单条记录输出
dbms_output.put_line('名称:'||rowVar_emp.fname||'——类型:'||rowVar_emp.FLINKTYPE||'——排序:'||rowVar_emp.Fsort);
end;
--变量 常量
var_str varchar2(30):='中国';
var_num constant number(8,4):=20;
--secondday
--》》》流程控制
--if
--SQL>
declare
var_str1 varchar2(200);
var_str2 varchar2(300);
begin
var_str1:='121321';
var_str2:='21321321';
if length(var_str1)>length(var_str2) then
dbms_output.put_line(var_str1);
else
dbms_output.put_line(var_str2);
end if;
end;
--if elseif
--SQL>
declare
num_age int :=&请输入年龄;
begin
if num_age>=56 then
dbms_output.put_line('您可以申请退休了');
elsif num_age<56 then
dbms_output.put_line('您小于56岁,不可以申请退休!');
else
dbms_output.put_line('对不起,年龄不合法!');
end if;
end;
--SQL>
declare
num_age int :=&请输入年龄;
aboutinfo varchar2(50);
begin
if num_age>=56 then
aboutinfo:=num_age||'您可以申请退休了';
elsif num_age<56 then
aboutinfo:=num_age||'您小于56岁,不可以申请退休!';
else
aboutinfo:=num_age||'对不起,年龄不合法!';
end if;
dbms_output.put_line(aboutinfo);
end;
--Case
declare
season int :=&请输入季度数字;
aboutinfo varchar(50);
begin
case season
when 1 then
aboutinfo:=season||'季度包含1,2,3月度';
when 2 then
aboutinfo:=season||'季度包含4,5,6月度';
when 3 then
aboutinfo:=season||'季度包含7,8,9月度';
when 4 then
aboutinfo:=season||'季度包含10,11,12月度';
else
aboutinfo:='输入的值不合法';
end case;
dbms_output.put_line(aboutinfo);
end;
--》》》循环语句
--loop语句 一直运行到exit when end_condition_exp 为true时退出
declare
sum_i int:=0;
i int:=0;
begin
loop
i:=i+1;
sum_i :=sum_i + i;
exit when i = 100;
end loop;
dbms_output.put_line('前100个自然数的和是:'||sum_i);
end;
--while循环
declare
sum_i int:=0;
var_i int:=0;
begin
while var_i<100 loop
var_i:=var_i+1;
sum_i:=sum_i+var_i;
end loop;
dbms_output.put_line('前100个自然数的和是:'||sum_i);
end;
--for 语句
declare
sum_i int:=0;
begin
for i in reverse 1..100 loop -- reverse 表示i从100递减
sum_i:= sum_i+i;
end loop;
dbms_output.put_line('前100个自然数的和是:'||sum_i);
end;
--》》》游标
/*
游标属性:
cur_tmp%found 至少影响到一行数据为true;
cur_tmp%notfound 与%found相反
cur_tmp%rowcount 返回受SQL语句影响的行数
cur_tmp%isopen 游标打开时为true
*/
declare
cursor cur_emp(var_ID in varchar2:='lili')
is select t.flinkcode,t.fname,t.flinktype
from ZM_TREE t
where flinkcode like var_ID||'%';
type record_emp is record
(
var_name ZM_TREE.FLINKCODE%type,
var_type ZM_TREE.Fname%type,
var_sort varchar2(200)
);
emp_row record_emp;
begin
DBMS_OUTPUT.ENABLE(buffer_size => null);--表示输出buffer不受限制
open cur_emp('2');--得到游标值
fetch cur_emp into emp_row;--循环游标
while cur_emp%found loop
dbms_output.put_line('名称:'||emp_row.var_name||'——类型:'||emp_row.var_type||'——排序:'||emp_row.var_sort);
fetch cur_emp into emp_row;
end loop;
close cur_emp;--关闭游标
end;
--for 中使用cursor 不用进行打开游标、读取游标、关闭游标 oracle内部自动完成
declare
var_ID varchar2(200):=&输入编码;
cursor cur_emp is
select t.flinkcode var_name,t.fname,t.flinktype from ZM_TREE t where flinkcode like var_ID||'%';
begin
DBMS_OUTPUT.ENABLE(buffer_size => null); --表示输出buffer不受限制
for emp_info in cur_emp loop
--显示的名称和列别名称一样
dbms_output.put_line('名称:'||emp_info.var_name||'——类型:'||emp_info.fname||'——排序:'||emp_info.flinktype);
end loop;
end;
begin
DBMS_OUTPUT.ENABLE(buffer_size => null); --表示输出buffer不受限制
for emp_info in (select t.flinkcode var_name,t.fname,t.flinktype from ZM_TREE t where flinkcode like '2%') loop
dbms_output.put_line('名称:'||emp_info.var_name||'——类型:'||emp_info.fname||'——排序:'||emp_info.flinktype);
end loop;
end;
--》》》异常处理
/*
预定义异常
自定义异常
*/
--预定义异常
declare
var_empno t_cscustomer.cust_no%type;
var_empname t_cscustomer.cust_name%type;
begin
select cust_no,cust_name into var_empno,var_empname from t_cscustomer where cust_no like '00%';
if sql%found then
dbms_output.put_line('雇员编号:'||var_empno||'、名称:'||var_empname);
end if;
exception
when too_many_rows then
dbms_output.put_line('返回记录超过一行');
when no_data_found then
dbms_output.put_line('无数据记录');
end;
/*drop table dept_tmp;
create table dept_tmp(
DEPT_NO VARCHAR2(12) primary key not null,
DEPT_NAME VARCHAR2(50),
LOCATION VARCHAR2(200)
);
*/
create or replace procedure pro_insertTmp is
begin
insert into dept_tmp values(1,'市场拓展部','join');
commit;
dbms_output.put_line('插入dept_tmp新记录成功');
end pro_insertTmp;
--执行pro_insertTmp
--execurte pro_insertTmp;
exec pro_insertTmp;
begin
pro_inserttmp;
end;
/**存储过程参数过程包括:in 输入参数、out 输出参数、in out可被修改的输入参数,并作为输出参数**/
-->>in
create or replace procedure pro_insertDept(v_deptno in varchar2,v_deptname in varchar2,v_loc in varchar2) is
begin
insert into dept_tmp values(v_deptno,v_deptname,v_loc);
commit;
dbms_output.put_line('通过in参数插入dept成功!');
end pro_insertDept;
--不按顺序传入参数,指定参数值
declare
i int:=1;
begin
while i<20 loop
-- pro_insertDept(v_deptname=>'采购部',v_loc=>'成都',v_deptno=>'2');
pro_insertDept(v_deptno => i,v_deptname => '采购部'||i,v_loc => '成都'||i);
i:=i+2;
end loop;
end;
-->>out
create or replace procedure pro_selectDept(v_deptno in varchar2,v_deptname out dept_tmp.dept_name%type,v_loc out dept_tmp.location%type) is
begin
select dept_name,location into v_deptname,v_loc from dept_tmp where dept_no=v_deptno;
exception
when no_data_found then
dbms_output.put_line('该编号的部门不存在!');
end pro_selectDept;
declare
var_deptname dept_tmp.dept_name%type;
var_loc dept_tmp.location%type;
var_deptno dept_tmp.dept_no%type:=&21;
begin
pro_selectDept(var_deptno,v_deptname => var_deptname,v_loc => var_loc);
dbms_output.put_line(var_deptname||'位于:'||var_loc);
end;
variable v_deptname varchar2(50);
variable v_loc varchar2(50);
exec pro_selectDept('1',:v_deptname,:v_loc);
print v_deptname v_loc;
select :v_deptname,:v_loc from dual;
create or replace procedure pro_square(num in out number,flag in boolean) is
i int:=2;
begin
if flag then
num := power(num,i); --计算平方
else
num := sqrt(num); --计算平方根
end if;
end pro_square;