搜档网
当前位置:搜档网 › Oralce PLSQL 存储过程 之 游标 实践!

Oralce PLSQL 存储过程 之 游标 实践!

Oralce PLSQL 存储过程 之 游标 实践!
Oralce PLSQL 存储过程 之 游标 实践!

声明:本文中的所有代码均是实战敲出来的!杜绝copy!

代码是以游标的编写、调用、为目的。从简单到复杂,用迭代的思想不断丰富例子功能。希望对各位同学有所帮助。

------------单纯的一个声明,然后输入一个数,打印输出

declare v_salemp.sal%type ;

begin

select sal into v_sal from emp

where emp.empno=&no;

dbms_output.put_line(v_sal||'Oralce存储过程实践!我是可以实现的!');

end;

-----------声明一个游标,遍历游标,处理游标的值/不输入参数的形式

declare

cursor v_sal_cursor isselect sal from emp ;

var_salemp.sal%type;

begin

open v_sal_cursor;

loop

fetch v_sal_cursor into var_sal ;

exitwhen v_sal_cursor%notfound;

dbms_output.put_line(var_sal ||'--------我就要成功了!!') ;

dbms_output.put_line('当前游标状态:');

endloop;

close v_sal_cursor;

end;

----------声明一个游标,遍历游标,对话框输入一个参数,打印输出

declare

cursor v_sal_cursor isselect sal from emp where emp.empno=&no ;

var_salemp.sal%type;

begin

open v_sal_cursor;

loop

fetch v_sal_cursor into var_sal ;

exitwhen v_sal_cursor%notfound;

dbms_output.put_line(var_sal ||'--------我就要成功了!!') ;

dbms_output.put_line('当前游标状态:');

endloop;

close v_sal_cursor;

end;

---------在这里没有用循环,所以只会取出一个结果,也就是一行记录

declare

cursor v_sal_cursor isselect emp.sal from emp ;

var_salemp.sal%type;

open v_sal_cursor;

fetch v_sal_cursor into var_sal;

if v_sal_cursor%foundthen

dbms_output.put_line('我是XXX,高级JAVA软件工程师!'|| var_sal); endif;

close v_sal_cursor;

end;

-----用if else 判断语句,另外加上循环进行遍历的方法。

declare

cursor v_sal_cursor isselect emp.sal from emp orderby emp.sal asc; var_salemp.sal%type;

begin

open v_sal_cursor;

loop

fetch v_sal_cursor into var_sal;

dbms_output.put_line('行的总数:'||v_sal_cursor%rowcount);

if v_sal_cursor%foundthen

dbms_output.put_line('我是XXX,我是高级软件工程师!'|| var_sal); elseexit;

endif;

endloop;

close v_sal_cursor;

end ;

----------带参数的游标[select distinct emp.deptno from emp;] declare

cursor v_sal_cursor (in_deptNo number)

isselect emp.sal from emp where emp.deptno=in_deptNo;

var_salemp.sal%type;

begin

open v_sal_cursor(10);

loop

fetch v_sal_cursor into var_sal;

if v_sal_cursor%foundthen

dbms_output.put_line(' 部门薪水是='|| var_sal);

elseexit;

endif;

endloop;

close v_sal_cursor;

-------定义两个游标,类似于两个集合list ,一个用于获取部门的id ,然后作为参数传递到下一个游标中去,取出数据处理

declare

cursor v_depno_cursor isselectdistinct dept.deptno from dept;

cursor v_emp_cursor(in_deptno number)

isselect emp.ename,emp.sal from emp where emp.deptno=in_deptno;

var_deptNodept.deptno%type;

var_emp_name_salv_emp_cursor%rowtype;

begin

open v_depno_cursor;

loop

fetch v_depno_cursor into var_deptNo;

if v_depno_cursor%foundthen

dbms_output.put_line('当前的部门编号是:'||var_deptNo);

open v_emp_cursor(var_deptNo);

loop

fetch v_emp_cursor into var_emp_name_sal;

if v_emp_cursor%foundthen

