tags:

views:

130

answers:

4

I'm looking for a way to return a bit representing whether 2 ints are equal. When I try the following I get "Incorrect syntax near '='." What am I missing? I'm using SQL Server 2005.

DECLARE @Table1Count AS INT

DECLARE @Table2Count AS INT

SELECT @Table1Count = COUNT(*) FROM Table1

SELECT @Table2Count = COUNT(*) FROM Table2

PRINT @Table1Count = @Table2Count

Thanks.

+10  A: 
IF @Table1Count = @Table2Count
    PRINT 1
ELSE
    PRINT 0

alternately:

PRINT CASE WHEN @Table1Count = @Table2Count THEN 1 ELSE 0 END
Thanks, figured it was trying to assign with my approach.
NATO24
+3  A: 
DECLARE @Table1Count AS INT
DECLARE @Table2Count AS INT
SELECT @Table1Count = COUNT(*) FROM Table1
SELECT @Table2Count = COUNT(*) FROM Table2
PRINT case when @Table1Count = @Table2Count then '1' else '0' end
santiiiii
A: 

Just an addendum: Our network here is down at the moment so I can't test anything -- which is why I'm goofing around on Stackoverflow rather than writing code -- so take what I say here with a grain of salt. But:

Some SQL engines have boolean variables and some don't. In Oracle, "select 1+1=2 from dual" is a synatx error, because 1+1=2 is an operation, not an expression. You'd have to use a "case" as other posters have noted. But in Postgres, "select 1+1=2" would display "true".

The idea that "<" is an operation just like "+", except that it returns a boolean rather than an int, is one of those simple but powerful ideas that have great ramifications ... but it was invented after SQL and is only slowly creeping in.

Jay