sql 觸發器 if條件判斷


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM