存储与触发器

create procedure BuyFriut @Uid varchar(50), @Fcode varchar(50), @Sl int as begin  declare @kc int,@price float  select @kc=Numbers,@price=Price from Fruit where Ids=@Fcode  if @Sl>@kc  begin   print '库存不足!'  end  else  begin   declare @ye float   select @ye=Account from Login where UserName=@Uid   if @ye>= @price*@Sl   begin    update Fruit set Numbers=Numbers-@Sl where Ids=@Fcode    update Login set Account=Account-@price*@Sl where UserName=@Uid    declare @sj int    set @sj = cast(rand()*10000 as int)        insert into Orders values(@sj,@Uid,GETDATE())    insert into OrderDetails values(@sj,@Fcode,@Sl)     end   else   begin    print '余额不足!'       end  end  end

go --使用存储过程 declare @s int exec  @s = BuyFriut 'wangwu','k001',20 print @s

--删除存储过程 drop proc BuyFriut

--触发器,特殊的存储过程,执行的时间和存储过程不一样,存储过程在调用的时候执行,触发器是在执行某种操作的时候触发执行,相当于C#里面的事件

--inserted表和deleted表,临时表,作用是为了恢复数据

select * from Loginone select * from biandong go --创建触发器 create trigger TR_LOGINONE_DELETE on Loginone for delete --删除的时候执行 --after delete --删除之后执行 --instead of delete --替代执行

as begin  declare @uid varchar(50),@name varchar(50)  select @uid=UserName,@name=Name from deleted  insert into biandong values(@uid,@name,'删除') end go --在对表loginnoe进行删除的时候触发器执行 delete from Loginone where UserName='aaa'

go --instead of触发器 --删除student里面的数据的时候用另外两条语句替代,先删从表再删主表

create trigger TR_STUDENT_DELETE on Student instead of delete as begin  declare @sno varchar(20)  select @sno=sno from deleted  delete from score where sno=@sno  delete from student where sno=@sno end --执行删除的时候触发 delete from student where sno='101' select * from student select * from score select * from course select * from teacher

posted @ 2015-07-31 21:31  疯子霖  阅读(155)  评论(0编辑  收藏  举报