Oracle认证 百分网手机站

Oracle数据库语句(3)

时间:2018-03-21 09:05:13 Oracle认证 我要投稿

Oracle数据库语句大全

  cursor boys_cur is select * from user_tbl where sex='h';

  begin

  open boys_cur;

  loop

  fetch boys_cur into users;

  exit when boys_cur%notfound;

  dbms_output.put_line(users.user_name||' '||users.password);

  dbms_output.put_line(boys_cur%rowcount);

  end loop;

  close boys_cur;

  end;

  带参的显式游标

  declare

  users user_tbl%rowtype;

  cursor boys_cur(sexParam varchar2)

  is select * from user_tbl where sex=sexParam;

  begin

  open boys_cur('&sex');

  loop

  fetch boys_cur into users;

  exit when boys_cur%notfound;

  dbms_output.put_line(users.user_name||' '||users.password);

  dbms_output.put_line(boys_cur%rowcount);

  end loop;

  close boys_cur;

  end;

  使用显式游标更新行

  declare

  cursor user_update_cur is select sex from user_tbl for update;

  usersex user_tbl.sex%type;

  begin

  open user_update_cur;

  loop

  fetch user_update_cur into usersex;

  exit when user_update_cur%notfound;

  dbms_output.put_line(usersex);

  if usersex = 'M' then

  update user_tbl set score=score-5 where current of user_update_cur;

  else

  update user_tbl set score=score+5 where current of user_update_cur;

  end if;

  end loop;

  close user_update_cur;

  commit;

  end;

  循环游标

  declare

  cursor user_cur is select * from user_tbl;

  begin

  for username in user_cur loop

  dbms_output.put_line(username.user_name||' '||username.sex);

  end loop;

  end;

  ==========REF游标==========

  REF游标和游标变量用于处理运行时动态执行的SQL查询

  创建游标变量的步骤:

  J 声明REF游标类型

  J 声明REF游标类型的变量

  声明类型的语法

  Type ref_cursor_name is ref cursor [return return_type];

  打开游标变量的语法

  Open cursor_name for select_statement;

  ----声明强类型的游标

  declare

  type ref_cur is ref cursor return user_tbl%rowtype;

  users_cur ref_cur;

  ----声明弱类型的游标

  declare

  type ref_cur is ref cursor;

  users_cur ref_cur;

  示例

  ----强类型

  declare

  type ref_cur is ref cursor return user_tbl%rowtype;

  users_cur ref_cur;

  users user_tbl%rowtype;

  begin

  open users_cur for select * from user_tbl where user_name='ny2t92';

  loop

  fetch users_cur into users;

  exit when users_cur%notfound;

  dbms_output.put_line(users.user_Name);

  end loop;

  close users_cur;

  end;

  ----弱类型

  declare

  type ref_cur is ref cursor;

  my_cur ref_cur;

  users user_tbl%rowtype;

  stus stu_tbl%rowtype;

  begin

  open my_cur for select * from user_tbl;

  loop

  fetch my_cur into users;

  exit when my_cur%notfound;

  dbms_output.put_line(users.user_Name);

  end loop;

  close my_cur;

  open my_cur for select * from user_tbl where user_name='ny2t92';

  loop

  fetch my_cur into users;

  exit when my_cur%notfound;

  dbms_output.put_line(users.user_Name);

  end loop;

  close my_cur;

  open my_cur for select * from stu_tbl;

  loop

  fetch my_cur into stus;

  exit when my_cur%notfound;

  dbms_output.put_line(stus.stu_Name);

  end loop;

  close my_cur;

  end;

  ----动态SQL游标

  declare

  type ref_cur is ref cursor;

  my_cur ref_cur;

  users user_tbl%rowtype;

  username varchar2(20);

  sqlstmt varchar2(200);

  begin

  username:='&username';

  sqlstmt := 'select * from user_tbl where user_name= :name';

  open my_cur for sqlstmt using username;

  loop

  fetch my_cur into users;

  exit when my_cur%notfound;

  dbms_output.put_line(users.user_Name);

  end loop;

  close my_cur;

  end;

  六.子程序

  子程序分为:存储过程和函数,它是命名的PL/SQL块,编译并存储在数据库中。

  子程序的各个部分:声明部分,可执行部分,异常处理部分。

  过程----执行某些操作

  函数----执行操作并返回值

  ==========存储过程==========

  创建过程的语法:

  create or replace procedure

  proce_name (parameter_list)

  is|as

  local variable declaration

  begin

  executable statements

  exception

  exception_handlers

  end proce_name;

  过程参数的三种模式:

  In----用于接收调用的值,默认的参数模式

  Out----用于向调用程序返回值

  In out----用于接收调用程序的值,并向调用程序返回更新的值

  执行过程的语法:

  Execute proce_name(parameter_list);

  或

  Declare

  Variable var_list;

  Begin

  Proce_name(var_list);

  End;

  将过程执行的权限授予其他用户:

  Grant execute on proce_name to scott;

  Grant execute on proce_name to public;

  删除存储过程:

  Drop procedure proce_name;

  ==========函数==========

  创建函数的语法:

  Create or replace function

  Fun_name (parameter_list)

  Return datatype is|as

  Local declarations

  Begin

  Executable statements;

  Return result;

  Exception

  Exce_handlers;

  End;

  函数只能接收in参数,不能接受out或in out参数,形参不能是PL/SQL类型

  函数的返回类型也必须是数据库类型

  访问函数的方式:

  J 使用PL/SQL块

  J 使用SQL语句

  Select fun_name(parameter_list) from dual;

【Oracle数据库语句大全】相关文章:

1.oracle数据库基本语句

2.Oracle数据库SELECT语句

3.Oracle数据库基础知识:SELECT语句

4.ORACLE数据库操作基本语句

5.ORACLE数据库基本命令

6.Oracle数据库的聚簇技术

7.Oracle数据库SQLPLUS介绍

8.oracle的sql语句