zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    数组过滤函数 array_filter
    unset()到底搞掉了谁?
    $a=NULL 他到底是啥
    wordpress
    (转)Apache对文件后缀解析的分析利用
    isset()和empty()到底区别是什么。
    cad.net 封装jig
    cad.net 封装两个填充类
    ASP.NET CORE WEBAPI文件下载
    swagger配置
  • 原文地址:https://www.cnblogs.com/activities/p/2537605.html
Copyright © 2011-2022 走看看