set serverout on; ----这个只能在SQL PLUS里面使用,意思是在窗口里显示服务器输出信息。 declare n number:=1; ---定义一个变量(当然可以连续定义多个变量) v varchar2(20):=‘百零‘; n2 number:=5; begin dbms_output.put_line(‘这是‘||n||v||n2); ----随便在什么地方,只要是BEGIN和END之间,就可以使用DBMS_OUTPUT.PUT_LINE(output) end; ---- if then end if set serveroutput on; declare scount number; begin select count(*) into scount from student where score>80; ----into 赋值 if scount>0 then dbms_output.put_line(‘有‘||scount||‘个学生成绩大于80分‘); end if; end; ---- case when then set serveroutput on; declare scount number; begin select count(*) into scount from student where score>80; case scount when 0 then dbms_output.put_line(‘有0个学生成绩大于80分‘); when 1 then dbms_output.put_line(‘有1个学生成绩大于80分‘); when 2 then dbms_output.put_line(‘有2个学生成绩大于80分‘); when 3 then dbms_output.put_line(‘有3个学生成绩大于80分‘); when 4 then dbms_output.put_line(‘有4个学生成绩大于80分‘); end case; end; ---- loop循环(无条件,所以要ziji定义退出的条件) set serveroutput on; declare siid number:=1; sname varchar2(20); sscore varchar2(20); begin loop if(siid>4) then exit; end if; select s.name,s.score into sname,sscore from student s where s.id=siid; dbms_output.put_line(siid||‘号的成绩和名字是:‘||sscore||sname); siid:=siid+1; end loop; end; ---- while循环 set serveroutput on; declare siid number:=1; sname varchar2(20); sscore varchar2(20); begin while(siid<5) loop select s.name,s.score into sname,sscore from student s where s.id=siid; dbms_output.put_line(siid||‘号的成绩和名字是:‘||sscore||sname); siid:=siid+1; end loop; end; ----for 循环 (参数用..链接) set serveroutput on; declare sname varchar2(20); sscore varchar2(20); begin for s_id in 1..4 loop select s.name,s.score into sname,sscore from student s where s.id=s_id; dbms_output.put_line(s_id||‘号的成绩和名字是:‘||sscore||sname); end loop; end;
时间: 2024-10-29 19:06:58