PL/SQL基础语法

1.分支结构
    PL/SQL中,使用if关键字作为分之结构的程序起始段。
    总体有以下几种分支结构:
    1)if condition then statement end if;
    2)if condition then statement
        else then statement
        end if;
    3)if condition then statement
        elsif condition then statement
        ...
        else then statement
        end if;
    分别对应于其它语言中的if(){},if(){}else(){},if(){}else if(){}...else{}结构
    此外,PL/SQL还提供case关键字作为分支结构的标识段。
    case的用法有以下几种:
    1)对某个字段进行单值匹配
       
case column1
            when value1 then statement
            when value2 then statement
            ...
        end case;

    这种结构类似于其它语言中的switch结构。
    2)使用case..when结构来表达if..else条件语法
       
case 
            when condition then statement
            when condition then statement
           ...
        end case;

2.循环结构
    PL/SQL中,具体的循环结构有以下几种:
    1)for
    for i in rs.first..rs.last loop
    end loop;
    这种结构在遍历游标指向的集合时,显得简洁而高效,该结构可实现自动管理游标:包括游标的打开、关闭和自动fetch。
    示例:
    declare  
        cursor c_emp is 
               select * from emp;
        emp_record emp%rowtype;
    begin
        for emp_record in c_emp
            loop
                 dbms_output.put_line(emp_record.empno);
            end loop;
    end;
     这段PL/SQL程序并未打开游标,也没有使用fetch语句,更没有关闭游标,但是使用for..in结构可以自动隐式完成这些工作,因此简洁高效,且不易出错。
  2)loop when
    loop
    	exit when (statement)
    end loop;
     3)loop while
    while (statement)
    loop
    end loop;
      4)goto
    loop
    	if (statement) then goto label_name
    	end if;
    end loop;
    <<label_name>>
    在PL/SQL中,可以使用<<label_name>>的形式定义一个标签,使用goto关键字即可让程序在执行时跳转至该标签,从而结束循环体。

猜你喜欢

转载自bjtale.iteye.com/blog/2229303