关
--deleted:操作之前的旧表:所有新表与原始的物理表没有关系,只与当前操作的数据有关
if exists(select * from sysobjects where name='tr_grade_insert')
drop trigger tr_grade_insert
go
create trigger tr_grade_insert
on grade after insert
as
print '操作之前的表:操作之前,这一条记录还没有插入,所以没有数据'
select * from deleted
print '操作之后的表:已经成功插入一条记录,所有新表中有一条记录'
select * from inserted
go
--测试:
insert into grade values('aaaaa')
if exists(select * from sysobjects where name='tr_grade_update')
drop trigger tr_grade_update
go
create trigger tr_grade_update
on grade after update
as
print '操作之前的表:存储与这个修改操作相关的没有被修改之前的记录'
select * from deleted
print '操作之后的表:存储这个操作相关的被修改之后 记录'
select * from inserted
go
--测试
update grade set classname=classname+'aa' where ClassId>15
if exists(select * from sysobjects where name='tr_grade_delete')
drop trigger tr_grade_delete
go
create trigger tr_grade_delete
on grade after delete
as
print '操作之前的表:存储与这个修改操作相关的没有被删除之前的记录'
select * from deleted
print '操作之后的表:存储这个操作相关的被删除之后 记录--没有记录'
select * from inserted
go
--测试
delete from grade where ClassId>15
?
?
?
20.存储过程
?
存储过程就相当于c#中的方法
参数,返回值,参数默认值,参数:值的方式调用
在调用的时候有三个对应:类型对应,数量对应,顺序对应
创建语法:
create proc usp_用户自定义名称
对应方法的形参 --(int age, out string name)
as
对应方法体:创建变量,逻辑语句,增加删除修改和查询..return返回值
go
调用语法:
exec 存储过程名称 实参,实参,实参 ...
--获取所有学员信息
if exists(select * from sysobjects where name='usp_getAllStuInfo')
drop proc usp_getAllStuInfo
go
create procedure usp_getAllStuInfo
as
select * from Student
go
--调用存储过程,获取的有学员信息
execute usp_getAllStuInfo
--exec sp_executesql 'select * from Student'
--查询指定性别的学员信息
go
if exists(select * from sysobjects where name='usp_getAllStuInfoBySex')
drop proc usp_getAllStuInfoBySex
go
create procedure usp_getAllStuInfoBySex
@sex nchar(1) --性别 参数不需要declare
as
select * from Student where Sex=@sex
go
--调用存储过程,获取指定性别的学员信息
Exec usp_getAllStuInfoBySex '女'
--创建存储过程获取指定班级和性别的学员信息
go
if exists(select * from sysobjects where name='usp_getAllStuInfoBySexandClassName')
drop proc usp_getAllStuInfoBySexandClassName
go
create procedure usp_getAllStuInfoBySexandClassName
@classname nvarchar(50), --班级名称
@sex nchar(1)='男'--性别 有默认的参数建议写在参数列表的最后
as
declare @classid int ---班级ID
set @classid=(select classid from grade where classname=@classname) --通过参数班级名称获取对应的班级ID
select * from Student where Sex=@sex and ClassId=@classid
go
--执行存储过程获取指定班级和性别的学员信息
--exec usp_getAllStuInfoBySexandClassName '八期班'
exec usp_getAllStuInfoBySexandClassName default, '八期班' --有默认值的参数可以传递default
exec usp_getAllStuInfoBySexandClassName @classname='八期班' --也可以通过参数=值的方式调用
exec usp_getAllStuInfoBySexandClassName @classname='八期班' ,@sex='女'
exec usp_getAllStuInfoBySexandClassName @classname='八期班',@sex='女'
--创建存储过程,获取指定性别的学员人数及总人数
go
if exists(select * from sysobjects where name='usp_getCountBySexandClassName')
drop proc usp_getCountBySexandClassName
go
create procedure usp_getCountBySexandClassName
@cnt int=100 output, --output标记说明它是一个输出参数。output意味着你向服务器请求这个参数的值,那么在执行的时候,服务器发现这个参数标记了output,就会将这个参数的值返回输出
@totalnum int =200output, --总人数
@className nvarchar(50), --输入参数没有默认值,在调用的时候必须传入值
@sex nchar(1)='男'--输入参数有默认值,用户可以选择是否传入值
as
declar