-- plSQL语句:编程
-- sqlserver:declare @v_name varchar(20)
-- 显示服务器打印
set serveroutput on;
-- 定义变量
declare v_ename varchar2(20);-- String v_ename;
begin
-- 变量赋值,
v_ename := 'Smith';
-- 输出变量
dbms_output.put_line('名字是:'||v_ename);
end;
-- 执行符号
/
-- 定义两个变量,相加得到结果
-- 两个整数的
declare v_i number;
v_j number;
v_res number;
begin
v_i := 100;
v_j := 200;
v_res := v_i + v_j;
dbms_output.put_line('相加的结果是:'||v_res);
end;
/
-- 查询出用户名为1的人的名字,并打印出来
declare v_username varchar2(20);
begin
select username into v_username from userinfo where userid = 3;-- 赋值语句
dbms_output.put_line('名字是:'||v_username);
end;
/
-- PL/SQL错误处理方式
declare v_username varchar2(20);
begin
select username into v_username from userinfo where userid = 2;
dbms_output.put_line('名字是:'||v_username);
exception
when no_data_found then
dbms_output.put_line('对不起,没有这个员工号……');
end;
/
-- 参数变量
declare v_username varchar2(20);
v_no number;
begin
v_no := 2;
select username into v_username from userinfo where userid = v_no;
dbms_output.put_line('名字是:'||v_username);
exception
when no_data_found then
dbms_output.put_line('对不起,没有这个员工号……');
end;
/
-- 任意参数
declare v_username varchar2(20);
begin
select username into v_username from userinfo where userid = &no;
dbms_output.put_line('名字是:'||v_username);
exception
when no_data_found then
dbms_output.put_line('对不起,没有这个员工号……');
end;
/
-- plSQL 分支结构
-- 定义一个年份 ,是不是闰年
declare v_year number;
begin
v_year := 2000;
if v_year mod 400 = 0 then
dbms_output.put_line(v_year||'是闰年');
end if;
end;
/
declare v_year number;
begin
v_year := 2000;
if v_year mod 400 = 0 then
dbms_output.put_line(v_year||'是闰年');
else
dbms_output.put_line(v_year||'是闰年');
end if;
end;
/
declare v_socre number;
begin
v_socre := 98;
if v_socre >90 then
dbms_output.put_line(v_socre||'优秀');
elsif v_socre>80 then
dbms_output.put_line(v_socre||'好');
else
dbms_output.put_line(v_socre||'差');
end if;
end;
/
-- 判断userid为1的人的身高,如果小于120,将它修改为120,否则,每上人加5CM
declare v_height float;
begin
select height into v_height from userinfo where userid=1;
if v_height<120 then
update userinfo set height = 120 where userid = 1;
else
update userinfo set height = height + 5 where userid = 1;
end if;
end;
/
-- for,while循环 -- i in 1..10相当于for(int i = 0;i<=10,i++){}
begin
for i in 1..10 loop
dbms_output.put_line('循环'||i);
end loop;
end;
/
-- 变量
declare n number;
begin
select count(userid) into n from userinfo;-- 有几个人
for i in 1..n loop -- 为什么是10次
dbms_output.put_line('循环'||i);
end loop;
end;
/
-- 找出这张表里面所有人的名字和身高
declare n number;
v_username varchar2(20);
v_height float;
begin
select count(userid) into n from userinfo;-- 找出有几个人,知道循环几次
for i in 1..n loop
select username,height into v_username,v_height from userinfo where userid = i;
dbms_output.put_line('姓名:'||v_username||' 身高:'||v_height);
end loop;
end;
/
-- while循环
declare i number;
begin
i:=1;
while(i<5) loop
dbms_output.put_line('循环'||i);
i :=i + 1;
end loop;
end;
/
-- 定义表的列类型
-- 存储用户名
declare v_username userinfo.username%type; -- v_username类型就是uerinfo.username这个字段类型
begin
select username into v_username from userinfo where userid=1;
dbms_output.put_line(v_username);
end;
/
-- 定义表的行级类型
declare v_userinfo userinfo%rowtype;
begin
select userid,height,username,sex into v_userinfo.userid,v_userinfo.height,v_userinfo.username,v_userinfo.sex from userinfo where userid = 1;
dbms_output.put_line('姓名:'||v_userinfo.username);
end;
/
-- sqlserver:declare @v_name varchar(20)
-- 显示服务器打印
set serveroutput on;
-- 定义变量
declare v_ename varchar2(20);-- String v_ename;
begin
-- 变量赋值,
v_ename := 'Smith';
-- 输出变量
dbms_output.put_line('名字是:'||v_ename);
end;
-- 执行符号
/
-- 定义两个变量,相加得到结果
-- 两个整数的
declare v_i number;
v_j number;
v_res number;
begin
v_i := 100;
v_j := 200;
v_res := v_i + v_j;
dbms_output.put_line('相加的结果是:'||v_res);
end;
/
-- 查询出用户名为1的人的名字,并打印出来
declare v_username varchar2(20);
begin
select username into v_username from userinfo where userid = 3;-- 赋值语句
dbms_output.put_line('名字是:'||v_username);
end;
/
-- PL/SQL错误处理方式
declare v_username varchar2(20);
begin
select username into v_username from userinfo where userid = 2;
dbms_output.put_line('名字是:'||v_username);
exception
when no_data_found then
dbms_output.put_line('对不起,没有这个员工号……');
end;
/
-- 参数变量
declare v_username varchar2(20);
v_no number;
begin
v_no := 2;
select username into v_username from userinfo where userid = v_no;
dbms_output.put_line('名字是:'||v_username);
exception
when no_data_found then
dbms_output.put_line('对不起,没有这个员工号……');
end;
/
-- 任意参数
declare v_username varchar2(20);
begin
select username into v_username from userinfo where userid = &no;
dbms_output.put_line('名字是:'||v_username);
exception
when no_data_found then
dbms_output.put_line('对不起,没有这个员工号……');
end;
/
-- plSQL 分支结构
-- 定义一个年份 ,是不是闰年
declare v_year number;
begin
v_year := 2000;
if v_year mod 400 = 0 then
dbms_output.put_line(v_year||'是闰年');
end if;
end;
/
declare v_year number;
begin
v_year := 2000;
if v_year mod 400 = 0 then
dbms_output.put_line(v_year||'是闰年');
else
dbms_output.put_line(v_year||'是闰年');
end if;
end;
/
declare v_socre number;
begin
v_socre := 98;
if v_socre >90 then
dbms_output.put_line(v_socre||'优秀');
elsif v_socre>80 then
dbms_output.put_line(v_socre||'好');
else
dbms_output.put_line(v_socre||'差');
end if;
end;
/
-- 判断userid为1的人的身高,如果小于120,将它修改为120,否则,每上人加5CM
declare v_height float;
begin
select height into v_height from userinfo where userid=1;
if v_height<120 then
update userinfo set height = 120 where userid = 1;
else
update userinfo set height = height + 5 where userid = 1;
end if;
end;
/
-- for,while循环 -- i in 1..10相当于for(int i = 0;i<=10,i++){}
begin
for i in 1..10 loop
dbms_output.put_line('循环'||i);
end loop;
end;
/
-- 变量
declare n number;
begin
select count(userid) into n from userinfo;-- 有几个人
for i in 1..n loop -- 为什么是10次
dbms_output.put_line('循环'||i);
end loop;
end;
/
-- 找出这张表里面所有人的名字和身高
declare n number;
v_username varchar2(20);
v_height float;
begin
select count(userid) into n from userinfo;-- 找出有几个人,知道循环几次
for i in 1..n loop
select username,height into v_username,v_height from userinfo where userid = i;
dbms_output.put_line('姓名:'||v_username||' 身高:'||v_height);
end loop;
end;
/
-- while循环
declare i number;
begin
i:=1;
while(i<5) loop
dbms_output.put_line('循环'||i);
i :=i + 1;
end loop;
end;
/
-- 定义表的列类型
-- 存储用户名
declare v_username userinfo.username%type; -- v_username类型就是uerinfo.username这个字段类型
begin
select username into v_username from userinfo where userid=1;
dbms_output.put_line(v_username);
end;
/
-- 定义表的行级类型
declare v_userinfo userinfo%rowtype;
begin
select userid,height,username,sex into v_userinfo.userid,v_userinfo.height,v_userinfo.username,v_userinfo.sex from userinfo where userid = 1;
dbms_output.put_line('姓名:'||v_userinfo.username);
end;
/