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

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

请帮助这个声明

谢谢

4

3 回答 3

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

或者

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
于 2012-09-21T12:23:40.887 回答
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

尽管请注意,您不能在同一语句中分配 + 选择。

于 2012-09-21T12:13:45.900 回答
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
于 2012-09-21T12:17:48.363 回答