Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Comparing 2 counts always returns the same value

Author  Topic 

monfu
Yak Posting Veteran

81 Posts

Posted - 2008-11-24 : 12:38:31
Hi All

I am comparing 2 counts in a stored procedure. Basically i have this :-

DECLARE @AllMarketsEdbsCount int
DECLARE @UploadedMarketsEdbsCount int

SELECT COUNT(*) AS [@AllMarketsCount]
FROM .......
WHERE ........

SELECT COUNT(*) AS [@UploadedMarketsCount]
FROM .....
WHERE ......

IF @AllMarketsCount = @UploadedMarketsCount
BEGIN
SET @UploadedFinished = 1
END
ELSE
BEGIN
SET @UploadedFinished = 0
END


@AllMarketsCount is 16 and @UploadedMarketsCount is 6.

This is returning 0, which is correct, however, if I change the "=" to for example "<", the result is still the same, ie 0.

Am I doing something wrong?

Thanks for your time

Johann

bjoerns
Posting Yak Master

154 Posts

Posted - 2008-11-24 : 12:40:35
Since 16 is not lower than 6, I expect 0 actually...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-24 : 12:42:51
you cant assign values to variables like this. it should be as below. try like this and see


DECLARE @AllMarketsEdbsCount int
DECLARE @UploadedMarketsEdbsCount int

SELECT @AllMarketsCount=COUNT(*)
FROM .......
WHERE ........

SELECT @UploadedMarketsCount = COUNT(*)
FROM .....
WHERE ......

IF @AllMarketsCount = @UploadedMarketsCount
BEGIN
SET @UploadedFinished = 1
END
ELSE
BEGIN
SET @UploadedFinished = 0
END



i guess in your case variables where never assigned values so it tokk default value which is NULL so it will always return the else part value 0 as NULL <> NULL under any case.
Go to Top of Page

monfu
Yak Posting Veteran

81 Posts

Posted - 2008-11-24 : 12:51:33
Thanks

SELECT @var = COUNT(*) worked!

Thanks for your help and time!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-24 : 12:54:53
Cheers
Go to Top of Page
   

- Advertisement -