How can I save the comparison result in a variable

I want to print a simple print statement (1 = 1), I expect the result to be TRUE or 1, but the sql server will tell me: The syntax is incorrect around '='. Why is this?

The same thing will happen for such a statement.

declare @test bit set @test = (1=1) 

in short, how can I “see” what is returned from a comparison without using the IF statement

Update: the reason I'm asking is because I'm trying to debug why the following statement

 declare @AgingAmount smallint set @AgingAmount = 500 select Amount, datediff(day,Batch.SubmitDate,getdate()) as Aging from myreporrt where datediff(day,Batch.SubmitDate,getdate()) > @AgingAmount 

will return all rows even when aging 300 so I wanted to check if the datum (day, dateubmited, getdate ())> 500 returns true or false, but could not find a way to display the result of this comparison.

+6
tsql
source share
1 answer

Although SQL Server has a concept like boolean , and it understands expressions that allow boolean IF in IF and WHERE , it does not support the declaration of boolean variables or parameters. The bit data type cannot store the result of a boolean expression directly, even if it looks suspiciously like.

Closest you can get the boolean data type:

 -- Store the result of a boolean test. declare @result bit select @result = case when <boolean expression> then 1 else 0 end -- Make use of the above result somewhere else. if @result = 1 ... else ... 

To add to the confusion, SQL Server Management Studio treats bit as a boolean when displaying results, and ADO.NET maps bit to System.Boolean when transferring data back and forth.

Update: To answer your last question, use the case when ... then 1 else 0 end syntax in the select statement.

+6
source share