数据库实验九 存储过程(新)

use province;
 #代码开始
 #定义过程
DELIMITER //
CREATE PROCEDURE qxxx(in city varchar(10),in district varchar(10))
BEGIN
declare x int;
declare jd int;
declare z int;
declare qt int;
select count(name)  from jdxx  where cs=city and qxmc=district and name like '%乡' into x;
select count(name)  from jdxx  where cs=city and qxmc=district and name like '%街道' into jd;
select count(name)  from jdxx  where cs=city and qxmc=district and name like '%镇' into z;
select count(name)  from jdxx  where cs=city and qxmc=district and name not like '%镇' and name not like '%街道' and name not like '%乡'into qt;
select x as "乡",jd as "街道",z as "镇",qt as "其他";
END//
DELIMITER ;
 #调用过程
 call qxxx("长沙市","开福区");
 call qxxx("厦门市","同安区");

 #代码结束
use sale;
 #代码开始
 #定义过程
delimiter ||
create procedure ygyj(in nf  int ,in yf int,in xm varchar(10),out pj varchar(10))
begin
declare jg int;
select sum(sjfk) from xsd join gzry on gzry.gyh=xsd.gyh where year(xsrq)=nf and month(xsrq)=yf  and gyxm=xm into jg;
case
when isnull(jg) then
set pj="无业绩";
when jg<5000  then
set pj="不达标";
when jg<10000  then
set pj="达标";
else
set pj="优秀";
end case;
end
||
delimiter ;

 #调用过程
call ygyj(2015,7,"王雅静",@yj1);
call ygyj(2015,6,"廖秉娴",@yj2);
call ygyj(2015,7,"赵敏",@yj3); 
call ygyj(2015,7,"章伟",@yj4);

 #代码结束
 select @yj1,@yj2,@yj3,@yj4;
use sale;
#代码开始
#函数定义
delimiter ||
create function gkjb( nf int, xm varchar(10))
returns varchar(10)
DETERMINISTIC
begin
declare jg int;
declare pj varchar(10);
select sum(sjfk) from xsd join gk on gk.hyh=xsd.hyh where  name=xm  and year(xsrq)=nf into jg;
case
when isnull(jg) then
set pj="非会员";
when jg<5000  then
set pj="一般会员";
when jg<10000  then
set pj="vip";
else
set pj="超级vip";
end case;
return pj;
end
||
delimiter ;
select name as 姓名,gkjb(2015,name) as 等级 from gk;
use library;
 #代码开始
 #定义过程
delimiter $$
create procedure hs(in sh varchar(8), in dzbh varchar(3), in rq date, out zt varchar(12))
begin
    declare jywh int;
    select count(*) from borrow where txm=sh and dzzh=dzbh and isnull(hsrq) into jywh;
    if jywh=0 then
        set zt = "没有该借阅";
    else
        update borrow set hsrq=rq where dzzh=dzbh and txm=sh and isnull(hsrq);
        update book set zk=1 where txm=sh;
        set zt = "还书成功";
    end if;
end $$
delimiter ;
call hs("P0000001", "001", "2022-5-1", @zt1);
call hs("P0000001", "002", "2022-5-1", @zt2);
#代码结束
select @zt1,@zt2;
select txm, sm, zk from book;
select * from borrow;
use province;
 #代码开始
DELIMITER ||
CREATE PROCEDURE tjdq(in sm varchar(10))
BEGIN
declare flag int default 1;
declare city varchar(10);
declare qx varchar(10);
declare jd int;
declare x int;
declare z int;
declare qt int;
DECLARE dq CURSOR FOR SELECT distinct cs,qxmc from jdxx where sf=sm;
declare continue handler for not found set flag = 0;
delete from dqtj;
OPEN dq;
FETCH dq INTO city,qx;
while flag=1 do
select   count(*) from jdxx where cs=city and qxmc=qx and name like "%街道"  into jd;
select   count(*) from jdxx where cs=city and qxmc=qx and name like "%乡"  into x;
select   count(*) from jdxx where cs=city and qxmc=qx and name like "%镇"  into z;   
select   count(*) from jdxx where cs=city and qxmc=qx and name not like "%镇"  and name not like "%街道" and name not like "%乡" into qt;     
insert into dqtj values(city,qx,x,jd,z,qt);
FETCH dq INTO city,qx;
END while;
CLOSE dq;
END
||
DELIMITER ;
call tjdq("安徽省");


实验5.1 存储过程的建立与使用 一、实验目的 理解存储过程的概念、作用、建立和调用方法。 二、实验原理 使用CREATE PROCEDURE语句创建存储过程,ALTER PROCEDURE语句修改存储过程,DROP PROCEDURE语句删除存储过程存储过程有不带参数的、有带输入参数的、有带输出参数(output)的,还可以有带返回值的。创建好的存储过程可以使用EXEC procedure_name语句执行。 实验5.2 触发器的建立与使用 一、实验目的 理解触发器的概念和作用;了解触发器的分类及触发条件;掌握触发器的定义及应用。 二、实验原理 1、 使用CREATE TRIGGER语句定义触发器,ALTER TRIGGER语句修改触发器,DROP TRIGGER语句删除触发器。 2、 触发器分AFTER/FOR和INSTEAD OF两种类型:AFTER/FOR类型的触发器是在相应的触发语句(insert、delete、update)执行完后被触发的。如果触发语句对应的表上有完整性约束,这些完整性约束必须不违背时,相应的触发语句才能执行,然后才能触发对应的AFTER/FOR类型的触发器。INSTEAD OF类型的触发器会在触发语句(insert、delete、update)执行之前被触发,并取代相应的触发语句。 3、 在表或视图上,每个INSERT、UPDATE或DELETE语句只能创建一个INSTEAD OF类型的触发器,无法为有外键约束且指定为级联删除或级联修改的表创建DELETE或UPDATE语句上的INSTEAD OF 类型的触发器。 SQL SERVER为每个触发器都创建了两个临时表INSERTED表和DELETED表,这两个表的逻辑结构与被触发器作用的表一样,用户可以读取这两个表的内容,但不能对它们进行修改,触发器执行完后,这两个表也会自动删除。当执行INSERT时,INSERTED表中保存要向表中插入的所有行;当执行DELETE时,DELETED表中保存要从表中删除的所有行;当执行UPDATE时,修改前的行保存在DELETED表中,修改后的行保存在INSERTED行中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值