create database usertype
use usertype
create table usertype
(
  username varchar (50) not null,
  userscore bigint not null, 
  userlevel int not null, 
  userfunction varchar (50) not null
)CREATE TRIGGER Trigger1 
ON dbo.usertype 
FOR UPDATE
AS
declare @level int,
@function varchar(50)
if (update (userscore))
update usertype set @level=userscore/10,userlevel=@level,userfunction=@function 
case
when @level between 0 and 4 then @function='工兵' 
when @level between 5 and 9 then @function='副排长'
when @level between 10 and 29 then @function='排长'
when @level between 30 and 49 then @function='副连长'
when @level between 50 and 79 then @function='连长'
when @level between 80 and 119 then @function='副营长'
when @level between 120 and 169 then @function='营长'
when @level between 170 and 229 then @function='副团长'
when @level between 230 and 299 then @function='团长'
when @level between 300 and 369 then @function='副旅长'
when @level between 370 and 439 then @function='旅长'
when @level between 440 and 529 then @function='副师长'
when @level between 530 and 629 then @function='师长'
when @level between 630 and 749 then @function='副军长'
when @level between 750 and 889 then @function='军长'
when @level between 890 and 1059 then @function='副司令'
when @level between 1060 and 1229 then @function='司令'
when @level between 1230 and 1519 then @function='副元帅'
when @level between 1620 and 1999 then @function='元帅'
when @level not between 0 and 1999 then @function='三军统帅'
  end from usertype这个触发器有错,但我比较菜,不知道怎么改,请帮忙一下,谢谢!

解决方案 »

  1.   

    最好给出完整的表结构,测试数据,计算方法和正确结果.发帖注意事项
    http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281
      

  2.   

    我的实现目标是当userscore改变时通过userscore%10后得到userlevel,再根据userlevel更新userfunction,比如我update userscore=105,那么userscore%10=10,即userlevel=10,然后更新userfunction='排长'
      

  3.   

    CREATE TRIGGER Trigger1 
    ON dbo.usertype 
    FOR UPDATE 
    AS 
    declare @level int
    if (update (userscore)) 
    update usertype set @level=userscore/10,userlevel=@level,userfunction= 
    case 
    when @level between 0 and 4 then '工兵' 
    when @level between 5 and 9 then '副排长' 
    when @level between 10 and 29 then '排长' 
    when @level between 30 and 49 then '副连长' 
    when @level between 50 and 79 then '连长' 
    when @level between 80 and 119 then '副营长' 
    when @level between 120 and 169 then '营长' 
    when @level between 170 and 229 then '副团长' 
    when @level between 230 and 299 then '团长' 
    when @level between 300 and 369 then '副旅长' 
    when @level between 370 and 439 then '旅长' 
    when @level between 440 and 529 then '副师长' 
    when @level between 530 and 629 then '师长' 
    when @level between 630 and 749 then '副军长' 
    when @level between 750 and 889 then '军长' 
    when @level between 890 and 1059 then '副司令' 
    when @level between 1060 and 1229 then '司令' 
    when @level between 1230 and 1519 then '副元帅' 
    when @level between 1620 and 1999 then '元帅' 
    when @level not between 0 and 1999 then '三军统帅' end