內容:
觸發器:insetead of 替代觸發器,
after后觸發器,只能定義在表上,不能放在視圖上
delete操作前的資料
inserted 操作后的新資料
例如:
create trigger tr_update
on userinfo
insetead of update --替代觸發器
as
begin
declare @name varcgar(50)
select @name=Customers from inserted
if(@name='張三')
update userinfo set customername='王炸'
else
update userinfo set customername='三條A'
end
自己對觸發器的理解;替代觸發器是begin和end之間的部分,進行替代,也可以理解為替代觸發器修改不會成功
后觸發器:使用后觸發器,是先執行陳述句,然后去執行觸發器中的內容,執行陳述句必須能執行成功
兩張臨時表分別為:inserted,delete
insert--inserted有資料
-- delted無資料
delete與insert相反
update--inserted修改后的資料
--delted修改前的資料
游標:
作用:對逐行資料進行處理
使用程序:1.定義 2.打開 3.使用 4.關閉 5.釋放
--1.游標練習
declare @price money,@name varchar(200),@id int
declare cur_changeprice cursor
for select id,name ,sellprice from Product where SellPrice<=500
open cur_changeprice
fetch next cur_changeprice into @id,@name,@price
while @@FETCH_STATUS=0
begin
if(@price<100 and CHARINDEX('牛仔褲',@name)>0)
begin
update Product set SellPrice=SellPrice*1.1 where id=@id
end
if(@price>=100 and @price <=500 and CHARINDEX ('T恤',@name)>0)
begin
update Product set SellPrice=SellPrice*1.05 where id=@id
end
fetch next from cur_changeprice into @id,@name,@price
end
以上是我對游標和觸發器的理解,如有問題,請及時聯系我,我會更正,畢竟第一次發帖,還請各位大佬多多指點
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/58337.html
標籤:基礎類
