0

我有 3 个变量,它们在不为 NULL 时@path是有条件的,即 @serverName,@analysisDB,@cubeName。因此,我CASE在上面的语句中使用了一个SELECT语句。同样, for@path不是 NULL@cubename也需要有条件的 for LVL5.L5_CubeName(参见REPLACE语句),但它在REPLACE语句内部并且CASE块变得太复杂了。如果我想做类似的事情,我该怎么写

CASE WHEN @path!='' THEN @cubeName ELSE LVL5.L5_CubeName in the REPLACE statement
SELECT LVL5.L5_Id,        
         CASE WHEN @path!='' THEN @serverName ELSE LVL5.L5_ServerName END [AnalysisServer],        
         CASE WHEN @path!='' THEN @analysisDB ELSE LVL5.L5_AnalysisDatabase END [AnalysisDatabase],        
         REPLACE(REPLACE(REPLACE(LVL5.L5_MDXQuery,'@@Level1',ISNULL(@lvl1DataVal,'')),    
         '@@Level2',ISNULL(@lvl2DataVal,'')),'@@CubeName',ISNULL(@cubeName,'')) [MDXQuery],         
         LVL5.L5_ReplaceMDX [ReplaceMDX],      
         LVL5.L5_RefreshDate [RefreshDate],      
         LVL5.L5_ReportAttribute [ReportAttribute],      
         LVL5.L5_ReportTitle [ReportTitle]           
  FROM   Report_SR_Level5 [LVL5]                  
  WHERE L4_ID = @L4_ID ORDER BY LVL5.L5_DisplayOrder 
4

1 回答 1

0

像这样的东西:

CASE WHEN @path is not null or @path != '' 
THEN REPLACE(REPLACE(REPLACE(LVL5.L5_MDXQuery,'@@Level1',
ISNULL(@lvl1DataVal,'')),'@@Level2',ISNULL(@lvl2DataVal,'')),'@@CubeName',
ISNULL(@cubeName,'')) [MDXQuery]
ELSE Somethingelse END cubedata
于 2012-06-04T12:24:31.207 回答