customer表中有一列integral,我想當integral列大於50小於200時,lev為1,integral大於200小於500時,lev為2,大於500時lev為3
我編了一個觸發器,可是總提示出錯,我把我的源碼提供出來,希望萬能的百度幫我解決
create trigger lev
on customer
for update
as
if integral>50 and integral<200
begin
update customer
set lev=1
end
else if integral>200 and integral<500
begin
update customer
set lev=2
end
else integral>500
begin
update customer
set lev=3
end
解決方法代碼:
不知道是 Oracle 還是 SQL Server
如果是 Oracle , 基本上是用 fei07100107 那種的用法
如果是 SQL Server
那么需要定義幾個變量。
create trigger lev
on customer
for update
as
DECLARE
@NewIntegral INT;
BEGIN
-- 取得 本次 更新的 integral
-- 如果一條語句,更新很多條記錄的,這里要用游標處理。
SELECT @NewIntegral = integral FROM INSERTED
-- 如果這里不是更新全部表的,
-- 那么麻煩上面再多定義一個 變量,
-- 從 INSERTED 里面,取得 主鍵, 下面這里加 WHERE 條件。
if @NewIntegral>50 and @NewIntegral<200
begin
update customer
set lev=1
end
else if @NewIntegral>200 and @NewIntegral<500
begin
update customer
set lev=2
end
else @NewIntegral>500
begin
update customer
set lev=3
end
END