30
1

THỦ TỤC LƯU TRỮ VÀ TRIGGER

  • Upload
    avery

  • View
    85

  • Download
    0

Embed Size (px)

DESCRIPTION

THỦ TỤC LƯU TRỮ VÀ TRIGGER. Kiểu dữ liệu Char, VarChar, Nchar, NvarChar Bit, TinyInt, SmallInt, Int, BigInt Float, SmallMoney, Money, Real DateTime Text, Image, XML Toán tử : + - * / %. Căn bản ngôn ngữ. Cú pháp khai báo declare @tên_biến kiểu_dữ_liệu [,… n] Gán giá trị - PowerPoint PPT Presentation

Citation preview

  • *

  • Kiu d liu Char, VarChar, Nchar, NvarChar Bit, TinyInt, SmallInt, Int, BigIntFloat, SmallMoney, Money, RealDateTimeText, Image, XMLTon t : + - * / %

    *

  • C php khai bodeclare @tn_bin kiu_d_liu [, n]Gn gi tr set @tn_bin = gi_tr | biu_thc | @bin | hm

    VD: declare @x int, @y intset @y = 5set @x = @y + 3

    *

  • Cu trc lnh IFif (iu_kin) lnh . | khi_lnhelselnh . | khi_lnhkhi_lnh := beginlnh | khi_lnhend*

  • Cu trc lnh WHILEwhile (iu_kin) lnh | khi_lnh

    Lnh ngt vng lpbreakcontinue*

  • Bin Cursordeclare tn_bin_cursor cursor for cu_truy_vn S dngopen tn_bin_cursor.close tn_bin_cursorHy cursordeallocate tn_bin_cursor*

  • Di chuyn Cursorfetch nh_v from tn_bin_cursorinto @tn_bin [, n]

    nh_v := next | prior | last | first |absolute (gi_tr | bin) relative (gi_tr | bin)

    *

  • Trng thi Cursor@@fetch_status =0 :ang trong dng d liu(ln i k tip thnh cng)0:Ngoi dng d liu (ln i k tip khng thnh cng)*

  • V d Tnh tng s chn t 1 -> 100Declare @t int, @x intSet @t = 0 ; Set @x = 0While (@x
  • V d In cc sinhvien(masv char(5),tensv char(10))Declare sv cursor for select * from sinhvienOpen svDeclare @ma char(5),@ten char(10)Fetch next from sv into @ma,@tenWhile (@@fetch_status = 0) beginprint @ma + : + @ten Fetch next from sv into @ma,@tenendClose sv; Deallocate sv

    *

  • To lp th tccreate procedure tn_th_tc@tn_tham_s kiu_d_liu loi[,n]aslnh | khi_lnhTrong :loi := input(khng cn ghi)output*

  • Thc thi th tcexec tn_th_tc gi_tr | @bin [output] [,n] Xa th tc Drop procedure tn_th_tcThay i th tcAlter procedure tn_th_tc ..*

  • V d Vit th tc xa cc sinh vin theo thnh phsinhvien (masv char(5), tp char(5))

    create procedure xoasinhvien @tp char(5)asbegindelete from sinhvien where tp = @tpend

    exec xoasinhvien HCM*

  • V d Vit th tc m xem c bao nhiu sinh vin theo thnh ph.create procedure dem @tp char(5), @t int output asbeginselect @t = count(*) from sinhvien where tp = @tpenddeclare @tong intexec dem HCM , @tong outputprint @tong*

  • To lp hmcreate function tn_hm ( @tn_tham_s kiu loi [,n] )returns kiu_tr_vaslnh | khi_lnhTrong :loi := input(khng cn ghi)output*

  • Thc thi hm= tn_hm (gi_tr | @bin [output] [,n] )Xa hmDrop function tn_hmThay i hmAlter function tn_hm ..*

  • V d Vit hm m xem c bao nhiu sinh vin theo thnh ph.create function dem (@tp char(5)) returns intasbegindeclare @t int select @t = count(*) from sinhvien where tp = @tpreturn @tenddeclare @tong int set @tong = dbo.dem(HCM) *

  • V d Vit hm sinh ra m sinh vin t ng theo quy tc- M sinh vin c dng: BA0001BA : quy nh (lun c)0001 : l s VD:Hin ti sinh vin c m cao nht l BA0024Th sinh m mi l BA0025*

  • Create function sinhkhoa () returns char(6)AsBegin declare @max int select @max = max(cast(substring(masv,3,4) as int)) + 1from sinhvien declare @s char(6)set @s = 0000 + rtrim(cast(@max as char(4)))set @s = BA + right(@s,4)return @send*

  • V d vi Table Functioncreate function laydssv (@malop char(5))returns TABLEasreturn (select masv,tensv from sinhvien where malop = @malop )

    select * from laydssv('QT1')*

  • V d vi Table Functioncreate function laydssv1 (@malop char(5))returns @btam table(masv char(5),tensv char(20)) asbegininsert into @btam select masv,tensv from sinhvien where malop = @malopreturnend

    select * from laydssv1('QT1')*

  • By lnh c pht sinh sau nhng hnh vi thm mi hay thay i, xa trn bng.C th hy cc cp nhp trn d liuBy lnh c pht sinh thay th nhng hnh vi thm, i, xa.By s kin lu tr tch ri gi tr mi c a vo v gi tr c c xa b.Dng bng tm Inserted v deletedBy s kin cn p dng cho Login. *

  • Loi trigger FOR

    *Lu vo d liuInsert | Update | Deleteim luLnh by s kinInsertedDeletedKhng chp nhn s thay i (RollBack)Chp nhn (Commit)

  • Loi trigger INSTEAD OF

    *Insert | Update | DeleteLnh by s kinInsertedDeletedKhng thay i d liu

  • Bng tm cho by s kin inserted :Lu nhng thng tin sp c a vo d liudeleted :Lu nhng thng tin c v chun b c thay thBng tm c cu trc ging bng t s kin.VD:Cp nhp sinh vin m BA0002 vi thnh ph HCM thnh HN.=> Vo bng inserted l b (BA0002,aaa,HN)=> Vo bng deleted l b (BA0002,aaa,HCM)*

  • update(tn_ct) :Tr v kt qu True / False nu ct b cp nhp.columns_updated() :Tr v mt s xc nh cc ct c cp nhp. V d: Bang (c1,c2,c3,c4) Nu c2 c cp nht => 0010: kt qu 2Nu c1,c3,c4 c cp nht =>1101: kt qu 13*

  • S kin lng nhau c ti a 32Thay i thng s cho php lng nhaualter database tendatabaseset recursive_triggers { on | off }

    Thit lp gii hn lng nhauexec sp_configure Nested Triggers n

    *

  • To triggercreate trigger tn_trigger on tn_bng{for|instead of} {insert|delete|update} aslnh | khi_lnhXa v thay i Alter | Drop trigger tn_trigger ..*

  • To trigger cho bng sinhvien (masv, tensv, malop)tha mn iu kin mt lp khng qu 20 ngi.

    Create trigger tssv on sinhvien for insert,update AsBegindeclare @malop char(5), @ts int select @malop = malop from insertedselect @ts = count(*) from sinhvien where malop=@malopif (@ts > 20) rollback transactionend*

  • To trigger cho bng sinhvien (masv, tensv, trangthai)tha mn iu kin khi xa mt sinh vin tc thay i trng thi t 0 thnh 1.

    Create trigger tssv on sinhvien instead of delete AsBeginupdate sinhvien set trangthai = 1where masv in (select masv from deleted)end*

    *