Tsql 如何将比较结果存储到变量中
我想打印一个简单的语句 打印(1=1),我希望结果为真或1,但sql server告诉我: “=”附近的语法不正确。 为什么呢 这样的声明也会发生同样的情况Tsql 如何将比较结果存储到变量中,tsql,Tsql,我想打印一个简单的语句 打印(1=1),我希望结果为真或1,但sql server告诉我: “=”附近的语法不正确。 为什么呢 这样的声明也会发生同样的情况 declare @test bit set @test = (1=1) 总之,在不使用IF语句的情况下,如何“查看”比较返回的内容 declare @AgingAmount smallint set @AgingAmount = 500 select Amount, datediff(day,Batch.SubmitDate,getdat
declare @test bit
set @test = (1=1)
总之,在不使用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
更新:我问这个问题的原因是因为我正在调试为什么下面的语句
declare @AgingAmount smallint
set @AgingAmount = 500
select Amount, datediff(day,Batch.SubmitDate,getdate()) as Aging from myreporrt
where datediff(day,Batch.SubmitDate,getdate()) > @AgingAmount
即使老化时间为300,也将返回所有行
因此,我想测试datediff(day,datesubmited,getdate())>500是否返回true或false,但找不到显示此比较结果的方法。尽管SQL Server具有布尔类型的概念,而且它理解解析为
IF
和WHERE
子句中布尔值的表达式,它不支持声明布尔值
变量或参数。位
数据类型不能直接存储布尔
表达式的结果,即使它看起来可疑
最接近布尔值的数据类型是:
-- 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
...
——存储布尔测试的结果。
声明@result位
当1或0结束时,选择@result=case
--在其他地方利用上述结果。
如果@result=1
...
其他的
...
更令人困惑的是,SQL Server Management Studio在显示结果时将bit
视为boolean
,而在来回传递数据时,ADO.NET将bit
映射到System.boolean
更新:要回答您的最新问题,请在以下情况下使用案例。。。然后,在select
语句中,1 else 0 end
语法。您能提供您试图执行的确切查询的示例吗?我应该将此问题的标题改为“如何在不使用if语句的情况下显示比较结果”请参阅“官方”确认布尔数据类型不可声明也不可分配。嘘,谢谢!!使用SQL Server回到石器时代