dbms_output.put_line('通过部门编号查询得到的雇员信息如下:'||' 雇员:

'||var_emp_name_sal.ename||' 薪水:'||var_emp_name_sal.sal);

elseexit;

endif;

endloop;

close v_emp_cursor;

elseexit;

endif;

endloop;

close v_depno_cursor;

end ;

--------------创建一个备份表,用于测试

createtable emp_bak asselect * from emp;

select * from emp_bak;

--------------用带参数的游标更新数据!【实际上和以前的变化主要在于:

--------------1、定义部分,加上for update。2、在修改数据的语句中加上where current of XXXcursor;】

declare

cursor v_dept_cursor isselectdistinct dept.deptno from dept;

cursor v_emp_cursor (in_deptNo number)

isselect emp_bak.ename,emp_bak.sal from emp_bak where emp_bak.deptno=in_de ptNo forupdate ;

var_deptNodept.deptno%type;

var_emp_name_salv_emp_cursor%rowtype;

begin

open v_dept_cursor;

loop

fetch v_dept_cursor into var_deptNo;

if v_dept_cursor%foundthen

open v_emp_cursor(var_deptNo);

loop

fetch v_emp_cursor into var_emp_name_sal;

if v_emp_cursor%foundthen

if var_emp_name_sal.sal<2000then

dbms_output.put_line('-----------'||var_emp_name_sal.sal); update emp_bak set sal=sal+200wherecurrentof v_emp_cursor; elseexit;

endif;

elseexit;

endif;

endloop;

close v_emp_cursor;

elseexit;

endif;

endloop;

commit;------提交,注意:如果是不放心的情况下,可以手动提交

close v_dept_cursor;

end;

游标与存储过程

实验5 游标与存储过程 1、实验目的 1. 学习实践游标与存储过程 2. 学习实践PL/SQL编程 2、实验原理 1. PL/SQL编程 2. 游标与存储过程 3、实验器材 1. 安装了Oracle,或者MySQL的计算机 4、实验内容 3. 创建表 Code Name Amt 01服装900 0101男装300 010101西装100 010102休闲装200 0102女装390 010201套装120 010202职业装130 010203休闲装140 0103童装210 02电器290 0201进口140 0202国产150 03日用品300 2.编写Oracle的存储过程,实现层次结构的逐级求和。

3.应用sql*plus,编写PL/SQL调用步骤2编写的存储过程。 五、实验报告要求 请将相应SQL语句写在实验报告上 1、 create table example(code number(10),name varchar2(20),amt number(10)); 2、 insert into example values(01,'服装',900); 3、 insert into example values(0101,'男装',300); 4、 insert into example values(010101,'西装',100); 5、 insert into example values(010102,'休闲装',200); 6、 insert into example values(0102,'女装',390); 7、 insert into example values(010201,'套装',120); 8、 insert into example values(010202,'职业装',130); 9、 insert into example values(010203,'休闲装',140); 10、 insert into example values(0103,'童装',210); 11、 insert into example values(02,'电器',290);、 12、 insert into example values(0201,'进口',140); select * from example; CODE NAME AMT --------- -------------------- ---------- 1 服装 900 101 男装 300 10101 西装 100 10102 休闲装 200 102 女装 390 10201 套装 120 10202 职业装 130 10203 休闲装 140

在Sql Server存储过程中使用Cursor(游标)操作记录

1.为何使用游标: 使用游标(cursor)的一个主要的原因就是把集合操作转换成单个记录处理方式。用SQL语言从数据库中检索数据后,结果放在内存的一块区域中,且结果往往是一个含有多个记录的集合。游标机制允许用户在SQL server内逐行地访问这些记录,按照用户自己的意愿来显示和处理这些记录。 2.如何使用游标: 一般地,使用游标都遵循下列的常规步骤: (1) 声明游标。把游标与T-SQL语句的结果集联系起来。 (2)打开游标。 (3)使用游标操作数据。 (4)关闭游标。 2.1.声明游标 DECLARE CURSOR语句SQL-92标准语法格式: DECLARE游标名[ INSENSITIVE ] [ SCROLL ] CURSOR FOR sql-statement Eg: Declare MycrsrVar Cursor FOR Select * FROM tbMyData 2.2打开游标 OPEN MycrsrVar 当游标被打开时,行指针将指向该游标集第1行之前,如果要读取游标集中的第1行数据,必须移动行指针使其指向第1行。就本例而言,可以使用下列操作读取第1行数据: FETCH FIRST from E1cursor 或FETCH NEXT from E1cursor 2.3 使用游标操作数据 下面的示例用@@FETCH_STATUS控制在一个WHILE循环中的游标活动 DECLARE E1cursor cursor FOR SELECT * FROM c_example OPEN E1cursor FETCH NEXT from E1cursor WHILE @@FETCH_STATUS = 0 BEGIN FETCH NEXT from E1cursor END CLOSE E1cursor DEALLOCATE E1cursor 2.4 关闭游标 使用CLOSE语句关闭游标 CLOSE { { [ GLOBAL ]游标名} |游标变量名} 使用DEALLOCATE语句删除游标,其语法格式如下: DEALLOCATE { { [ GLOBAL ]游标名} | @游标变量名

ex11游标与存储过程答案

实验十一游标与存储过程 (1)创建游标,逐行显示表Customer.的记录,并用WHILE结构来测试@@Fetch_Status的返回值。输出格式如下: '客户编号'+'-----'+'客户名称'+'----'+'客户地址'+'-----'+'客户电话 '+'------'+'客户邮编'+'------' DECLARE cur_cust SCROLL cursor FOR SELECT* FROM customer DECLARE @p_CustId char(5) DECLARE @p_CustName char(20) DECLARE @p_address char(40) DECLARE @p_Tel char(10) DECLARE @p_Zip char(6) DECLARE @p_All char(100) SELECT @p_All='客户编号'+'------'+'客户名称'+'------'+'客户地址 '+'-------------------------------------'+'客户电话'+'-------'+'客户邮 编'+'------' PRINT @p_All OPEN cur_cust FETCH cur_cust into @p_CustId,@p_CustName,@p_address,@p_Tel,@p_Zip WHILE(@@fetch_status<>-1) BEGIN SELECT @p_All=@p_CustId+' '+@p_CustName+@p_address+@p_Tel+' '+@p_Zip print @p_All FETCH cur_cust into @p_CustId,@p_CustName,@p_address,@p_Tel,@p_Zip END PRINT'客户数目: '+CONVERT(char(5),@@CURSOR_ROWS) CLOSE cur_cust DEALLOCATE cur_cust

实验16 游标、存储过程和函数参考答案

实验十六游标、存储过程和函数 一、目的与要求 1.了解游标的概念和工作原理; 2.了解存储过程的分类和使用方法; 3.了解触发器的概念; 4.学习编写和执行自定义过程; 5.学习编写和执行自定义函数; 6.学习创建和使用触发器。 二、实验准备 1.首先要了解游标是映射在结果集中一行数据上的位置实体,有了游标,用户就可以访问结果集中的任意一行数据了。将游标放置到某行后,即可对该行数据进行操作,最常见的操作是提取当前行数据。 2.使用显式游标的步骤: (1)说明游标。 (2)打开游标。 (3)读取数据。 (4)关闭游标。 3.了解PL/SQL包括3种存储过程,即过程、函数和程序包。 4.了解触发器是一种特殊的存储过程,当指定表中的数据发生变化时自动运行。 三、实验内容 1.练习书上的例子10.1—10.24。 2.以上机实验经常用到的数据库LIB为例,编写过程P_ResetPrice,此过程的功能是将表图书中指定书号的单价更改为10.0,调用该过程将书号为’TP311.13/CM3’的单价更改为10.0,将程序写在实验报告中。 create or replace procedure P_ResetPrice (vBno in varchar2) as begin update 图书 set 单价=10.0 where 图书号=vBno; end; execute P_ResetPrice(‘TP311.13/CM3’); 3.编写一函数F_GetBName,该函数的功能是在图书中根据指定的书号,返回该书的书名,并在匿名块中调用函数F_GetBName找出编号为“TP311.132/ZG1”的书名,将程序写在实验报告中。 create function F_GetName (vtno IN 图书.图书号%Type) return 图书.书名%Type

实验九 游标与存储过程

实验九游标与存储过程 1 实验目的与要求 (1) 掌握游标的定义和使用方法。 (2) 掌握存储过程的定义、执行和调用方法。 (3) 掌握游标和存储过程的综合应用方法。 2 实验内容 请完成以下实验内容: (1)创建游标,逐行显示Customer表的记录,并用WHILE结构来测试 @@Fetch_Status的返回值。输出格式如下: declare @C_no char(9),@C_name char(18),@C_phone char(10), @C_addchar(8),@C_zip char(6) declare @text char(100) declarecus_cur scroll cursor for select* from Customer select @text='=========================Customer 表的记录 =========================' print @text select @text='客户编号'+'-----'+'客户名称'+'----'+'客户住址'+'-----'+'客户电话'+'------'+'邮政编码' print @text select @text='============================================================ ============================' print @text opencus_cur fetchcus_cur into @C_no,@C_name,@C_phone,@C_add,@C_zip while(@@fetch_status=0) begin select @text=@cust_No+' '+@cust_name+' '+@addr+' '+@tel_no+' '+@zip print @text fetchcus_cur into @C_no,@C_name,@C_phone,@C_add,@C_zip end closecus_cur deallocatecus_cur '客户编号'+'-----'+'客户名称'+'----'+'客户住址'+'-----'+'客户电话'+'------'+'邮政编码'

SQL游标嵌套存储过程

--测试数据 create table tmp1 ( ID int not null, val varchar(10), constraint PK_tmp1 primary key (ID) ); create table tmp2 ( ID int not null, vals varchar(100), constraint PK_tmp2 primary key (ID) ); insert into tmp1(id, val) values (1, 'test'); insert into tmp1(id, val) values (2, 'test2'); insert into tmp1(id, val) values (3, 'test3'); insert into tmp1(id, val) values (4, 'test4'); insert into tmp1(id, val) values (5, 'test5'); insert into tmp2(id, vals) values (1, '1,2'); insert into tmp2(id, vals) values (2, '1,3'); insert into tmp2(id, vals) values (3, '2,5'); insert into tmp2(id, vals) values (4, '1,2,3,4,5'); --存储过程 drop procedure proc_tmp_1 go CREATE PROCEDURE proc_tmp_1 AS begin declare @vals varchar(500) declare @id int declare @vals2 varchar(1000) declare @command varchar(1000) declare @vals3 varchar(1000) declare @cmd varchar(1000) declare cursor_tmp_1 cursor for SELECT id, vals FROM tmp2 open cursor_tmp_1 fetch next from cursor_tmp_1 into @id, @vals while @@fetch_status = 0 begin set @vals3 = '' set@cmd = 'declare cursor_tmp_2 cursor for select val from tmp1 where id in (' + @vals + ')' EXEC (@cmd) open cursor_tmp_2 fetch next from cursor_tmp_2 into @vals2 while @@fetch_status = 0 begin if (@vals3 <> '') begin set @vals3 = @vals3 + ',' end SET @vals3 = @vals3 + @vals2

sql server存储过程详细代码(超赞!)

use jxgl --首先判断有没有已经建立up_getallstudents存储过程,有则先删除 if exists (select name from sysobjects where name='up_getallstudents'and type ='p') drop procedure up_getallstudents --编写存储过程up_getallstudents,用于获取学生表students的所有记录 create procedure up_getallstudents as select*from students --使用execute执行存储过程up_getallstudents exec up_getallstudents --也可写成 execute up_getallstudents --编写一个存储过程up_insertstudent,完成学生表students数据的插入 --1、不带默认值的参数 create procedure up_insertstudent @sid varchar(15),@sname varchar(30),@ssex char(10), @sbirth datetime,@sbirthplace varchar(300) as begin insert into students (stu_id,stu_name,stu_sex,stu_birth,stu_birthplace) values (@sid,@sname,@ssex,@sbirth,@sbirthplace) end exec up_insertstudent'200712110111','肖玉峰','男','1975-02-05','山东省滕州市木石镇' --等同于 exec up_insertstudent @sname='肖玉峰',@sid='200712110111',@ssex='男',@sbirth= '1975-02-05',@sbirthplace='山东省滕州市木石镇' drop procedure up_insertstudent delete students where stu_name='肖玉峰'

存储过程和游标

我们在进行pl/sql编程时打交道最多的就是存储过程了。存储过程的结构是非常的简单的,我们在这里除了学习存储过程的基本结构外,还会学习编写存储过程时相关的一些实用的知识。如:游标的处理,异常的处理,集合的选择等等 1.存储过程结构 1.1 第一个存储过程 Java代码 1.create or replace procedure proc1( 2. p_para1 varchar2, 3. p_para2 out varchar2, 4. p_para3 in out varchar2 5.)as 6. v_name varchar2(20); 7.begin 8. v_name := '三丰'; 9. p_para3 := v_name; 10. dbms_output.put_line('p_para3:'||p_para3); 11.end; 上面就是一个最简单的存储过程。一个存储过程大体分为这么几个部分: 创建语句:create or replace procedure 存储过程名 如果没有or replace语句,则仅仅是新建一个存储过程。如果系统存在该存储过程,则会报错。Create or replace procedure 如果系统中没有此存储过程就新建一个,如果系统中有此存储过程则把原来删除掉,重新创建一个存储过程。 存储过程名定义:包括存储过程名和参数列表。参数名和参数类型。参数名不能重复,参数传递方式:IN, OUT, IN OUT IN 表示输入参数,按值传递方式。 OUT 表示输出参数,可以理解为按引用传递方式。可以作为存储过程的输出结果,供外部调用者使用。 IN OUT 即可作输入参数,也可作输出参数。 参数的数据类型只需要指明类型名即可,不需要指定宽度。 参数的宽度由外部调用者决定。 过程可以有参数,也可以没有参数 变量声明块:紧跟着的as (is )关键字,可以理解为pl/sql的declare关键字,用于声明变量。 变量声明块用于声明该存储过程需要用到的变量,它的作用域为该存储过程。另外这里声明的变量必须指定宽度。遵循PL/SQL的变量声明规。 过程语句块:从begin 关键字开始为过程的语句块。存储过程的具体逻辑在这里来实现。 异常处理块:关键字为exception ,为处理语句产生的异常。该部分为可选 结束块:由end关键字结果。 1.2 存储过程的参数传递方式 存储过程的参数传递有三种方式:IN,OUT,IN OUT . IN 按值传递,并且它不允许在存储过程中被重新赋值。如果存储过程的参数没有指定存参数传递类型,默认为IN

Oracle存储过程学习_游标CURSOR使用

游标CURSOR的使用学习 游标的类型: 1,隐式游标:在 PL/SQL 程序中执行DML SQL 语句时自动创建隐 式游标,名字固定叫sql。 2,显式游标:显式游标用于处理返回多行的查询。 3,REF 游标:REF 游标用于处理运行时才能确定的动态 SQL 查询 的结果 一、隐式游标 在PL/SQL中使用DML语句时自动创建隐式游标 q隐式游标自动声明、打开和关闭,其名为 SQL q通过检查隐式游标的属性可以获得最近执行的DML 语句的信息q隐式游标的属性有: q%FOUND – SQL 语句影响了一行或多行时为 TRUE q%NOTFOUND – SQL 语句没有影响任何行时为TRUE q%ROWCOUNT – SQL 语句影响的行数 q%ISOPEN - 游标是否打开,始终为FALSE begin update student s set s.sage = s.sage + 10; if sql %FOUND then dbms_output.put_line('这次更新了' || sql% rowcount); else dbms_output.put_line('一行也没有更新'); end if; end; 在select中有两个中比较常见的异常: 1. NO_DATA_FOUND 2. TOO_MANY_ROWS declare sname1 student.sname%TYPE; begin

select sname into sname1 from student; if sql%found then dbms_output.put_line(sql%rowcount); else dbms_output.put_line('没有找到数据'); end if; exception when too_many_rows then dbms_output.put_line('查找的行记录多于1行'); when no_data_found then dbms_output.put_line('未找到匹配的行'); end; 显式游标: sqlserver与oracle的不同之处在于:最后sqlserver会deallocate 丢弃游标,而oracle只有前面四步:声明游标、打开游标、使用游标读取记录、关闭

存储过程与游标练习

创建三个表:学生(学号,姓名)、课程(课程号,课程名)、成绩(学号、课程号、分数),然后在三个表中分别添加记录。按照输入的课程名称打印此门课程的成绩报表(如不给定课程名称则打印SQL课程的成绩),输出结果按照分数降序排列: 例如: 《SQL》成绩表 **************************************************** 名次学号姓名成绩 1 0508044126 李军95 2 0508044124 李明85 3 0508044125 王刚75 **************************************************** */ use pubs IF EXISTS (SELECT NAME FROM sysobjects WHERE NAME = '学生') DROP table 学生 GO IF EXISTS (SELECT NAME FROM sysobjects WHERE NAME = '课程') DROP table 课程 GO IF EXISTS (SELECT NAME FROM sysobjects WHERE NAME = '成绩') DROP table 成绩 GO create table 学生(学号char(10) primary key constraint xh_chk check (学号like '0508044[1-4][0-3][0-9]'),姓名nvarchar(10) not null) create table 课程(课程号char(6) primary key,课程名称nvarchar(40)) create table 成绩(学号char(10) not null,课程号char(6) not null,分数numeric(4,1)) insert 学生values('0508044124','李明') insert 学生values('0508044125','王刚') insert 学生values('0508044126','李军') insert 课程values('080101','SQL') insert 课程values('080204','D S') insert 成绩values('0508044124','080101',85) insert 成绩values('0508044124','080204',95) insert 成绩values('0508044125','080101',75) insert 成绩values('0508044125','080204',86) insert 成绩values('0508044126','080101',95) go if exists(select * from sysobjects where name='cj_proc' and xtype='p') drop proc cj_proc

数据库编程技术——游标、存储过程与触发器

实验八数据库编程技术—游标、存储过程与触发器 一、实验目的 1.掌握游标的定义和使用方法 2.掌握存储过程的定义、执行和调用方法 3.掌握游标和存储过程的综合应用方法。 4.掌握触发器的创建和使用方法。 5.掌握游标和触发器的综合应用方法。 二、实验环境(实验的软件、硬件环境) 硬件:PC机软件:SQL2000 三、实验指导说明 请复习第八章数据库编程的相关知识,完成如下的实验内容。 四、实验内容 (1)利用游标查找所有女业务员的基本情况 (2)创建一游标,逐行显示表customer的记录,要求按 ‘客户编号’+‘-------’+‘客户名称’+‘-------’+‘客户地址’+‘-------------------’+‘客户电话’+‘----------’+‘客户邮编’+‘--------’格式输出,并且用while 结构来测试游标的函数@@Fetch_Status的返回值。 (3)利用游标修改orderMaster表中的Ordersum的值 (4)利用游标显示出orderMaster表中每一个订单所对应的明细数据信息。 (5)利用存储过程,给Employee表添加一条业务部门员工的信息。 (6)利用存储过程输出所有客户姓名、客户订购金额及其相应业务员的姓名 (7)利用存储过程查找某员工的员工编号、订单编号、销售金额。 (8)利用存储过程查找姓“李”并且职称为“职员”的员工的员工编号、订单编号、销售金额

(9)请使用游标和循环语句编写一个存储过程proSearchCustomer,根据客户编号,查询该客户的名称、地址以及所有与该客户有关的销售记录,销售记录按商品分组输出。 (10)设置一个触发器,该触发器仅允许dbo用户可以删除Employee表内数据,否则出错。 (11)在OrderMaster表中创建触发器,插入数据时要先检查Employee表中是否存在和Employee表同样值的业务员编号,如果不存在则不允许插入。 (12)级联更新:当更新customer表中的customerNo列的值时,同时更新OrderMaster表中的customerNo列的值,并且一次只能更新一行。 (13)对product表写一个UPDATE触发器。 五、实验步骤 请完成实验内容,并写出具体的实验步骤 六、思考题: 1.存储过程和触发器,函数的区别? 七、总结(实验过程的体会、心得和实验教与学之间还需改进的内容)

实验9-T-SQL、游标、存储过程、并发控制

XX实验报告 学号 : 系别专业班级姓名 课程名称课程 类型 学时数 实验 名称 T-SQL、游标、存储过程、并发控制 实验目的: 1、了解并能简单应用T-SQL语言。 2、理解并简单的使用游标。 实验内容: 一、了解并应用T-SQL编程语言 (1)用下面的脚本创建一个表并利用循环向表中添加26条记录: USE AdventureWorks CREATE TABLE MYTB(ID INT,VAL CHAR(1)) GO DECLARE @COUNTER INT; SET @COUNTER=0 WHILE(@COUNTER < 26) BEGIN INSERT INTO MYTB VALUES(@COUNTER,CHAR(@COUNTER + ASCII(‘A’))) SET @COUNTER= @COUNTER + 1 END 在Microsoft SQL Server Management Studio中新建一个查询,输入并执行上面的脚本,然后在Microsoft SQL Server Management Studio的“对象资源管理器”中查看MYTB表以及其中的数据。 (2)用下面的脚本查询Employee表中的雇员信息,包括EmployeeID和Gender,Gender的属性根据其值相应地显示为‘男’或‘女’。 USE AdventureWorks SELECT EmployeeID,Gender= CASE Gender WHEN ‘M’ THEN ‘Male’ WHEN ‘F’ THEN ‘Female’ END FROM HumanResources.Employee 在Microsoft SQL Server Management Studio中新建一个查询,输入并执行上面的脚本,观察执行结果。 (3)下面的脚本显示了T-SQL中的错误处理。

创建带有游标的存储过程sql语句

游标中用到的函数,就是前一篇文章中创建的那个函数。 另外,为了方便使用,把游标放在存储过程中,这样就可以方便地直接使用存储过程来执行游标了。 1create procedure UpdateHKUNo --存储过程里面放置游标 2as 3begin 4 5declare UpdateHKUNoCursor cursor--声明一个游标,查询满足条件的数据 6for select psn_code from person where type='E'and hku_no is null 7 8open UpdateHKUNoCursor --打开 9 10declare@noToUpdate varchar(20) --声明一个变量,用于读取游标中的值 11fetch next from UpdateHKUNoCursor into@noToUpdate 12 13while@@fetch_status=0--循环读取 14begin 15--print @noToUpdate 16update person set hku_no=dbo.GetExtUserHKUNo() where psn_code=@noToUpdate 17fetch next from UpdateHKUNoCursor into@noToUpdate 18end 19 20close UpdateHKUNoCursor --关闭 21 22deallocate UpdateHKUNoCursor --删除 23 24end 25 26--exec UpdateHKUNo 另外,判断数据库中是否存在某一存储过程(Sqlserver 2000): if exists (select*from sysobjects where name='UpdateHKUNo'and xtype ='P') print'yse' else print'no'

存储过程和游标详解

什么是存储过程呢? 定义: 将常用的或很复杂的工作,预先用SQL语句写好并用一个指定的名称存储起来, 那么以后要叫数据库提供与已定义好的存储过程的功能相同的服务时,只需调用execute,即可自动完成命令。 讲到这里,可能有人要问:这么说存储过程就是一堆SQL语句而已啊? Microsoft公司为什么还要添加这个技术呢? 那么存储过程与一般的SQL语句有什么区别呢? 存储过程的优点: 1.存储过程只在创造时进行编译,以后每次执行存储过程都不需再重新编译,而一般SQL语句每执行一次就编译一次,所以使用存储过程可提高数据库执行速度。 2.当对数据库进行复杂操作时(如对多个表进行Update,Insert,Query,Delete时),可将此复杂操作用存储过程封装起来与数据库提供的事务处理结合一起使用。 3.存储过程可以重复使用,可减少数据库开发人员的工作量 4.安全性高,可设定只有某此用户才具有对指定存储过程的使用权存储过程的种类: 1.系统存储过程:以sp_开头,用来进行系统的各项设定.取得信息.相关管理工作, 如sp_help就是取得指定对象的相关信息 2.扩展存储过程以XP_开头,用来调用操作系统提供的功能 exec master..xp_cmdshell 'ping 10.8.16.1' 3.用户自定义的存储过程,这是我们所指的存储过程 常用格式

Create PRocedure procedue_name [@parameter data_type][output] [with]{recompile|encryption} as sql_statement 解释: output:表示此参数是可传回的 with {recompile|encryption} recompile:表示每次执行此存储过程时都重新编译一次encryption:所创建的存储过程的内容会被加密 如: 表book的内容如下 编号书名价格 001 C语言入门$30 002 PowerBuilder报表开发$52 实例1:查询表Book的内容的存储过程 create proc query_book as select * from book go exec query_book 实例2:加入一笔记录到表book,并查询此表中所有书籍的总金额

游标实例与返回结果集的存储过程

游标实例: declare cursoremp_cursor(dept_num number:=20) is selectename,sal from emp where deptno=dept_num; v_enameemp.ename%type; v_salemp.sal%type; --可以使用下面的定义方法,代替上面两个变量 --one_empemp_cursor%rowtype; begin openemp_cursor(&dnum); loop fetchemp_cursor into v_ename,v_sal; --判断工资低于2000,增加工资 --if(v_sal<2000) --then update emp set sal=sal*(1.1) where ename=v_ename; --end if; exit when emp_cursor%NOTFOUND; dbms_output.put_line('当前检索的是第'||emp_cursor%rowcount||'行:'||v_ename||','||v_sal); end loop; closeemp_cursor; end; declare --定义游标sp_emp_cursor typesp_emp_cursor is ref cursor; --定义一个游标变量 test_cursorsp_emp_cursor; --定义变量 v_enameemp.ename%type; v_salemp.sal%type; begin --执行 --把test_cursor和一个select结合 opentest_cursor for select ename,sal from emp where deptno=&no; --循环取出 loop fetchtest_cursor into v_ename,v_sal; --判断是否test_cursor为空 exit when test_cursor%notfound; dbms_output.put_line('名字:'||v_ename||' 工资:'||v_sal); end loop;

实验七 游标的使用和存储过程

实验七游标的使用及存储过程的创建 1,实验目的 使同学加深对游标概念的理解,掌握游标的定义,使用方法及使用游标修改和删除数据的方法。使学生理解存储过程的概念,掌握创建存储过程的的使用,执行存储过程和查看、修改、删除存储过程的方法 2,实验内容 (1)、利用游标逐行显示所查询的数据块的内容 (2)、利用游标显示指定行的数据的内容 (3)、利用游标修改和删除指定的数据元组 (4)、创建存储过程 //(5)、修改存储过程 (6)、调用存储过程 (7)、删除存储过程 3、实验步骤-----游标主题 1)在student表中,定义一个包含sno,sname,age,sex,dept的只读游标,游标的名称称为cs_cursor,并将游标中的数据逐条显示出来。 (1)在数据库引擎上查询文档中输入如下代码: use学生选课 declare cs_cursor scroll cursor for select sno,sname,age,sex,dept from student for read only open cs_cursor fetch from cs_cursor (2)单击“执行”按钮,运行结果 (3)接着读取游标中的第二行,在查询编辑器重输入如下语句: fetch from cs_cursor (4)连续单击“执行”按钮,就可以逐条显示记录 (5)最后关闭游标、释放游标。 注意:游标中定义的参数scroll是说明可以用所有的方法来存取数据,允许删除和更新 Prior,first,last,absolute n,relative n选项只有在定义游标时并使用了scroll选项后才可以使用。其中N是正数时,返回结果集的第N行,若N是负数,则返回结果集倒数第N行 实验内容一: 1)在student 表中定义一个所在系为“计算机系”,包含sno,sname,sex,age,dept的游标,游标的名称为cs_cursor,完成如下操作 use zz declare cs_cursor scroll cursor for select sno,sname,sage,sdept from student for read only open cs_cursor

DB2 中游标的使用以及 存储过程的写法

什么时候才会发生not found异常,以及db2中sqlcode的值是如何变化的? 在db2中,一条select 语句也有可能发生not found异常,譬如 declare sqlcode integer default 0; declare sql_code integer default 0; declare classCode varchar(40) ; select app_class_code into classCode from kf_app_class where app_name='无效记录'; set sql_code=sqlcode; 如果此时没有检索到记录,那么sqlcode的值为100,有的话为0; 我们可以定义NOT FOUND 异常处理 declare sqlcode integer default 0; declare sql_code integer default 0; declare classCode varchar(40) ; begin declare continue handler for not found begin --注如果发生not found那么此时的sqlcode必定为100 set sql_code=sqlcode;/*在这里sqlcode的值为100;*/ --如果再次得到sqlcode的值那么它的值变为0 set sql_code=sqlcode;/*这里sqlcode变成了0,因为上一条语句执行成功了,那么sqlcode变成了0*/ end; select app_class_code into classCode from kf_app_class where app_name='无效记录'; set sql_code=sqlcode;/*同理此时如果没有取到数据,那么会进declare continue handler ,返回后sqlcode的值也为0*/ end; 所以我们可以通过两种方法来捕获和处理not found 方法1: begin declare continue handler for not found begin --异常处理代码 end; sql语句 end; 方法2: begin sql语句 if sqlcode=100 then --异常处理代码

数据库原理实验-游标与存储过程

?建立EPoem表和CPoem表,并插入数据EPoem

?显示“英文诗” ?依次显示EPoem的所有记录 ?显示一个空行 ?显示“中文诗” ?依次显示CPoem的所有记录 代码: create table czk_EPoem ( LineText char(1000) not null ) create table czk_CPoem ( LineText char(100) not null ) insert into czk_EPoem values('The furthest distance in the world, is not between life and death') insert into czk_EPoem values('But when I stand in front of you, yet you don’t know that I love you') insert into czk_EPoem values('The furthest distance in the world, is not when I stand in front of you, yet you can’t see my love') insert into czk_EPoem values('But when undoubtedly knowing the love from both, yet cannot be together') insert into czk_EPoem values('The furthest distance in the world, is not being apart while being in love') insert into czk_EPoem

写存储过程中涉及到一些游标的问题 六

写存储过程中涉及到一些游标的问题六 写存储过程中涉及到一些游标的问题收藏 我记得在我上学的时候,学PB编程的时候,乔老师跟我们说了很多关于这方面的很多内容,说游标很有用,我一直都没有这么觉得,原来是我上学时做的东西太简单,只是把功能实现,访问数据量小,涉及的数据库数据少.还好那时听老师的,认真地学了一下,要不然现在肯定很后悔了.编程语言的相通,让我学习ORACLE这方面的数据库编程更容易上手. SQL是用于访问ORACLE数据库的语言,PL/SQL扩展和加强了SQL的功能,它同时引入了更强的程序逻辑。PL/SQL支持DML命令和SQL的事务控制语句。DDL 在PL/SQL中不被支持,这就意味作在PL/SQL程序块中不能创建表或其他任何对象。较好的PL/SQL程序设计是在PL/SQL块中使用象DBMS_SQL这样的内建包或执行EXECUTE IMMEDIATE命令建立动态SQL来执行DDL命令,PL/SQL编译器保证对象引用以及用户的权限。 下面我们将讨论各种用于访问ORACLE数据库的DDL和TCL语句。 查询 SELECT语句用于从数据库中查询数据,当在PL/SQL中使用SELECT语句时,要与INTO子句一起使用,查询的返回值被赋予INTO子句中的变量,变量的声明是在DELCARE中。SELECT INTO语法如下: SELECT [DISTICT|ALL]{*|column[,column,...]} INTO (variable[,variable,...] |record) FROM {table|(sub-query)}[alias] WHERE............ PL/SQL中SELECT语句只返回一行数据。如果超过一行数据,那么就要使用显式游标(对游标的讨论我们将在后面进行),INTO子句中要有与SELECT子句中相同列数量的变量。INTO子句中也可以是记录变量。 %TYPE属性 在PL/SQL中可以将变量和常量声明为内建或用户定义的数据类型,以引用一个列名,同时继承他的数据类型和大小。这种动态赋值方法是非常有用的,比

相关主题