案例陈述集参数

时间:2012-09-21 12:07:48

标签: sql sql-server sql-server-2005

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

消息156,级别15,状态1,过程SP_CheckDB,第31行 关键字“SET”附近的语法不正确。 消息156,级别15,状态1,过程SP_CheckDB,第36行 关键字“ELSE”附近的语法不正确

请帮助解释此声明

由于

3 个答案:

答案 0 :(得分: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 :(得分: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

虽然请注意您不能在同一声明中分配+选择。

答案 2 :(得分: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>'+@xml +'</table></body></html>')
        ELSE 'NULL' END) as Variance  
    FROM TestTbl