Oracle 变量与循环

SET serveroutput ON;
--引用行变量
DECLARE
    --查询打印7839的姓名和薪水
    Ename EMP.ENAME%TYPE;
    Sal EMP.SAL%TYPE;
BEGIN
    SELECT ename,sal INTO ename,sal FROM emp WHERE empno=7839;
    dbms_output.put_line(ename||'的薪水是'||sal);
END;

--记录行变量 表中某一行的数据
SET serveroutput ON;
declare
 emp_row emp%rowtype;
begin
  select * into emp_row from emp where empno=7839;
  dbms_output.put_line(emp_row.ename||'的薪水是'||emp_row.sal||',部门编码是'||emp_row.deptno);
end;

--IF判断语句
SET serveroutput ON;
declare
  emp_row emp%rowtype;
begin
  select * into emp_row from emp where empno=7839;
  if emp_row.sal>1000 then 
    dbms_output.put_line('1级');
  end if;
end;

--循环语句 while 打印数字的1-10
set serveroutput on;
declare
  pnum int:=1;
begin
  while pnum<=10 loop
    dbms_output.put_line(pnum);
    --变量自增+1
    pnum:=pnum+1;
  end loop;  
end;

--loop 循环
set serveroutput on;
declare
 pnum int:=1;
begin
  loop 
   exit when pnum>10;
   dbms_output.put_line(pnum);
    --变量自增+1
    pnum:=pnum+1;
  end loop;
end;

--FOR
set serveroutput on;
declare
  pnum number:=1;
begin
  for pnum in 1..10 loop
    dbms_output.put_line(pnum);
  end loop;
end;

你可能感兴趣的:(Oracle)