存储过程:
存储过程(stored procedure)有时也称为sproc。存储过程存储于数据库中而不是在单独的文件中,有输入参数、输出参数以及返回值等。
在数据库中,创建存储过程和创建其他对象的过程一样,除了它使用的AS关键字外。存储过程的基本语法如下:
create proc hah
as
begin
end
go
exec hah
触发器:
在 deleted 和 inserted 表保存了可能会被用户更改的行的旧值或新值。
对于INSERT 操作,inserted保留新增的记录,deleted无记录
对于DELETE 操作,inserted无记录,deleted保留被删除的记录
对于UPDATE操作,inserted保留修改后的记录,deleted保留修改前的记录
例题:
1 create database chaoshiguanlixitong 2 go 3 use chaoshiguanlixitong 4 go 5 create table gongyingshang 6 ( 7 gcode int primary key identity(1001,1), 8 gname varchar(20), 9 gtel varchar(20), 10 gadd varchar(50) 11 ) 12 create table cangku 13 ( 14 ccode int primary key identity(100001,1), 15 cname varchar(20), 16 cshu int, 17 cjin decimal(18,2), 18 cgcode int 19 ) 20 create table chaoshi 21 ( 22 cccode int primary key, 23 ccname varchar(20), 24 ccshu int, 25 cshou decimal(18,2) 26 ) 27 go 28 insert into gongyingshang values('供应商a','12345678901','地址a') 29 insert into gongyingshang values('供应商b','12345678902','地址b') 30 insert into gongyingshang values('供应商c','12345678903','地址c') 31 insert into gongyingshang values('供应商d','12345678904','地址d') 32 insert into gongyingshang values('供应商e','12345678905','地址e') 33 go 34 insert into cangku values('商品a',1000,18.5,1001) 35 insert into cangku values('商品b',2000,14.5,1002) 36 insert into cangku values('商品c',3000,12.5,1003) 37 insert into cangku values('商品d',4000,17.5,1004) 38 insert into cangku values('商品e',5000,10.5,1005) 39 go 40 insert into chaoshi values(100001,'商品a',100,100.5) 41 insert into chaoshi values(100002,'商品b',200,50.5) 42 insert into chaoshi values(100003,'商品c',50,200.5) 43 insert into chaoshi values(100004,'商品d',400,1000.5) 44 insert into chaoshi values(100005,'商品e',1000,80) 45 go 46 --1.跟苹果公司建立了合作,标号为1006,电话是88888888,地址是美国 47 insert into gongyingshang values('苹果','88888888','美国') 48 go 49 --2.新进iPhone6s和iPad5 各十部,进价3000,3500,拿到超市各两部,售价4000,4500 50 insert into cangku values('iPhone6s',8,3000,1006) 51 insert into cangku values('iPad5',8,3500,1006) 52 go 53 insert into chaoshi values(100006,'iPhone6s',2,4000) 54 insert into chaoshi values(100007,'iPad5',2,4500) 55 go 56 --3.卖掉可口可乐10瓶, 57 --(需要判断超市的货够不够,若不够加上仓库的货看看够不够, 58 --若够就卖掉,拿2瓶放在超市,若不够有多少卖掉多少,通知可口可乐公司进货) 59 insert into gongyingshang values('可口可乐','9999999999','美国') 60 insert into cangku values ('可口可乐',0,1,1007) 61 insert into chaoshi values(100008,'可口可乐',3,5) 62 go 63 declare @a int 64 declare @b int 65 declare @c int 66 select @a = ccshu from chaoshi where cccode = 100008 67 select @b = cshu from cangku where ccode = 100008 68 set @c = @a + @b 69 if @a < 10 70 begin 71 if @c < 12 72 begin 73 print '卖光了没货了要货' 74 update cangku set cshu = 0 where ccode = 100008 75 update chaoshi set ccshu = 0 where cccode = 100008 76 end 77 else 78 begin 79 print '够了可以卖' 80 update cangku set cshu = (@a + @b - 12) where ccode = 100008 81 update chaoshi set ccshu = 2 where cccode = 100008 82 end 83 end 84 else 85 begin 86 print '够了直接卖' 87 update chaoshi set ccshu = @a - 10 where cccode = 100008 88 end 89 go 90 --4.跟1001公司关系破裂,所有有关产品全部退货 91 delete from chaoshi where cccode in (select ccode from cangku where cgcode = 1001) 92 delete from cangku where cgcode = 1001 93 delete from gongyingshang where gcode = 1001 94 go 95 --5.100003号商品新进30个 96 update cangku set cshu = (select cshu from cangku where ccode = 100003) + 30 where ccode = 100003 97 --6.打印小票 98 create table xiaopiao 99 ( 100 cccccode int primary key identity(1000001,1), 101 xname varchar(20), 102 xnum int, 103 danjia decimal(18,2), 104 zongjia decimal(18,2) 105 ) 106 go 107 create proc dayin 108 @shu int, 109 @hao int 110 as 111 begin 112 declare @a int 113 declare @b int 114 declare @c varchar(20) 115 declare @d decimal(18,2) 116 declare @e decimal(18,2) 117 select @a = ccshu from chaoshi where cccode = @hao 118 select @b = cshu from cangku where ccode = @hao 119 select @c = ccname from chaoshi where cccode = @hao 120 select @d = cshou from chaoshi where cccode = @hao 121 set @e = @d * @shu 122 if @a + @b < @shu 123 begin 124 print '货不够' 125 end 126 else 127 begin 128 insert into xiaopiao values(@c,@shu,@d,@e) 129 end 130 end 131 go 132 exec dayin 100,100002 133 select * from xiaopiao