释放双眼,带上耳机,听听看~!
触发器是一种特殊的存储过程,下面通过触发器实现对银行存取款的操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 1---创建触发器
2create trigger tr1 on saveinfo after
3insert
4AS
5BEGIN
6declare @uid nvarchar(50)
7declare @type int
8declare @qian float
9declare @yuer float
10select @uid = uid, @type=[type],@qian=[money] from saveinfo
11select @yuer= [money] from [userinfo]
12where uid = @uid
13if(@type = 1) --1表示存款
14begin
15 update userinfo set [money] = [money] + @qian where uid = @uid
16end
17else
18begin
19 if(@yuer < @qian)
20 begin
21 print '余额不足'
22 end
23 else
24 begin
25 update userinfo set [money] = [money] - @qian where uid = @uid
26 end
27end
28END
29GO
30
31