0

编辑:

对不起,我一开始没有澄清。CompanyType 是 smallint,不为 null,TotalLicenses 是 int,null

基本上我只想检查TotalLicenses > 0 if C.CompanyType != 4

我查看了一些参考指南和教程,但找不到一个可以确认我正在使用 CASE WHEN 我打算如何在此示例中使用。

当 Expiration 大于或等于今天的日期时,我尝试将“Purchased”列更新为 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
4

3 回答 3

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
    )
于 2013-10-23T14:31:08.990 回答
1

第一,我认为案例结构在不同的 DBMS 之间有所不同。因此,以下内容可能不适用于您。

第二,当您没有指定 Else 时,案例返回 NULL。与 NULL 的比较也是 NULL,因此不会返回该行。这意味着您需要一个默认情况。

所以你可能应该写:

and C.TotalLicenses > 
    Case 
        when C.CompanyType != 4 THEN 0
        else -1
    END
于 2013-10-23T14:33:27.007 回答
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)
于 2013-10-23T14:50:25.480 回答