2012-09-21 81 views
1
SELECT (CASE 
     WHEN(percentage >= @Start AND percentage < @End)       
     THEN 
      SET @body ='<html><body><H3>Report</H3> 
         <table border = 1> 
         <tr> 
         <th>No </th> <th> date </th> <th> lag </th> <th> Variance </th></tr>'  
      SET @body = @body + @xml +'</table></body></html>' 
     ELSE 'NULL' END) as Variance 
    FROM TestTbl 

Msg 156,Level 15,State 1,Procedure SP_CheckDB,Line 31 关键字'SET'附近的语法错误。 消息156,级别15,状态1,过程SP_CheckDB,36 行中的关键字 'ELSE'Case Statement Set参数

本声明请帮助

感谢

+2

'CASE'是*表达* - 它产生的a *值*。它不是*控制流量表。 –

回答

4

使用此

Declare @str VARCHAR(MAX) 

SET @str = '<html><body><H3>Report</H3> 
      <table border = 1> 
      <tr> 
       <th>No </th> <th> date </th> 
       <th> lag </th> <th> Variance </th> 
      </tr>'+ @xml +'</table></body></html>' 


SELECT (CASE 
     WHEN(percentage >= @Start AND percentage < @End)       
     THEN @str 
     ELSE 'NULL' END) as Variance 
FROM TestTbl 

OR

DECLARE @body VARCHAR(MAX) = 'NULL' 

    IF(percentage >= @Start AND percentage < @End) THEN 
    BEGIN 
     SET @body ='<html><body><H3>Report</H3> 
        <table border = 1> 
        <tr> 
         <th>No </th> <th> date </th> 
         <th> lag </th> <th> Variance </th> 
        </tr>'  
     SET @body = @body + @xml +'</table></body></html>' 
    END 

    SELECT Variance = @body 
1

如果你只是想在有条件分配附近的语法不正确,那么你需要这样做:

SELECT TOP 1 @body = (CASE 
     WHEN(percentage >= @Start AND percentage < @End)       
     THEN 
      '<html><body><H3>Report</H3> 
         <table border = 1> 
         <tr> 
         <th>No </th> <th> date </th> <th> lag </th> <th> Variance </th></tr>'  
      + @xml +'</table></body></html>' 
     ELSE 'NULL' 
     END) 
FROM TestTbl 

虽然请注意,你不能指定+选择从同一语句。

1
SELECT @body=(CASE WHEN(percentage >= @Start AND percentage < @End)       
     THEN ('<html><body><H3>Report</H3> 
         <table border = 1> 
         <tr> 
         <th>No </th> <th> date </th> <th> lag </th> <th> Variance </th></tr>'[email protected] +'</table></body></html>') 
     ELSE 'NULL' END) as Variance 
    FROM TestTbl