编辑:
对不起,我没有在开始时澄清。 CompanyType为smallint,不为null,TotalLicenses为int,null
基本上我只想查看TotalLicenses > 0 if C.CompanyType != 4
我查看了一些参考指南和教程,但是在这个例子中我找不到确认我正在使用CASE的方法。
当Expiration大于或等于Today的日期时,我试图将“已购买”列更新为1,DemoLicense等于0,如果公司类型不是4,则TotalLicenses大于0。如果是4它可以是0
update WebCatalog.Published.DemoTracking
set Purchased = 1
from WebCatalog.Published.Company C
inner join WebCatalog.Published.RCompany RC
on C.RCompany = RC.Link
inner join WebCatalog.Published.DemoTracking DT
on C.PKey = DT.CompanyPKey and DT.Purchased = 0
where RC.Expiration >= Cast(GETDATE() as Date) and RC.DemoLicense = 0
and C.TotalLicenses >
Case
when C.CompanyType != 4 THEN 0
END
答案 0 :(得分:2)
我想你想要:
and ( C.CompanyType <> 4 AND C.TotalLicenses > 0
OR C.CompanyType = 4 AND C.TotalLicenses >= 0
)
可以简化(假设这两列不可为空)到:
and ( C.TotalLicenses > 0
OR C.CompanyType = 4 AND C.TotalLicenses = 0
)
,如果Company.TotalLicenses
永远不会有负值,可以进一步简化为:
and ( C.TotalLicenses > 0
OR C.CompanyType = 4
)
答案 1 :(得分:1)
2nd当你没有指定Else时,case返回NULL。与NULL的比较也是NULL,因此不会返回该行。这意味着您需要一个默认案例。
所以你应该写一下:
and C.TotalLicenses >
Case
when C.CompanyType != 4 THEN 0
else -1
END
答案 2 :(得分:0)
与你所描述的方式没有多大区别:
update WebCatalog.Published.DemoTracking
set Purchased = 1
from WebCatalog.Published.Company C
inner join WebCatalog.Published.RCompany RC
on C.RCompany = RC.Link
inner join WebCatalog.Published.DemoTracking DT
on C.PKey = DT.CompanyPKey and DT.Purchased = 0
where RC.Expiration >= Cast(GETDATE() as Date) and RC.DemoLicense = 0
and (C.TotalLicenses > 0 or C.CompanyType = 4)