不管数据类型,直接用数据表的数据类型?
declare
v_id stu.sid%type;
v_name stu.sname%type;
begin
v_id:='&请输入编号';
select sname into v_name from stu where sid=v_id;
dbms_output.put_line('取出的姓名:'||v_name);
end;
行的数据类型 %rowtype
declare
v_id stu.sid%type;
v_temp stu%rowtype;
begin
v_id:='&请输入编号';
select * into v_temp from stu where sid=v_id;
dbms_output.put_line('编号:'||v_temp.sid||' 姓名:'||v_temp.sname||' 年龄:'||v_temp.sage);
end;
declare
v_sid student.sid%type;
v_date student.sdate%type;
begin
v_sid := '&请输入编号:';
select sdate into v_date from student where sid=v_sid;
--dbms_output.put_line('日期:'||v_date);
dbms_output.put_line('日期:'||to_char(v_date,'yyyy"年"mm"月"dd"日" hh24:mi:ss dy'));
end;
-- 行类型 带出一个新的问题:如果取出多行?用游标 cursor解决多行问题!
declare
v_gid stu.gid%type;
. v_temp stu%rowtype;
begin
v_gid:='&请输入编号';
select * into v_temp from stu where gid=v_gid;
dbms_output.put_line('编号:'||v_temp.sid||' 姓名:'||v_temp.sname||' 年龄:'||v_temp.sage);
end;
declare
--声明一个类型
type r_stu is record
(
v_id stu.sid%type,
v_temp stu%rowtype
);
v_t r_stu;--v_t:变量 r_stu:数据类型
begin
v_t.v_id:='&请输入编号';
select * into v_t.v_temp from stu where sid=v_t.v_id;
dbms_output.put_line('编号:'||v_t.v_temp.sid||' 姓名:'||v_t.v_temp.sname||' 年龄:'||v_t.v_temp.sage);
end;
/*
多重if结构
*/
declare
v_score number(3);
begin
v_score:='&请输入成绩:';
if(v_score>90) then
dbms_output.put_line('优秀!');
elsif(v_score>70) then
dbms_output.put_line('中等!');
elsif(v_score>60) then
dbms_output.put_line('及格!');
else
dbms_output.put_line('请家长!');
end if;
end;
/*
case when then else end case;
*/
declare
v_score number(3);
begin
v_score:='&请输入成绩:';
case
when v_score>90 then dbms_output.put_line('优秀!');
when v_score>70 then dbms_output.put_line('中等!');
when v_score>60 then dbms_output.put_line('及格!');
else
dbms_output.put_line('请家长!');
end case;
end;
/*
循环:loop end loop;
while和for
*/
declare
v_i number(4);
begin
v_i:=1;
loop
v_i:=v_i+1;
exit when v_i>50;--退出的时候
dbms_output.put_line(v_i);
end loop;
end;
--while
declare
v_i number(4);
begin
v_i:=1;
while(v_i<50) loop
dbms_output.put_line(v_i);
v_i:=v_i+1;--改变表达式值
end loop;
end;
--for
declare
begin
for v_i in 1..50 loop
dbms_output.put_line(v_i);
end loop;
end;
--for实现 50到1依次输出 reverse:逆向输出
declare
begin
for v_i in reverse 1..50 loop
dbms_output.put_line(v_i);
end loop;
end;