日期:2014-05-17  浏览次数:20457 次

如何取得SELECT COUNT(*) 的值并判断
在存储过程中如何根据 SELECT COUNT(*) 的值来做判断:

DECLARE @CountID int 

 SELECT COUNT(*) AS @CountID FROM Accounts_Comments WHERE UserID = @UserID
 IF @CountID=0
   
 ELSE
  
这样好像不行,能用:

SET @CountID = (SELECT COUNT(*) AS @CountID FROM Accounts_Comments WHERE UserID = @UserID〗

IF @CountID=0
   
 ELSE

求一个解答。

------解决方案--------------------
SQL code

DECLARE @CountID int  

 SELECT @CountID=COUNT(*) FROM Accounts_Comments WHERE UserID = @UserID
 IF @CountID=0
    
 ELSE

------解决方案--------------------
SQL code
IF (SELECT COUNT(1) FROM Accounts_Comments WHERE UserID = @UserID)=0
    PRINT 'No Data for '+@UserID
else
...

------解决方案--------------------
SQL code

--日,这个地方count(*)要改,不然会一直存在一条记录,忘记改了
IF not exists (select 1 from Accounts_Comments WHERE UserID = @UserID)
    
 ELSE