触发器、游标
一、觸發(fā)器
? ? 觸發(fā)器的兩張?zhí)摂M表:
????
???? Tickets表原始數(shù)據(jù):
?? ??
???? TicketsOrders表原始數(shù)據(jù):
?????
?? 1.插入觸發(fā)器
????? 創(chuàng)建:
1 create trigger ticketsOrdersInsert
2 on edisondb..ticketsOrders
3 after insert
4 ?as
5 ?begin
6 if (select t.currentAmount from tickets as t,inserted
7 where t.ticketid=inserted.ticketid)<
8 (select amount from inserted)
9 begin
10 print '訂票數(shù)超過(guò)庫(kù)存數(shù),無(wú)法訂票!'
11 rollback transaction -----將插入操作進(jìn)行回滾
12 ? end
13 else
14 print '訂票成功!'
15 update tickets set tickets.currentAmount=tickets.currentAmount-inserted.amount
16 from inserted
17 where tickets.ticketid=inserted.ticketid
18 ?end
???? 插入非法值:
2
3 ?insert into ticketsOrders(custid ,ticketid ,amount)
4 values(10001,1,100); ????執(zhí)行結(jié)果:
?? 2.更新觸發(fā)器
???? 創(chuàng)建:?
1create trigger ticketsUpdate
2 on edisondb..Tickets
3 after update
4as
5begin
6 if update(totalAmount)
7 begin
8 if (select totalAmount from deleted)<>(select currentAmount from deleted)
9 begin
10 raiserror('該票已經(jīng)有人訂票,不可更新總票數(shù)',16,1)
11 rollback transaction----回滾
12 end
13 end
14end ??? 更新非法數(shù)據(jù):?
1use edisondb;
2update Tickets set tickets.totalAmount=100
3 where ticketid=1; ?? 執(zhí)行結(jié)果為:?
?? 3.刪除觸發(fā)器
????? 創(chuàng)建:
1create trigger ticketsDelete
2 on edisondb..Tickets
3 after delete
4as
5begin
6 if (select totalAmount from deleted)<>(select currentAmount from deleted)
7 begin
8 print '該票已經(jīng)有人訂購(gòu),不可刪除'
9 rollback transaction----回滾
10 end
11end ????? 非法刪除數(shù)據(jù):?
1use edisondb;
2delete from tickets
3where ticketid=1; ????? 執(zhí)行結(jié)果為:?
二、游標(biāo)
???? 游標(biāo):行級(jí)操作,性能殺手
???? SQL查詢:數(shù)據(jù)集操作,速度快(優(yōu)先選用)
????
???? 使用游標(biāo)進(jìn)行操作:(查出每個(gè)用戶的總運(yùn)費(fèi))
1 set nocount on;2 use edisondb;
3 declare @startDT datetime
4 set @startDT=getdate()
5 declare @CustomerTotalFreight table
6 (
7 custid numeric(18,0),
8 totalFreight numeric(18,2)
9 );
10 declare
11 @currentCustid as numeric(18,0),
12 @prvCutstid as numeric(18,0),
13 @currentFreight as numeric(18,2),
14 @totalFreight as numeric(18,2);
15 declare customerTotalFreight_cursor cursor for ----聲明游標(biāo)
16 select custid,freight
17 from orders
18 order by custid;
19 open customerTotalFreight_cursor ----打開游標(biāo)
20 ----循環(huán)獲取每行記錄
21 fetch next from customerTotalFreight_cursor into @currentCustid,@currentFreight;
22 select @prvCutstid=@currentCustid,@totalFreight=0.00;
23 while @@fetch_status=0
24 begin
25 if @prvCutstid<>@currentCustid
26 begin
27 insert into @CustomerTotalFreight values(@prvCutstid,@totalFreight);
28 select @prvCutstid=@currentCustid,@totalFreight=@currentFreight;
29 end
30 else
31 begin
32 set @totalFreight=@totalFreight+@currentFreight;
33 end
34 fetch next from customerTotalFreight_cursor into @currentCustid,@currentFreight;
35 end;
36 insert into @CustomerTotalFreight values(@prvCutstid,@totalFreight);
37 close customerTotalFreight_cursor; ----關(guān)閉游標(biāo)
38
39 deallocate customerTotalFreight_cursor;----釋放游標(biāo)
40
41 select * from @CustomerTotalFreight order by custid;
42
43 print '耗時(shí):'+replace(str(datediff(ms,@startDT,getdate())),' ','')+' 毫秒' ?? 執(zhí)行結(jié)果為:
??
?? 以上功能如果用SQL查詢來(lái)實(shí)現(xiàn)則十分簡(jiǎn)潔:
1 use edisondb;
2 declare @startDT datetime
3 set @startDT=getdate()
4 select custid,sum(freight) as totalFreight
5 from orders
6 group by custid;
7 print '耗時(shí):'+replace(str(datediff(ms,@startDT,getdate())),' ','')+' 毫秒' ?? 執(zhí)行結(jié)果為:
??
轉(zhuǎn)載于:https://www.cnblogs.com/edisonfeng/archive/2011/07/11/2102672.html
總結(jié)
- 上一篇: 几个常用的宏:likely和unlike
- 下一篇: 如何发送Head请求