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

  • 相关阅读:
    七. 多线程编程3.主线程
    七. 多线程编程1.线程的概念
    六. 异常处理12.断言
    liunx 安装 mysql 5.6
    idea Unable to open debugger port (127.0.0.1:58006) Address already in use: JVM_Bind 的解决办法
    liunx 安装redis 4.0
    liunx 安装jdk1.8
    idea 去除xml文件sql语句背景色
    改变数据库和表编码
    mybatis 按in 函数参数顺序排序
  • 原文地址:https://www.cnblogs.com/activities/p/2537605.html
Copyright © 2011-2022 走看看