加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码网 (https://www.900php.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

oracle基础知识语法大全

发布时间:2021-01-15 09:28:22 所属栏目:百科 来源:网络整理
导读:副标题#e# ORACLE支持五种类型的完整性约束 NOT NULL (非空)--防止NULL值进入指定的列,在单列基础上定义,默认情况下,ORACLE允许在任何列中有NULL值. CHECK (检查)--检查在约束中指定的条件是否得到了满足. UNIQUE (唯一)--保证在指定的列中没有重复值.在该

五、游标管理游标类型:隐式游标,显式游标,REF游标REF游标用于处理运行时才能确定的动态SQL查询的结果 ==========隐式游标==========在PL/SQL中使用DML语句时自动创建隐式游标隐式游标自动声明、打开和关闭,其名为SQL隐式游标的属性:%found SQL语句影响实质后返回true%notfound SQL语句没有影响实质后返回true%rowcount SQL语句影响的行数%isopen 游标是否打开,始终为false示例:beginupdate user_tbl set score=score+5;if SQL%found then dbms_output.put_line(‘数据被更改: ‘||SQL%rowcount);elsif sql%notfound then dbms_output.put_line(‘没有找到数据!‘);end if;if SQL%isopen then dbms_output.put_line(‘Open‘);else dbms_output.put_line(‘Close‘);end if;end; ==========显式游标==========在PL/SQL的声明部分定义查询,该查询可以返回多行J 声明游标J 打开游标J 从游标中取回数据J 关闭游标声明游标完成两个任务:给游标命名将一个查询与游标关联cursor cursor_name is select statement;打开游标: open cursor_name;取数据: fetch cursor_name into record_list;关闭游标: close cursor_name;显式游标的属性:%found 执行最后一条fetch语句成功返回行时为true%notfound 执行最后一条fetch语句未能返回行时为true%rowcount 返回到目前为止游标提取的行数%isopen 游标是否打开 示例:declareusers user_tbl%rowtype;cursor boys_cur is select * from user_tbl where sex=‘h‘;beginopen boys_cur;loopfetch 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; 带参的显式游标declareusers user_tbl%rowtype;cursor boys_cur(sexParam varchar2)is select * from user_tbl where sex=sexParam;beginopen boys_cur(‘&sex‘);loopfetch 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; 使用显式游标更新行declarecursor user_update_cur is select sex from user_tbl for update;usersex user_tbl.sex%type;beginopen user_update_cur;loopfetch 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; 循环游标declarecursor user_cur is select * from user_tbl;beginfor 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;----声明强类型的游标declaretype ref_cur is ref cursor return user_tbl%rowtype;users_cur ref_cur;----声明弱类型的游标declaretype ref_cur is ref cursor;users_cur ref_cur;示例----强类型declaretype ref_cur is ref cursor return user_tbl%rowtype;users_cur ref_cur;users user_tbl%rowtype;beginopen 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;----弱类型declaretype ref_cur is ref cursor;my_cur ref_cur;users user_tbl%rowtype;stus stu_tbl%rowtype;beginopen 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;loopfetch my_cur into stus;exit when my_cur%notfound;dbms_output.put_line(stus.stu_Name);end loop;close my_cur;end;----动态SQL游标declaretype ref_cur is ref cursor;my_cur ref_cur;users user_tbl%rowtype;username varchar2(20);sqlstmt varchar2(200);beginusername:=‘&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 procedureproce_name (parameter_list)is|aslocal variable declarationbeginexecutable statementsexceptionexception_handlersend proce_name; 过程参数的三种模式:In----用于接收调用的值,默认的参数模式Out----用于向调用程序返回值In out----用于接收调用程序的值,并向调用程序返回更新的值执行过程的语法:Execute proce_name(parameter_list);或DeclareVariable var_list;BeginProce_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 functionFun_name (parameter_list)Return datatype is|asLocal declarationsBeginExecutable statements;Return result;ExceptionExce_handlers;End;函数只能接收in参数,不能接受out或in out参数,形参不能是PL/SQL类型函数的返回类型也必须是数据库类型访问函数的方式:J 使用PL/SQL块J 使用SQL语句Select fun_name(parameter_list) from dual;

(编辑:源码网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读