运行时出错:
消息 156,级别 15,状态 1,第 1 行
关键字 'else' 附近有语法错误。
代码如下:
select 考号,性别,身高,体重,[肺活量]=(case  when 性别='男' and [肺活量]>=stuff('≥67',1,1,'') then 10 when 性别='男' and [肺活量]>=left('67-58',charindex('-','67-58')-1) and [肺活量] <stuff('67-58',1,charindex('-','67-58'),'') then 9 when 性别='男' and [肺活量]>=left('58-50',charindex('-','58-50')-1) and [肺活量] <stuff('58-50',1,charindex('-','58-50'),'') then 8 when 性别='男' and [肺活量]>=left('50-46',charindex('-','50-46')-1) and [肺活量] <stuff('50-46',1,charindex('-','50-46'),'') then 7 when 性别='男' and [肺活量]>=left('46-40',charindex('-','46-40')-1) and [肺活量] <stuff('46-40',1,charindex('-','46-40'),'') then 6 when 性别='男' and [肺活量]>=left('40-31.9',charindex('-','40-31.9')-1) and [肺活量] <stuff('40-31.9',1,charindex('-','40-31.9'),'') then 5 when 性别='男' and [肺活量] <=stuff('≤32',1,1,'') then 4 when 性别='女' and [肺活量]>=stuff('≥49',1,1,'') then 10 when 性别='女' and [肺活量]>=left('49-41',charindex('-','49-41')-1) and [肺活量] <stuff('49-41',1,charindex('-','49-41'),'') then 9 when 性别='女' and [肺活量]>=left('41-36',charindex('-','41-36')-1) and [肺活量] <stuff('41-36',1,charindex('-','41-36'),'') then 8 when 性别='女' and [肺活量]>=left('36-31',charindex('-','36-31')-1) and [肺活量] <stuff('36-31',1,charindex('-','36-31'),'') then 7 when 性别='女' and [肺活量]>=left('31-26',charindex('-','31-26')-1) and [肺活量] <stuff('31-26',1,charindex('-','31-26'),'') then 6 when 性别='女' and [肺活量]>=left('26-20.9',charindex('-','26-20.9')-1) and [肺活量] <stuff('26-20.9',1,charindex('-','26-20.9'),'') then 5 when 性别='女' and [肺活量] <=stuff('≤21',1,1,'') then 4 else 0 end),[跳绳]=(case  when 性别='男' and [跳绳]>=stuff('≥105',1,1,'') then 7 when 性别='男' and [跳绳]>=left('105-100',charindex('-','105-100')-1) and [跳绳] <stuff('105-100',1,charindex('-','105-100'),'') then 6 when 性别='男' and [跳绳]>=left('100-95',charindex('-','100-95')-1) and [跳绳] <stuff('100-95',1,charindex('-','100-95'),'') then 5 when 性别='男' and [跳绳]>=left('95-90',charindex('-','95-90')-1) and [跳绳] <stuff('95-90',1,charindex('-','95-90'),'') then 4 when 性别='男' and [跳绳] < stuff('<90',1,1,'') then 3 when 性别='女' and [跳绳]>=stuff('≥110',1,1,'') then 7 when 性别='女' and [跳绳]>=left('110-105',charindex('-','110-105')-1) and [跳绳] <stuff('110-105',1,charindex('-','110-105'),'') then 6 when 性别='女' and [跳绳]>=left('105-100',charindex('-','105-100')-1) and [跳绳] <stuff('105-100',1,charindex('-','105-100'),'') then 5 when 性别='女' and [跳绳]>=left('100-95',charindex('-','100-95')-1) and [跳绳] <stuff('100-95',1,charindex('-','100-95'),'') then 4 when 性别='女' and [跳绳] < stuff('<95',1,1,'') then 3 else 0 end),[立定跳远]=(case  when 性别='男' and [立定跳远]>=stuff('≥222',1,1,'') then 7 when 性别='男' and [立定跳远]>=left('222-210',charindex('-','222-210')-1) and [立定跳远] <stuff('222-210',1,charindex('-','222-210'),'') then 6 when 性别='男' and [立定跳远]>=left('210-202',charindex('-','210-202')-1) and [立定跳远] <stuff('210-202',1,charindex('-','210-202'),'') then 5 when 性别='男' and [立定跳远]>=left('202-180',charindex('-','202-180')-1) and [立定跳远] <stuff('202-180',1,charindex('-','202-180'),'') then 4 when 性别='男' and [立定跳远] < stuff('<180',1,1,'') then 3 else 0 end),[仰卧起坐]=(case  when 性别='女' and [仰卧起坐]>=stuff('≥33',1,1,'') then 7 when 性别='女' and [仰卧起坐]>=left('33-27',charindex('-','33-27')-1) and [仰卧起坐] <stuff('33-27',1,charindex('-','33-27'),'') then 6 when 性别='女' and [仰卧起坐]>=left('27-23',charindex('-','27-23')-1) and [仰卧起坐] <stuff('27-23',1,charindex('-','27-23'),'') then 5 when 性别='女' and [仰卧起坐]>=left('23-15',charindex('-','23-15')-1) and [仰卧起坐] <stuff('23-15',1,charindex('-','23-15'),'') then 4 when 性别='女' and [仰卧起坐] < stuff('<15',1,1,'') then 3 else 0 end),[五十米]=(case  when 性别='男' and [五十米] <=stuff('≤7.6',1,1,'') then 7 when 性别='男' and [五十米]>=left('7.6-8.1',charindex('-','7.6-8.1')-1) and [五十米] <stuff('7.6-8.1',1,charindex('-','7.6-8.1'),'') then 6 when 性别='男' and [五十米]>=left('8.1-8.6',charindex('-','8.1-8.6')-1) and [五十米] <stuff('8.1-8.6',1,charindex('-','8.1-8.6'),'') then 5 when 性别='男' and [五十米]>=left('8.6-9',charindex('-','8.6-9')-1) and [五十米] <stuff('8.6-9',1,charindex('-','8.6-9'),'') then 4 when 性别='男' and [五十米]>=stuff('≥9',1,1,'') then 3 when 性别='女' and [五十米] <=stuff('≤9',1,1,'') else 0 end),[得分]=-1 from TiYuFenShu 

