数据库——存储,触发器

数据库——存储,触发器

/------存储1 根据用户输入该学生编号,检查是否存在,若存在则显示出/

Create procedure p1
@sno char(10) ,@sname char(10),@ssex char(2),@borndate datetime,@classname varchar(50)
As
Begin
If exists(select * from student2 where sno=@sno)
Select * from student2 where sno=@sno
Else
Begin
insert into student2 (sno,sname,ssex,borndate,classname)
values(@sno,@sname,@ssex,@borndate,@classname)
End
Select * from student2 where sno=@sno
end
exec p1 ‘100109’,‘鹬蚌’,‘男’,‘2000-6-8’,‘计算机班’

/-----存储2 如果存在编号为“100112”的学生则将其信息删除,如果不存在,则显示“没有这个学生”/

Create procedure f6
as
begin
if exists(select * from student2 where sno=‘100112’)
begin
delete from student2 where sno=‘100112’
delete from student2 where sno=‘100112’
end
else
print’没有这个学生’
end
exec f6

/-----存储3使用形参名进行传值/

Create procedure g6
@sno char(10),@score char(10),@rank char(10)
As
Begin
If exists(select * from student3 where sno=@sno)
Select * from student3 where sno=@sno
Else
Begin
insert into student3 (sno,score,rank)
values(@sno,@score,@rank)
End
Select * from student3 where sno=@sno
end
Exec g6 @sno=‘100104’,@score=‘86’,@rank=‘4’

/–触发器1删除这个学生的信息,同时删除他的记录/

Create trigger a2
on student2
for delete
as
begin
if exists(select sno from deleted)
begin
delete from student2 where sno=(select sno from deleted)
end
else
print’没有这个学生’
end
Delete from student2 where sno=‘100112’

/—触发器2 当更新这个学生编号的同时,更新这个学生的其他信息/

create trigger q6
On student2
for update
As
begin
if exists(select sno from deleted)
begin
update student2
set sno=(select sno from inserted)
where sno=(select sno from deleted)
end
else
print’找不到这个学生的记录’
end
update student2 set sno='100105’where sno=‘100101’

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轩辕椿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值