Oracle基础 各种语句的定义格式(3)

str_sql:=’create table’||’ ’||table_name||’(’||field1||’ ’||’datatype1’||’,’|| field2||’ ’||’datatype2’||’)’;
execute immediate str_sql;

10、 if条件语句
if condition then
sequence_of_statements
end if;

if condition then
sequence_of_statement
else
sequence_of_statement
end if;

if condition then
sequence_of_statement
else if condition2 then
sequence_of_statement
else
sequence_of_statement
end if;


11、 case语句
case selector
when expression then sequence_of_statements;
when expression then sequence_of_statements;
when expression then sequence_of_statements;
[else  sequence_of_statements;]
end case;

12、 loop循环
loop
  sequence_of_statements
  if a>0 then   
exit              或exit when a>0
end if;
end loop;

13、 for-loop语句
for counter in [reverse] lower_bound..higher_bound loop
sequence_of_statement
        end loop;

如:
        for I in 1..v_count loop
list(i) :=i*I;
end loop;

14、 while-loop语句
while condition loop
sequence_of_statements
end loop;

15、 游标定义
a、cursor cursor_name [(parameter[,parameter]…)]
[return return_type] is select_statement
b、open cursor_name
c、fetch cursor_name into variable[,variable,…]
d、close cursor_name

例:
declare
  cursor c_emp_ename is select ename form emp;
v_ename emp.ename%type;
v_count binary_integer;
begin
select count(rowed)
into v_count
from emp;
open c_emp_ename;
for I in i..v_count loop
fetch c_emp_ename into v_ename;
dbms_output.put_line(vname);
end loop;
close c_emp_ename;
end

16、 cursor for循环及其替代语句
a、 先定义游标,之后用in(cursor_name)的方式使用该循环
cursor cursor_dept is select deptno ,dname from dept order by deptno;
for var in cursor_dept loop
在这里可以使用var来得到游标所指数据
end loop
b、 采用in(查询语句)的方式使用该循环
for var  in(select deptno ,dname from dept order by deptno;) loop
在这里可以使用var来得到游标所指数据
end loop


17、 显示游标属性
%found:if c_emp_ename %fount then … end if;
% notfount:exit when c_emp_ename %notfound;
%isopen:if c_emp_ename % isopen then … end if;
%rowcount:提取次数if c_emp_name %rowcount >10 then … end if

18、 隐式游标(SQL游标)
用来处理insert、update、delete和返回一行的select into语句,对这几个语句进行操作时判断处理结果用的。
不可使用open、fetch和close进行操作。
也包含%fount、%notfount、%isopen(总是false)、%rowcount。

19、 异常处理
a、 异常的抛出方式
pl/sql运行时
raise exception_name
调用raise_application_erroe
b、 exception
when exception_name then
    处理代码;
when exception_name then
    处理代码;
when others then
    处理代码;
c、 自定义异常
declare
  exceptin_name exception;
begin
  statements;
raise <exception_name>
exception
  when <exception_name> then
end;

20、 子程序

1、 存储过程
create [or replace] procedure <procedure_name>
(<arg1[in|out|in out] ,datatype,……>)
is|as
[local declaration]
begin
executable statements
[exception handler]
edn [procedure_name]

2、 函数
create [or replace] function <function_name>
(<arg1[mode],datatype>,……)
return<datatype> is|as
[local declaration]
begin
executable statements
[exception handler]
end [function_name]

函数和过程都可以通过参数列表接收或返回另个或多个值;函数和过程的主要区别在于他们的调用方式,过程是作为一个独立的执行语句调用的,而调用函数后需将函数的返回值赋值给某一变量。
3、 包
包定义:
create [or replace] package package_name {as|is}
public_variable_declarations|
public_type_declarations|
public_exception_declarations|
public_cursor_declarations|
function_declarations声明|
procedure_specifications声明
end [package_name]
包主体:
create [or replace] package body package_name {as|is}
public_variable_declarations|
public_type_declarations|
public_exception_declarations|
public_cursor_declarations|
function_declarations实现|
procedure_specifications实现
end [package_name]

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/498bd2621184a3ecd7a14f0d9c5e12d0.html