T-SQL存储过程:获取父级类别图片

如果当前类别无图片,则获取父级图片,直到找到图片

CREATE   PROCEDURE  w_Category_GetImg
@PCID   int
AS
DECLARE
    
@Cache_PCID   int ,
    
@ImgCount   int
BEGIN
    
SET   @Cache_PCID = @PCID ;
    
SET   @ImgCount = ( SELECT   COUNT ( * FROM  w_CategoryPhotos  WHERE  PCID = @Cache_PCID )
    
IF ( @ImgCount > 0 )
    
BEGIN
        
SELECT   *   FROM  w_CategoryPhotos  WHERE  PCID = @Cache_PCID
    
END
    
ELSE
    
BEGIN
    
WHILE   @ImgCount = 0
        
BEGIN
            
SET   @Cache_PCID = ( SELECT  ParentID  FROM  w_ProductCategorys  WHERE  PCID = @Cache_PCID );
            
SET   @ImgCount = ( SELECT   COUNT ( * FROM  w_CategoryPhotos  WHERE  PCID = @Cache_PCID );
            
IF ( @ImgCount = 1 )
            
BEGIN
                
SELECT   *   FROM  w_CategoryPhotos  WHERE  PCID = @Cache_PCID
                
BREAK ;
            
END
        
END
    
END
END

 

你可能感兴趣的:(t-sql)