如何将比较结果存储到变量中

Shr*_*itz 6 t-sql

我想打印一个简单的语句print(1 = 1),我希望结果为TRUE或1但是sql server告诉我:'='附近的语法不正确.这是为什么?

类似的声明也会发生同样的情况

declare @test bit
set @test = (1=1)
Run Code Online (Sandbox Code Playgroud)

总结如何在不使用IF语句的情况下"看到"从比较中返回的内容

更新:我问的原因是因为我正在尝试调试以下语句的原因

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

将返回所有行,即使老化300,所以我想测试datediff(day,datesubmited,getdate())> 500返回true或false但无法找到如何显示此比较结果的方法.

Chr*_*ter 7

虽然SQL Server具有boolean类型的概念,并且它理解解析为booleanin IFWHERE子句的表达式,但它不支持声明boolean变量或参数.该bit数据类型不能一个的结果存储boolean表达直接,尽管它看起来很像一个.

离您最近的boolean数据类型是这样的:

-- 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
    ...
Run Code Online (Sandbox Code Playgroud)

更令人困惑的是,SQL Server Management Studio 在显示结果时会bit像处理一样boolean,而ADO.NET bitSystem.Boolean在来回传递数据时进行映射.

更新:要回答您的最新问题,请使用语句中的case when ... then 1 else 0 end语法select.