当前位置:网站首页>SQLServer数据库应用与开发:第九章上机
SQLServer数据库应用与开发:第九章上机
2022-06-10 21:22:00 【小白白中之白】
代码及解释
9.1:
create procedure StuInfo3
as
select studentno,sname,birthdate,phone
from student
where studentno like '18%'
go
exec StuInfo3
9.2
create procedure ScoreInfo
as
select student.studentno,sname,sex,cname,final
from student,course,score
where student.studentno=score.studentno and score.courseno=course.courseno
go
exec ScoreInfo
9.3
if exists(
select name from sysobjects
where name= 'stu_age' and type= 'p'
)
drop procedure stu_Age
go
create procedure stu_Age
@studentno nvarchar(10),@age int output
as
declare @errorvalue int
set @errorvalue=0
select @age=year(GETDATE())-year(birthdate)
from student
where studentno=@studentno
if(@@ERROR<>0)
set @errorvalue=@@ERROR
return @errorvalue
go
declare @average int
exec stu_age '1',@age=@average output
select @average
- procedure相当于函数,有两个参数,在函数体可以对参数进行设置,代码中设置了@age=年龄,便于返回;@studentno用于匹配
- @@error不等于0说明出错了,返回的是错误号
- @average用于接收返回来的@ave
- type='p’表明为prodecure
9.4
create trigger stu_insert
on student
after insert
as
print '你插了一条新纪录'
go
9.5
CREATE TRIGGER TR_ScoreCheck
ON score
FOR INSERT, UPDATE
AS
IF UPDATE(final )
PRINT 'AFTER触发器开始执行……'
BEGIN
DECLARE @ScoreValue real
SELECT @ScoreValue=(SELECT final FROM inserted)
IF @ScoreValue>100 OR @ScoreValue<0
PRINT '输入的分数有误,请确认输入的考试分数!'
END
GO
- insert和update语句激活相应的触发器之后,所有被添加或被更新的记录都被存到inserted表中
- delete和update语句激活相应的触发器之后,所有被删除的记录都被存到deleted表中
- for相当于after
9.6
if exists(select name from sysobjects where name='dela' and type='tr')
drop trigger dela
go
create trigger dela
on course
after delete
as
print'不能删除'
- type='tr’表示为trigger
边栏推荐
猜你喜欢

Kdd2022 | neural network compression of depth map based on antagonistic knowledge distillation

Solution de gestion de la zone pittoresque intelligente pour la réunion des baleines

【TcaplusDB知识库】TcaplusDB查看线上运行情况介绍

ICML2022 | Sharp-MAML:锐度感知的模型无关元学习

Visio 转为高质量PDF
![[tcapulusdb knowledge base] tcapulusdb refresh tbus channel introduction](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb refresh tbus channel introduction

【TcaplusDB知识库】TcaplusDB引擎参数调整介绍

GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.

Sealem finance builds Web3 decentralized financial platform infrastructure

Diablo immortality database station address Diablo immortality database website
随机推荐
[MySQL] Table constraints
【TcaplusDB知识库】TcaplusDB TcapDB扩缩容介绍
Model construction of mmdetection
【Debug】could not find ref wiht poc XXX解决
A number that appears only once in an array
C language to judge whether a file or folder exists
【TcaplusDB知识库】TcaplusDB TcapProxy扩缩容介绍
Pytorch 安装超简单
C#使用S7.net连接西门子S1200PLC,C#直接连接西门子PLC
Add, delete, query and modify [MySQL] table data (DML)
Mmcv Config class introduction
torch_geometric
【TcaplusDB知识库】TcaplusDB刷新tbus通道介绍
(十一)TableView
(11) Tableview
Whether there are duplicate elements in the array
[tcapulusdb knowledge base] Introduction to tcapulusdb push configuration
Has the samesite cookie problem occurred when using identityserver?
Array intersection of two arrays II
Ability to deliver packages within D days [abstract class dichotomy -- Abstract judgment method]