当前位置:网站首页>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
边栏推荐
- 【C#】overide可重写从更高父级继承来的virtual方法
- Mmcv Config class introduction
- "The specified database user/password combination is rejected..." Solutions for
- C use s7 Net connected to Siemens s1200plc, C # directly connected to Siemens PLC
- Array rotates the array from bits of the specified length
- leetcode 130. Surrounded Regions 被围绕的区域(中等)
- Back to table query of MySQL? How to avoid it?
- How to stimulate the vitality and driving force of cultural innovation
- ICML2022 | Sharp-MAML:锐度感知的模型无关元学习
- Bitwise and shift operators
猜你喜欢

Latex error: file ‘xxx.sty‘ not found

鲸会务智慧景区管理解决方案
![[tcapulusdb knowledge base] tcapulusdb transaction management introduction](/img/cd/dc23cb8bddcbf04efeaf60dfadaec9.png)
[tcapulusdb knowledge base] tcapulusdb transaction management introduction

Mysql 什么是聚集索引和非聚集索引?
![[MySQL] summary of common data types](/img/96/010c21f0aa7b443c130c5f55e5277a.png)
[MySQL] summary of common data types

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

datagrip 报错 “The specified database user/password combination is rejected...”的解决方法

【TcaplusDB知识库】TcaplusDB推送配置介绍
![[tcapulusdb knowledge base] tcapulusdb viewing online operation](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb viewing online operation

(十一)TableView
随机推荐
TcaplusDB君 · 行业新闻汇编(三)
Part 7: Lesson 2 general skills of consultants - how to install and uninstall SAP ERP system client
Icml2022 | sharp maml: model independent meta learning for sharpness perception
【TcaplusDB知识库】TcaplusDB TcapDB扩缩容介绍
Solution de gestion de la zone pittoresque intelligente pour la réunion des baleines
oc swift 混编
SQL Server查询区分大小写
Notes (IV) - multithreading
【TcaplusDB知识库】TcaplusDB查看线上运行情况介绍
很流行的状态管理库 MobX 是怎么回事?
笔记(四)- 多线程
[MySQL] summary of common data types
TcaplusDB君 · 行业新闻汇编(五)
C language - quick sorting in sorting
Error parsing mapper XML
C program example 1 -- personal address book management system
笔记(二)
Record (III)
torch_ geometric
"The specified database user/password combination is rejected..." Solutions for