實(shí)驗(yàn)四觸發(fā)器實(shí)驗(yàn)
(一)
after
觸發(fā)器
(1 1 )
在l l i neitem 表上定義一個(gè) after 觸發(fā)器, , 當(dāng)修改列項(xiàng)目e e x tendedprice d i scount
x tax 時(shí), , 要把 s orders 表得to o tal pri ce e 一起修改, , 以保證數(shù)據(jù)一致性
C RE ATE T RIGGER
trig _line ite m_ pr ice_ update on line it em fo r upda te
a as
begin i f (UPDATE(ex tend edprice)
o r UPDATE(tax)
or UPD AT E(di scou nt)) begin
-— 聲明游標(biāo)變量指向 inserted 表
d eclare
cursor_inserted c urs or
rea d_only
o for select order key,linenu mber,exte nd edpr ice, dis coun t, tax
from
in ser ted
—-息信找查取獲量變明聲? 聲明變量獲取查找信息
de clare order key in t, @linenumb er
int,exte nd edprice
real,dis scount real,tax real
—- 打開游標(biāo) epo? en cursor_i ns ert ed
—-標(biāo)游取讀? 讀取游標(biāo)
fe tch
next
from cur sor _i ns erte d int o @o rderkey, @lin enumber,e ext ende dprice, @di scount,t ax
w whi le FETC H_S TATUS =0 nigeb? n
? —- 聲明一個(gè)變量保存重新計(jì)算得新價(jià)格 ced?? ecl are @n ew_tota lpri ce
r eal
? select @n ew_t otal price= @ext en dedpr ice *(1 -@di scou nt) *(1 +@tax)
—- 用新得總價(jià)格變量更新 orders 表得 t ot alprice pu?? update orde rs
se t tot alpri ce= new_totalprice where or derkey=orde rkey
en hctef? ext f ro m cur sor_i nser ted int o @order ke y, @li nenum ber, @ex tende dp rice,discoun t, @tax
dne?llaed? locate c ur sor_i nser te d e end end ?(2)在 在 linei tem表上定義一個(gè) aftr er 觸發(fā)器, , 當(dāng)增加一項(xiàng)訂單明細(xì)時(shí), ,
自動(dòng)修改 s orders 表得 total p rice, 以保證數(shù)據(jù)一致性
CREATE TR IGGER tri g_ lineit em_price_ in sert on l ine item f for inse rt
a s begin ——向指量變標(biāo)游明聲? 聲明游標(biāo)變量指向ins erted 表
de clare
c ursor_inse rted
cursor
read_ onl y
? for s ele ct orderkey,linenumbe r,ex te ndedp ric e,di scou nt,t ax
from ins serte d
-- 聲明變量獲取查找信息 ed? eclare @orde rk ey
int, @lin enumber int,e xten dedp rice
real, @discount
real, @ta x real -—? - 打開游標(biāo)
open cursor_i ns erte d --? - 讀取游標(biāo)
fe tc h
nex t
f rom
cu rsor_ins erted
into
@o rd erkey,li nen umbe er r, ex ten ded pric e,dis count,tax ihw? ile @@FE TCH_ STATU S=0 eb? egin
-—格價(jià)新得算計(jì)新重存保量變個(gè)一明聲?? 聲明一個(gè)變量保存重新計(jì)算得新價(jià)格
ced? clare @n ew_tot alprice real
celes? ct new _to ta lprice= @extend edp rice *( 1-d is count) *(1+tax) —-新更量變格價(jià)總得新用?? 用新得總價(jià)格變量更新orders 表得totalpri ce
? u pda te
or ders s et
t ota lpric e=total pric e+ @new_ tota lpr ice w wh he re o rderke y=orderkey
tef? etch next from cursor_ ins erte d into o rder key, @l inen umber,e xtended dp ric e, @disc ou nt, @t ax
e end aed? deall oca te cu rsor_in serted en d
(3)
在l in e ite m表上定義一個(gè)a ft er觸發(fā)器, , 當(dāng)刪除一項(xiàng)訂單明細(xì)記錄時(shí), ,
自動(dòng)修改 orders 表得 tot a lprice, 以保證數(shù)據(jù)一致性
CREATE TRI GG ER trig _line item_price_de let e
on line item fo r de let e
A AS begin
- -聲明游標(biāo)變量指向delet ed 表
de clar e curso r_d eleted cursor
re ad _on ly
es
rof?? ele ct ord erk ey,line numbe r,extende dp rice,discoun t, tax
from
del eted -? -- 聲明變量獲取查找信息
declare ord erkey int,linenum be r
int,extendedp ri ce r eal,discou nt real,ta x real -—? -
打開游標(biāo) epo? en c urso r_ deleted
——標(biāo)游取讀? 讀取游標(biāo) ef? etch next fr om cur sor_delete d in to
order ke y,l inenumb er,ext end dedprice, @di scoun t,tax
wh ile @FETCH_STATUS=0
begi n
-— 聲明一個(gè)變量保存重新計(jì)算得新價(jià)格
? declare @ne w_ to talpric e real
? s selec t @new_t otalpr ice= @exte nde dpri ce*(1- @disco unt) *(1+ tax)
-? -— 用新得總價(jià)格變量更新orders 表得tot alp rice u?? upd at(yī)e
orders set t otalpri ce=to talpr ice- @new _totalp rice wh er e order key= @ord erkey
fetch
n ext f rom cursor_inse rt ed in to orderkey, @line numbe r,extende ed dprice, @d iscou nt,t ax dne? nd
d deal lo cat e cur sor_inse rted e end ( (4 4) ) 驗(yàn)證 up d at e觸發(fā)器
— -查瞧 號訂單得 to ta a l pr i ce
selec t
*
fro o m
o o r ders where
orde r key=1 8 30;
— -查瞧明細(xì)表得相關(guān)信息
se l ect *
f ro m
lin ei i te e m
w here or de e r key=183 0
and l ine num m be e r =1;
—— 驗(yàn)證 e update 觸發(fā)器
updat e
lineitem set t ax=tax+0 、 05
whe re orderkey=1830;
(二)
i i n stead
of
觸發(fā)器
( (1)
)
在 在 lineit em 表上定義一個(gè)ins tead o f upda te觸 發(fā)器, 當(dāng)修改明細(xì)表中得數(shù)量 量quan ti ty 時(shí), 應(yīng)先檢查供應(yīng)表par tsupp 得av ai lqty 就是否足夠, 不足夠則拒絕執(zhí)行, 否則執(zhí)行并修改相應(yīng)數(shù)值以保證數(shù)據(jù)一致性 于 由于 in stead
of 觸發(fā)器更新某個(gè)表會(huì)使得該表上其她不滿足更新列不能更新,因 因用 此逆向思維使用 a fter 觸發(fā)器實(shí)現(xiàn)相同效果 即先更新 qu antity, 再比較av ailq qt ty, 如果滿足更新數(shù)量, 就修改partsupp 得 表得 a vailqty, 如果不滿足, 則把lineitem得quantity 更新之后得數(shù)據(jù)重新修改回來 c create trigge r trig_lin eit em_quanti ty_ upda te
on
li neit em f or upda te
as begin if UPDATE(qu ant ity) b begin ——向指別分量變標(biāo)游明聲? 聲明游標(biāo)變量分別指向 i nserted 表與 d el eted 表 ed? declare c urso r_inser ted
cur sor
r ead_on ly
for
tceles?? t
orde rkey, partk ey,s uppkey,lin enum ber, quantit y fr om i nserted
decl are
cursor_de leted c urs or
rea d_ onl y
f or select quantity
fr om deleted
-—息信找查取獲量變明聲? 聲明變量獲取查找信息
decl are
@qu an tity _dif f_lineitem i nt,q uanti ty_pa rtsup p i nt ced? clare suppkey int, @par tke y in t,o rd erkey i nt,
unenil?? um ber int ,qty _inserted in t , @qty_delete d int
- - 打開游標(biāo) ruc nepo? rsor_in sert ed po? open cur sor_d eleted
-—量變給賦值數(shù)標(biāo)游取讀? 讀取游標(biāo)數(shù)值賦給變量 f? fet ch next from
cu rsor_ insert ed
? i into or de rk ey,pa rtkey, @suppk ey, @line nu mber, @qty _ins erte d
f et ch
next from
cursor _d eleted
int o q ty _de lete d
whi le fe tch_st at(yī)us=0 geb? egin
- -計(jì)算訂單明細(xì)修改時(shí), 訂購數(shù)量得變化值 inserte d表項(xiàng)-d elet ed表項(xiàng)
s sel ect quantity_d if f_ li neit em= @q ty _in se rte d—@ @q ty_delete ed
? --從parts upp 表獲取ava ilq ty值, 注意partsupp 表得主鍵為(partk ey,suppke ey) tceles?? t @quanti ty_p ar tsupp =av ailq ty fro m pa rtsu pp
w?? wh er e suppkey= suppke y
and part key= @par tk ey
-—斷判始開?? 開始判斷
geb? begi n
fi? f quant ity _d iff_ lin eite m=0
?
? p rin t " 更新得數(shù)量與原表中得值相同, 不需要更新"
e ls e if @quantit y_d iff_lin eitem 〈=q uantity_partsupp
?
be gin
? pu?? pd at(yī)e partsupp
s et avail qty= availqty- @qua ntit y_d iff_li ne item
? pus erehw? ppkey=suppkey
and
p artkey= @par tke y
?
? p rint " 兩個(gè)表都更新成功’ ?
??ne? nd
els e
igeb?? in
? u?? update li nei tem
set
quantit y=quantity+ @quanti ty_diff_linei tem
whe re o rd erke y=@orde rke y and li nenu mber= @li ine number
?
p ri nt '更新失敗"
?? end
? e nd ef?? etch ne xt
fr om c urso r_i nserted
i nto @ord erke y, @partkey,s upp key, @linenumb er, @qt y_ ins ert ed
f etch ne xt from curso r_de leted into @qt y_d elet ed dne? nd
d eallocat e cur sor _i nserte d
dealloc at(yī) e cursor_de le ted e end e end (2)在 在 l ineite m表上定義一個(gè) instea d of in sert 觸發(fā)器, 當(dāng)插入明細(xì)表中一條記錄 時(shí) 時(shí), 應(yīng)先檢查供應(yīng)表par tsupp 得 得 ava il qt y就是否足夠 qu anti ty 得數(shù)量 c rea te t rig ger tri g_lineitem_q ua nti ty_ insert
on
line item i instead of inser t as b begin
-— 聲明游標(biāo)變量指向 inserte d表
d eclar e cur sor_inserte d cur sor
rea d_ only f or
s?? select or derk ey,pa rtkey,sup pk ey, lin en umber,q uantity
f rom ins er ted
-—
聲明變量獲取查找信息
dec lare quantity int, @av ailq ty i nt, @suppkey
in t, @partkey
in nt t, @o rderkey int, @linenu mber int
-—標(biāo)游開打? 打開游標(biāo) c nepo? curs or_ins erted -? -— 讀取游標(biāo)
f etc h next fro m cursor_insert ed int o @orde rkey,partkey,@ @s suppkey, @linenumber,qu antity
wh ile @@FETCH_S TATUS= 0 igeb? in
-- 為變量賦值
a tceles? availqty y
= =av ai lqt y fr om
partsupp
w? whe re suppkey =@su ppk ey and part ke y= partke y
? if @quant ity 〈= @avail qt y
-— 如果可以更新
b? begin /??? /*將 將 insert ed 表中得記錄插入到明細(xì)表*/
? sni? sert
i nto l ineite m select *
from i nserted
ro=yekredro
erehw?? rderk ey and
linenumb er = @linenumber */??新更時(shí)同? *同時(shí)更新 part supp 表得數(shù)量*/
?? u upd ate pa rtsup p set a vailqty=availqty- @quanti ty
?
erehw? e sup pke y= @sup pkey and partkey=part key
p pr int ’pa arts upp 表有足夠得貨物可以滿足 lin eitem 得quan tity y, 插入成功’
end
else
begin
?? p rint t
' 'pa rt sup p表沒有足夠得貨物可以滿足 l ineitem 得 得 q uantity,插入失敗’
dne?f?? fetc h next from curso r_ins ert ed in to @ord erkey, partkey, suppkey, @li inenumbe r, qu antity e? en d
deall ocat e cursor_ inserted end ( 3)在 在 line ite m表上定義一個(gè) inste ad of del ete 觸發(fā)器,當(dāng) 當(dāng) 刪除 明細(xì)表中記錄 時(shí) 時(shí), 同時(shí)改變表 供應(yīng)表 partsupp 得ava il qty y 數(shù)值 c re at(yī)e trigge r tri g_ lin eit em_quanti ty_ del ete o n line item inste ad of del ete as be gin
—- 聲明游標(biāo)變量指向deleted 表
de cla re curs or_ del eted c ursor
read _only for
les?? elect or derke y, par tkey,sup pkey, linenumbe r,quan tity fr om deleted -? -- 聲明變量
decl are s upp key i nt, par tke y
int, @orde rkey int,linen umb er
int, @qua ant it y int
—-標(biāo)游開打? 打開游標(biāo)
open
cursor_deleted
-—標(biāo)游取讀? 讀取游標(biāo)
f fetch next fr om
curs or _del et ed
in to @ord erkey , @partkey,s uppke ey, @lin en umber,q ua ntity
whi le
F FET CH_ STATUS=0 igeb? in
*/*除刪? *?jiǎng)h除*/
? de let e from
lineite m where linenu mber= line number and o rde rkey =ord der key
*/新更時(shí)同? 同時(shí)更新 pa rt supp 表得數(shù)量*/
u pdate
parts upp se t
availqt y=a vai lq ty+quant ity
? where s uppke y= @s uppkey a nd par tk ey= @p ar tkey
? p rin t
’ 刪除成功, 并且已經(jīng)把貨物數(shù)量歸還到 p ar tsup p里" f?? fetc h ne xt fr om
curso r_del eted
into @ord er ke y
,p ar tkey,@ @suppkey, @lin enu mber,quanti ty ne? nd ed? ea lloc at(yī)e cursor_ delete d end (4) 驗(yàn)證 update 觸發(fā)器 -- 查瞧li neit em 得quan tit y select *
fr om
lin eit em whe re or derk ey =1830
and li nenum ber=1;
—— 查詢partsupp 表得ava ilqt y se lect * from part supp w here suppk ey =(select
supp key fr om
lin e
item w here ord erkey=18 30)
and partk ey
=(s elec t part key from lin eite m wh er e
order key =18 30 a nd linenu mber=1)
-- - 更新數(shù)量過大
—— 更新得值與原值相同
-- - 更新到+ + 2 00 數(shù)量, , 成功
update
li neitem set
quant ity
=q uanti ty+ 200
where order key=1830
and lin en umber = 1;
-- 更新 +2 00 成功后l ineite m得 quanti ty y 變化
—— 更新+200 成功后par ts upp 表得a va ilqty 變化
實(shí)驗(yàn)到此。所有創(chuàng)建得觸發(fā)器結(jié)果
刪除觸發(fā)器
dr op trigger tri g_li nei tem_de lete; 實(shí)驗(yàn)六存儲(chǔ)過程實(shí)驗(yàn)
(1 1 )
定義無參數(shù) 得存儲(chǔ)過程并執(zhí)行
更新所有訂單總價(jià)
go?
CREATE
PROCED URE Pro c_Ca lTotalP ri ce
AS BEGIN
up date
o rders set to talprice =
(sele ct sum( exte ndedprice*(1-discount)*(1+ tax))
? fr om li nei tem
o
erehw? orders 、orderkey= li neite m。o rderke y) END
go 執(zhí)行此存儲(chǔ)過程
exec P ro c_CalTotal Pric c e; ;
(2 2 )
定義有參數(shù)得存儲(chǔ) 過程并執(zhí)行
更新給定訂單號得總價(jià)
g go create pro cedu re P ro_Cal TotalP rice 4Ord er @id_ order
intege r a as be gin
u up date e
o orders set total price=(
les? lect SU M(ext en ded price*(1 -di sc ount)*(1 +tax))
? fr om li neit em
? whe re
orde rs 。or derke y=lin eite m。orderkey) end g o 執(zhí)行此存儲(chǔ)過程 exe c P ro_ CalTota lPr ice 4Order @id_ order=2; ( (3)
)
定義有局部變量得存儲(chǔ)過程 更新某一個(gè)顧客所有訂單得總價(jià) g o cr eate
procedure Proc_Ca lTot alPr ice4Custo me r n ame _cus to mer varc har( 50) as
dec lare cus tk ey _c usto mer i nt b egin tceles? t
cus tke y_cu stomer=cu st key
from
cu sto mer
where name _customer =RTRI M(n ame)
u pdate order s set to talp ri ce=( tceles?? t S UM(exte nde dp rice *(1 -dis count)*(1+tax)) rf?? rom li neite m
w? wher e orders 、order key=lineitem 。or derke y
? and
o rde rs 、cust key= @c ustkey _cust omer) ?e nd go 執(zhí)行此存儲(chǔ)過程
e xec Pro c_CalT ot alPr ic e4C ustome r name_custom er=" 艾錦亮’; 查瞧結(jié)果 u pdate
linei te m set
ex tende dpric e=1 00 , ta x=0.25 where orderkey="1 830’; s elect * from
l ineite m
w here order key ='183 0’;
s elect * fr om or ders wh ere custke y= (sele ct cus tk ey from custo mer whe re name=’ 艾錦亮’);
(4 )
定義一個(gè)帶輸出參數(shù)得存儲(chǔ)過程 更新某個(gè)顧客得所有得訂單總價(jià), 并輸出總價(jià) go crea te proc edure Proc_C alT otalP ri ce4Custo me r2
na me_ cu st ome r va rch ar( 50), totalpr ice_ord er real out put
as —— 聲明一個(gè)變量存儲(chǔ) name 對應(yīng)得顧客編號 d eclar e @c us tkey _cus tomer integer
—— 為該變量賦值
se lec t c ustke y_cu sto mer =cu stkey
from cus tome r w here n ame_ customer=R TRIM(n ame) begi n -? -- 更改訂單總價(jià)
up date
o rd ers
set t otalp rice=(
se lec t SUM(ext endedprice*(1-dis count)*( 1+tax))
f? fr om l in eitem rehw?? ere
orders 。o rderk ey=li neit em.ord erk ey
、sredro dna? 、c ustkey =@custkey_ custome r)
-— 為返回變量賦值
select
totalprice_order=total pri ce fro m orders
? w here custk ey= @c ustkey_c ustom er
?
e nd go —- 執(zhí)行帶有輸出參數(shù)得存儲(chǔ)過程要聲明輸出參數(shù)變量 declare t otalp rice re al ; exe c Proc_ Cal Tota lPrice4C ustomer2
" 艾錦亮 ', @to talpri ce
output; -- 在屏幕上輸出返回值結(jié)果
selec t t otalpri ce;
實(shí)驗(yàn)到此, , 所有結(jié)果顯示
(5 5 )
修改存儲(chǔ)過程名
exec sp_renam e ’Pr o_Cal TotalPrice4Ord er",
’C alTotalPr ice4 Or der';
(6) 編譯存儲(chǔ)過程
exe c sp_ re compile ’CalTotalPric ce 4Order’;
(7) 刪除存儲(chǔ)過程
d drop
proc edure CalT otalPric e4Orde r; e exe c sp_h elptext CalT ot alPrice4 Order;
推薦訪問: 實(shí)驗(yàn) 報(bào)告 SQL同志們:今天這個(gè)大會(huì),是市委全面落實(shí)黨要管黨、從嚴(yán)治黨要求的一項(xiàng)重大舉措,也是對縣市區(qū)委書記履行基層黨建工作第一責(zé)任人情況的一次集中檢閱,同時(shí)是對全市基層黨建工作的一次再部署、再落實(shí)的會(huì)議。前面,**
***年,我認(rèn)真履行領(lǐng)班子、帶隊(duì)伍、抓黨員、保穩(wěn)定的基層黨建工作思路,以學(xué)習(xí)貫徹習(xí)近平新時(shí)代中國特色社會(huì)主義思想和黨的十九大歷次全會(huì)精神為主線,以市局基層黨建工作考核細(xì)則為落腳點(diǎn),落實(shí)全面從嚴(yán)治黨主體
根據(jù)會(huì)議安排,現(xiàn)將2022年履行抓基層黨建工作職責(zé)情況報(bào)告如下:一、履職工作特色和亮點(diǎn)1 突出政治建設(shè),著力在思想認(rèn)識上提高。牢固樹立抓黨建就是抓政績的理念,以“黨建工作抓引領(lǐng)、社區(qū)治理求突破,為民服
2022年以來,在**黨委的正確領(lǐng)導(dǎo)下,堅(jiān)持以習(xí)近平新時(shí)代中國特色社會(huì)主義思想為指導(dǎo),深入學(xué)習(xí)宣傳貫徹黨的二十大精神,以黨建工作為統(tǒng)領(lǐng),扎實(shí)開展夯實(shí)“三個(gè)基本”活動(dòng),以“四化四力”行動(dòng)為抓手,聚力創(chuàng)建
各位領(lǐng)導(dǎo),同志們:根據(jù)會(huì)議安排,現(xiàn)就2022年度抓基層黨建工作情況匯報(bào)如下:一、主要做法及成效(一)強(qiáng)化政治引領(lǐng)。一是不斷強(qiáng)化理論武裝。堅(jiān)持通過黨組會(huì)、中心組學(xué)習(xí)會(huì)和“三會(huì)一課”,第一時(shí)間、第一議題學(xué)
2022年度抓基層黨建工作述職報(bào)告按照黨委工作部署,現(xiàn)將本人2022年度抓基層黨建工作情況報(bào)告如下:一、2022年度抓基層黨建工作情況(一)旗幟鮮明講政治將旗幟鮮明講政治放在全局發(fā)展首要位置,積極開展
2022年,是我在數(shù)計(jì)系黨總支書記這個(gè)新崗位上度過的第一個(gè)完整的工作年度。回首一年來在校黨委的正確領(lǐng)導(dǎo)下,與數(shù)計(jì)系領(lǐng)導(dǎo)班子和全體師生共同走過的日子,艱辛歷歷在目,收獲溫潤心田。作為黨總支書記,我始終牢
按照考核要求,現(xiàn)將本人一年來,作為統(tǒng)戰(zhàn)部長履行職責(zé)、廉潔自律等方面情況報(bào)告如下:一、著眼增強(qiáng)政治素質(zhì),不斷深化理論學(xué)習(xí)堅(jiān)持把旗幟鮮明講政治作為履職從政的第一位要求,帶領(lǐng)統(tǒng)戰(zhàn)系統(tǒng)干部堅(jiān)決擁護(hù)“兩個(gè)確立”
**年,緊緊圍繞黨工委、管委會(huì)的決策部署,全體人員團(tuán)結(jié)協(xié)作、凝心聚力,緊扣黨工委“**”基本工作思路,全力開拓進(jìn)取,認(rèn)真履職盡責(zé),圓滿完成各項(xiàng)工作任務(wù)。一、個(gè)人思想政治狀況檸檬文苑www bgzjy
按照縣委關(guān)于開展抓基層黨建述職評議會(huì)議的有關(guān)要求,經(jīng)請示縣委組織部同意,今天,我們在此召開2022年度基層黨組織書記抓基層黨建述職評議會(huì)議。1 首先,請**黨委書記,**同志述職。**黨委能夠主動(dòng)研究