解决方案 »

  1.   

    when 性别='女' and [五十米] <=stuff('≤9',1,1,'') THEN ... else 0 end),[得分]=-1 from TiYuFenShu 
    少個then吧
      

  2.   

    So do I. But I think you can manage.
      

  3.   

    谢谢
    但是以上代码是由以下动态代码产生的,我该如何改造如下代码,使之能正确运行?
    也就是下面的串@sql.set ANSI_NULLS ON 
    set QUOTED_IDENTIFIER ON 
    GO 
    -- ============================================= 
    -- Author: <Author,,Name> 
    -- Create date: <Create Date,,> 
    -- Description: <Description,,> 
    -- ============================================= 
    ALTER PROCEDURE [dbo].[Others] 
    AS 
    BEGIN 
    declare @sql varchar(8000),@name varchar(20) 
    set @sql='' declare tc cursor for 
    select name from syscolumns where id=object_id('TiYuFenShu') and colid>4 open tc fetch next from tc into @name while @@fetch_status=0 
    begin 
        if exists(select 1 from PingFenBiaoZhun2 where 类别=@name) 
        begin 
            set @sql=@sql+',['+@name+']=(case ' 
            
            
            select 
                @sql=@sql+' when 性别='''+性别+''' and '+ 
                    case when charindex('-',数量)>0  then '['+@name+']>=left('''+数量+''',charindex(''-'','''+数量+''')-1) and '+ 
                                                          '['+@name+'] <stuff('''+数量+''',1,charindex(''-'','''+数量+'''),'''')' 
                          when charindex('≥',数量)>0  then '['+@name+']>=stuff('''+数量+''',1,1,'''')' 
                          when charindex('≤',数量)>0  then '['+@name+'] <=stuff('''+数量+''',1,1,'''')' 
                          when charindex('<',数量)>0 then '['+@name+'] < stuff('''+数量+''',1,1,'''')' end 
                    +' then '+rtrim(分值)  
            from 
                PingFenBiaoZhun2 
            where 
                类别=@name 
            
            set @sql=@sql+' else 0 end)' 
        end 
        else 
            set @sql=@sql+',['+@name+']=-1' 
        
        fetch next from tc into @name 
    end 
    close tc 
    deallocate tc set @sql='select 考号,性别,身高,体重'+@sql+' from TiYuFenShu' 
    print @sql 
    exec(@sql) 
    END 
      

  4.   

    --try
    set ANSI_NULLS ON 
    set QUOTED_IDENTIFIER ON 
    GO 
    -- ============================================= 
    -- Author: <Author,,Name> 
    -- Create date: <Create Date,,> 
    -- Description: <Description,,> 
    -- ============================================= 
    ALTER PROCEDURE [dbo].[Others] 
    AS 
    BEGIN 
    declare @sql varchar(8000),@name varchar(20) 
    set @sql='' declare tc cursor for 
    select name from syscolumns where id=object_id('TiYuFenShu') and colid>4 open tc fetch next from tc into @name while @@fetch_status=0 
    begin 
        if exists(select 1 from PingFenBiaoZhun2 where 类别=@name) 
        begin 
            set @sql=@sql+',['+@name+']=(case ' 
            
            
            select 
                @sql=@sql+' when 性别='''+性别+''' and '+ 
                    case when charindex('-',数量)>0  then '['+@name+']>=left('''+数量+''',charindex(''-'','''+数量+''')-1) and '+ 
                                                          '['+@name+'] <stuff('''+数量+''',1,charindex(''-'','''+数量+'''),'''')' 
                          when charindex('≥',数量)>0  then '['+@name+']>=stuff('''+数量+''',1,1,'''')' 
                          when charindex('≤',数量)>0  then '['+@name+'] <=stuff('''+数量+''',1,1,'''')' 
                          when charindex('<',数量)>0 then '['+@name+'] < stuff('''+数量+''',1,1,'''')' end 
                    +' then '+rtrim(分值)  
            from 
                PingFenBiaoZhun2 
            where 
                类别=@name 
            
            set @sql=@sql+'THEN 2 else 0 end)' 
        end 
        else 
            set @sql=@sql+',['+@name+']=-1' 
        
        fetch next from tc into @name 
    end 
    close tc 
    deallocate tc set @sql='select 考号,性别,身高,体重'+@sql+' from TiYuFenShu' 
    print @sql 
    exec(@sql) 
    END 
      

  5.   

    谢谢
    试了您的代码,产生错误如下:
    消息 156,级别 15,状态 1,第 1 行
    关键字 'THEN' 附近有语法错误。
    @sql的值如下:select 考号,性别,身高,体重,[肺活量]=(case  when 性别='男' and [肺活量]>=stuff('≥67',1,1,'') then 10 when 性别='男' and [肺活量]>=left('67-58',charindex('-','67-58')-1) and [肺活量] <stuff('67-58',1,charindex('-','67-58'),'') then 9 when 性别='男' and [肺活量]>=left('58-50',charindex('-','58-50')-1) and [肺活量] <stuff('58-50',1,charindex('-','58-50'),'') then 8 when 性别='男' and [肺活量]>=left('50-46',charindex('-','50-46')-1) and [肺活量] <stuff('50-46',1,charindex('-','50-46'),'') then 7 when 性别='男' and [肺活量]>=left('46-40',charindex('-','46-40')-1) and [肺活量] <stuff('46-40',1,charindex('-','46-40'),'') then 6 when 性别='男' and [肺活量]>=left('40-31.9',charindex('-','40-31.9')-1) and [肺活量] <stuff('40-31.9',1,charindex('-','40-31.9'),'') then 5 when 性别='男' and [肺活量] <=stuff('≤32',1,1,'') then 4 when 性别='女' and [肺活量]>=stuff('≥49',1,1,'') then 10 when 性别='女' and [肺活量]>=left('49-41',charindex('-','49-41')-1) and [肺活量] <stuff('49-41',1,charindex('-','49-41'),'') then 9 when 性别='女' and [肺活量]>=left('41-36',charindex('-','41-36')-1) and [肺活量] <stuff('41-36',1,charindex('-','41-36'),'') then 8 when 性别='女' and [肺活量]>=left('36-31',charindex('-','36-31')-1) and [肺活量] <stuff('36-31',1,charindex('-','36-31'),'') then 7 when 性别='女' and [肺活量]>=left('31-26',charindex('-','31-26')-1) and [肺活量] <stuff('31-26',1,charindex('-','31-26'),'') then 6 when 性别='女' and [肺活量]>=left('26-20.9',charindex('-','26-20.9')-1) and [肺活量] <stuff('26-20.9',1,charindex('-','26-20.9'),'') then 5 when 性别='女' and [肺活量] <=stuff('≤21',1,1,'') then 4THEN 2 else 0 end),[跳绳]=(case  when 性别='男' and [跳绳]>=stuff('≥105',1,1,'') then 7 when 性别='男' and [跳绳]>=left('105-100',charindex('-','105-100')-1) and [跳绳] <stuff('105-100',1,charindex('-','105-100'),'') then 6 when 性别='男' and [跳绳]>=left('100-95',charindex('-','100-95')-1) and [跳绳] <stuff('100-95',1,charindex('-','100-95'),'') then 5 when 性别='男' and [跳绳]>=left('95-90',charindex('-','95-90')-1) and [跳绳] <stuff('95-90',1,charindex('-','95-90'),'') then 4 when 性别='男' and [跳绳] < stuff('<90',1,1,'') then 3 when 性别='女' and [跳绳]>=stuff('≥110',1,1,'') then 7 when 性别='女' and [跳绳]>=left('110-105',charindex('-','110-105')-1) and [跳绳] <stuff('110-105',1,charindex('-','110-105'),'') then 6 when 性别='女' and [跳绳]>=left('105-100',charindex('-','105-100')-1) and [跳绳] <stuff('105-100',1,charindex('-','105-100'),'') then 5 when 性别='女' and [跳绳]>=left('100-95',charindex('-','100-95')-1) and [跳绳] <stuff('100-95',1,charindex('-','100-95'),'') then 4 when 性别='女' and [跳绳] < stuff('<95',1,1,'') then 3THEN 2 else 0 end),[立定跳远]=(case  when 性别='男' and [立定跳远]>=stuff('≥222',1,1,'') then 7 when 性别='男' and [立定跳远]>=left('222-210',charindex('-','222-210')-1) and [立定跳远] <stuff('222-210',1,charindex('-','222-210'),'') then 6 when 性别='男' and [立定跳远]>=left('210-202',charindex('-','210-202')-1) and [立定跳远] <stuff('210-202',1,charindex('-','210-202'),'') then 5 when 性别='男' and [立定跳远]>=left('202-180',charindex('-','202-180')-1) and [立定跳远] <stuff('202-180',1,charindex('-','202-180'),'') then 4 when 性别='男' and [立定跳远] < stuff('<180',1,1,'') then 3THEN 2 else 0 end),[仰卧起坐]=(case  when 性别='女' and [仰卧起坐]>=stuff('≥33',1,1,'') then 7 when 性别='女' and [仰卧起坐]>=left('33-27',charindex('-','33-27')-1) and [仰卧起坐] <stuff('33-27',1,charindex('-','33-27'),'') then 6 when 性别='女' and [仰卧起坐]>=left('27-23',charindex('-','27-23')-1) and [仰卧起坐] <stuff('27-23',1,charindex('-','27-23'),'') then 5 when 性别='女' and [仰卧起坐]>=left('23-15',charindex('-','23-15')-1) and [仰卧起坐] <stuff('23-15',1,charindex('-','23-15'),'') then 4 when 性别='女' and [仰卧起坐] < stuff('<15',1,1,'') then 3THEN 2 else 0 end),[五十米]=(case  when 性别='男' and [五十米] <=stuff('≤7.6',1,1,'') then 7 when 性别='男' and [五十米]>=left('7.6-8.1',charindex('-','7.6-8.1')-1) and [五十米] <stuff('7.6-8.1',1,charindex('-','7.6-8.1'),'') then 6 when 性别='男' and [五十米]>=left('8.1-8.6',charindex('-','8.1-8.6')-1) and [五十米] <stuff('8.1-8.6',1,charindex('-','8.1-8.6'),'') then 5 when 性别='男' and [五十米]>=left('8.6-9',charindex('-','8.6-9')-1) and [五十米] <stuff('8.6-9',1,charindex('-','8.6-9'),'') then 4 when 性别='男' and [五十米]>=stuff('≥9',THEN 2 else 0 end),[得分]=-1 from TiYuFenShu
      

  6.   

    and [五十米] <=stuff('≤9',1,1,'')then (就是这个地方少了) else 0 end),[得分]=-1 from TiYuFenShu 
      

  7.   

    谢谢回复
    但是在存储过程中怎么改?
    请看上面的帖子,这个长语句是动态产生的,希望能在存储过程中帮我改过来,谢谢
    我对于动态tsql还是不会
      

  8.   

    when 性别='女' and [五十米] <=stuff('≤9',1,1,'') ----这有错,缺个then ...
      

  9.   

    谢谢回复
    但是在存储过程中怎么改? 
    请看上面的帖子,这个长语句是动态产生的,希望能在存储过程中帮我改过来,谢谢 
    我对于动态tsql还是不会
      

  10.   


    写了一天的sql语句,本来就头晕眼花的,只有顶的份儿了
      

  11.   

    呵呵,不太清楚为什么会少了一个 then ... SQL语句应该不会导致这个问题。
      

  12.   

    这种语句,效率实在成问题,如果记录过万,可能就麻烦了
    错误如下:注意9999
    select 考号,性别,身高,体重,
    --[肺活量]=(case  
    --when 性别='男' and [肺活量]>=stuff('≥67',1,1,'') then 10 
    --when 性别='男' and [肺活量]>=left('67-58',charindex('-','67-58')-1) and [肺活量] <stuff('67-58',1,charindex('-','67-58'),'') then 9 
    --when 性别='男' and [肺活量]>=left('58-50',charindex('-','58-50')-1) and [肺活量] <stuff('58-50',1,charindex('-','58-50'),'') then 8 
    --when 性别='男' and [肺活量]>=left('50-46',charindex('-','50-46')-1) and [肺活量] <stuff('50-46',1,charindex('-','50-46'),'') then 7 
    --when 性别='男' and [肺活量]>=left('46-40',charindex('-','46-40')-1) and [肺活量] <stuff('46-40',1,charindex('-','46-40'),'') then 6 
    --when 性别='男' and [肺活量]>=left('40-31.9',charindex('-','40-31.9')-1) and [肺活量] <stuff('40-31.9',1,charindex('-','40-31.9'),'') then 5 
    --when 性别='男' and [肺活量] <=stuff('≤32',1,1,'') then 4 when 性别='女' and [肺活量]>=stuff('≥49',1,1,'') then 10 
    --when 性别='女' and [肺活量]>=left('49-41',charindex('-','49-41')-1) and [肺活量] <stuff('49-41',1,charindex('-','49-41'),'') then 9 
    --when 性别='女' and [肺活量]>=left('41-36',charindex('-','41-36')-1) and [肺活量] <stuff('41-36',1,charindex('-','41-36'),'') then 8 
    --when 性别='女' and [肺活量]>=left('36-31',charindex('-','36-31')-1) and [肺活量] <stuff('36-31',1,charindex('-','36-31'),'') then 7 
    --when 性别='女' and [肺活量]>=left('31-26',charindex('-','31-26')-1) and [肺活量] <stuff('31-26',1,charindex('-','31-26'),'') then 6 
    --when 性别='女' and [肺活量]>=left('26-20.9',charindex('-','26-20.9')-1) and [肺活量] <stuff('26-20.9',1,charindex('-','26-20.9'),'') then 5 
    --when 性别='女' and [肺活量] <=stuff('≤21',1,1,'') then 4 else 0 end),[跳绳]=(case  when 性别='男' and [跳绳]>=stuff('≥105',1,1,'') then 7 
    --when 性别='男' and [跳绳]>=left('105-100',charindex('-','105-100')-1) and [跳绳] <stuff('105-100',1,charindex('-','105-100'),'') then 6 
    --when 性别='男' and [跳绳]>=left('100-95',charindex('-','100-95')-1) and [跳绳] <stuff('100-95',1,charindex('-','100-95'),'') then 5 
    --when 性别='男' and [跳绳]>=left('95-90',charindex('-','95-90')-1) and [跳绳] <stuff('95-90',1,charindex('-','95-90'),'') then 4 
    --when 性别='男' and [跳绳] < stuff('<90',1,1,'') then 3 when 性别='女' and [跳绳]>=stuff('≥110',1,1,'') then 7 
    --when 性别='女' and [跳绳]>=left('110-105',charindex('-','110-105')-1) and [跳绳] <stuff('110-105',1,charindex('-','110-105'),'') then 6 
    --when 性别='女' and [跳绳]>=left('105-100',charindex('-','105-100')-1) and [跳绳] <stuff('105-100',1,charindex('-','105-100'),'') then 5 
    --when 性别='女' and [跳绳]>=left('100-95',charindex('-','100-95')-1) and [跳绳] <stuff('100-95',1,charindex('-','100-95'),'') then 4 
    --when 性别='女' and [跳绳] < stuff('<95',1,1,'') then 3 else 0 end),
    --
    --[立定跳远]=(case  when 性别='男' and [立定跳远]>=stuff('≥222',1,1,'') then 7 
    --when 性别='男' and [立定跳远]>=left('222-210',charindex('-','222-210')-1) and [立定跳远] <stuff('222-210',1,charindex('-','222-210'),'') then 6 
    --when 性别='男' and [立定跳远]>=left('210-202',charindex('-','210-202')-1) and [立定跳远] <stuff('210-202',1,charindex('-','210-202'),'') then 5 
    --when 性别='男' and [立定跳远]>=left('202-180',charindex('-','202-180')-1) and [立定跳远] <stuff('202-180',1,charindex('-','202-180'),'') then 4 
    --when 性别='男' and [立定跳远] < stuff('<180',1,1,'') then 3 else 0 end),
    --
    --[仰卧起坐]=(case  when 性别='女' and [仰卧起坐]>=stuff('≥33',1,1,'') then 7 
    --when 性别='女' and [仰卧起坐]>=left('33-27',charindex('-','33-27')-1) and [仰卧起坐] <stuff('33-27',1,charindex('-','33-27'),'') then 6 
    --when 性别='女' and [仰卧起坐]>=left('27-23',charindex('-','27-23')-1) and [仰卧起坐] <stuff('27-23',1,charindex('-','27-23'),'') then 5 
    --when 性别='女' and [仰卧起坐]>=left('23-15',charindex('-','23-15')-1) and [仰卧起坐] <stuff('23-15',1,charindex('-','23-15'),'') then 4 
    --when 性别='女' and [仰卧起坐] < stuff('<15',1,1,'') then 3 else 0 end),[五十米]=(case  when 性别='男' and [五十米] <=stuff('≤7.6',1,1,'') then 7 
    when 性别='男' and [五十米]>=left('7.6-8.1',charindex('-','7.6-8.1')-1) and [五十米] <stuff('7.6-8.1',1,charindex('-','7.6-8.1'),'') then 6 
    when 性别='男' and [五十米]>=left('8.1-8.6',charindex('-','8.1-8.6')-1) and [五十米] <stuff('8.1-8.6',1,charindex('-','8.1-8.6'),'') then 5 
    when 性别='男' and [五十米]>=left('8.6-9',charindex('-','8.6-9')-1) and [五十米] <stuff('8.6-9',1,charindex('-','8.6-9'),'') then 4 
    when 性别='男' and [五十米]>=stuff('≥9',1,1,'') then 3 
    when 性别='女' and [五十米]<=stuff('≤9',1,1,'') then 99999999 else 0 end),[得分]=-1 
    from TiYuFenShu 
      

  13.   

    谢谢回复,您说我的明白
    但是在存储过程中怎么改? 
    请看上面的帖子,这个长语句是动态产生的,希望能在存储过程中帮我改过来,谢谢 
    我对于动态tsql还是不会