存储过程和触发器
---恢复内容开始---
存储过程:保存代码,直接调用
create proc jiafa
--需要的 参数
@a int
@b int
as
--存储过程的内容
declare @c int
set @c=@a+@b
return @c
go
执行 之后就 保存了 在可编程性-存储过程
调用 exec
declare @f int
exec @f=jiafa3,5
print @f
用处:
--根据用户传入的参数查询汽车表符合该条件的汽车数量
create proc chaxun
@n varchar(20)
as
declare @num int
select @num= count(*) from car where name like '%'+@n+'%'
return @num
go ---执行,保存
调用:
declare @m int
exec @m=chaxun'奥迪'
print @m
触发器:
是一个特殊的存储过程
通过增删改的动作来触发执行,没有参数,没有返回值
create trigger insert_student---命名规范 在student表中添加数据时触发的
on student --针对于哪一个表
for insert --针对于哪一个动作来触发
as
触发执行的代码段
go
触发器有两种:
1、for insert 意思是在添加执行之后触发
2、instead of delete 替换执行:替换了删除操作成了as...go里的代码
delecte from info where code='p001'
create trigger delete_info
on info
instead of delete
as
declare @c varchar(20)
select @c=code from deleted
delete from work where infocode=@c
delete from family where infocode=@c
delete from info where code=@c
go
表 --触发器
执行:delete from info where code='p001'
for:
create trigger delete_nation
on nation
for delete
as
insert into nation values('n010','回族')
go
delete from nation where code='n002'
---恢复内容结束---
转载自:https://www.cnblogs.com/yp11/p/5746544.html