我正在使用SQL Server 2012,并且我认为对于内联视图来说这是一个非常奇怪的问题。

首先,我有以下查询:

select
    parcelnumber,
    sectionname,
    value.Value CalculatedValue,
    overriddenvalue.Value OverriddenValue,
    homk.Value CalculatedHomeownersValue,
    cv.CharacteristicValue,
    pdt.PropertyDataTypeID
from landsections ls
join lands l
  on ls.landid = ls.landid
join parcels p
  on l.ParcelID = p.ParcelID
join CharacteristicValueGroups cvg
  on cvg.ParcelObjectID = ls.ParcelObjectID
join CharacteristicValues cv
  on cvg.CharacteristicValueGroupID = cv.CharacteristicValueGroupID
join Characteristics c
  on cv.CharacteristicID = c.CharacteristicID
join CharacteristicDetails cd
  on c.CharacteristicDetailsID = cd.CharacteristicDetailsID
 and cd.name = '09 Composite Adjustment'
join PropertyDataTypes pdt
  on cv.PropertyDataTypeID = pdt.PropertyDataTypeID
 and pdt.Label = 'Type'
join parcelobjectvalues value
  on value.ParcelObjectID = ls.ParcelObjectID
 and value.valuetype = 'CalculatedAdjustedValue'
left join parcelobjectvalues overriddenvalue
  on overriddenvalue.ParcelObjectID = ls.ParcelObjectID
 and overriddenvalue.valuetype = 'OverriddenValue'
join parcelobjectvalues homk
  on homk.ParcelObjectID = ls.ParcelObjectID
 and homk.valuetype = 'CalculatedHomeownersValue'
where year(appraisaldate) = 2016
  and CharacteristicValue like '%F%'


注意where子句中的最后一项。该查询返回0个结果,这应证明没有包含的CharacteristicValue列的行。

现在运行以下查询:

select *
from
(
    select
        *,
        case
            when CharacteristicValue like '%A' then convert(int, substring(characteristicvalue, 1, len(characteristicvalue) - 1))
            when CharacteristicValue like '%K' then convert(int, substring(characteristicvalue, 1, len(characteristicvalue) - 1))
            when isnumeric(CharacteristicValue) = 1 then convert(int, characteristicvalue)
            else 0
        end Quantity
    from
    (
        select
            parcelnumber,
            sectionname,
            value.Value CalculatedValue,
            overriddenvalue.Value OverriddenValue,
            homk.Value CalculatedHomeownersValue,
            cv.CharacteristicValue,
            pdt.PropertyDataTypeID
        from
            landsections ls
            join lands l on ls.landid = ls.landid
            join parcels p on l.ParcelID = p.ParcelID
            join CharacteristicValueGroups cvg on cvg.ParcelObjectID = ls.ParcelObjectID
            join CharacteristicValues cv on cvg.CharacteristicValueGroupID = cv.CharacteristicValueGroupID
            join Characteristics c on cv.CharacteristicID = c.CharacteristicID
            join CharacteristicDetails cd on c.CharacteristicDetailsID = cd.CharacteristicDetailsID and cd.name = '09 Composite Adjustment'
            join PropertyDataTypes pdt on cv.PropertyDataTypeID = pdt.PropertyDataTypeID and pdt.Label = 'Type'
            join parcelobjectvalues value on value.ParcelObjectID = ls.ParcelObjectID and value.valuetype = 'CalculatedAdjustedValue'
            left join parcelobjectvalues overriddenvalue on overriddenvalue.ParcelObjectID = ls.ParcelObjectID and overriddenvalue.valuetype = 'OverriddenValue'
            join parcelobjectvalues homk on homk.ParcelObjectID = ls.ParcelObjectID and homk.valuetype = 'CalculatedHomeownersValue'
        where
            year(appraisaldate) = 2016
    ) Tbl
) Tbl2
where
    CharacteristicValue like '%A' and Quantity > 10


请注意,我已经删除了%F%检查。由于第一个查询证明CharacteristicValue列中没有带F的行,因此我认为外部查询可以依赖于此。

但是,我得到这个:


将varchar值'F'转换为数据类型int时,转换失败。


我认为发生这种情况的唯一方法就是忽略pdt.Label = 'Type',但是对于我一生,我无法理解为什么。

最佳答案

您的视图筛选依据:

where year(appraisaldate) = 2016 and CharacteristicValue like '%F%'


这意味着可能存在仅满足CharacteristicValue like '%F%'的行。

在T-SQL中未指定执行顺序。当试图保护可能需要进行某些检查的计算时,这是一个问题。

通常,case ... when是可靠的保护措施。我认为没有理由不支持您。因此,您可以执行when CharacteristicValue like '%A' and CharacteristicValue not like '%F%'

或者,您可以使用始终安全的TRY_CONVERT

where不是可靠的保护者。在另一个答案中,您发布了此功能,但这只是巧合。使用不同的查询计划,可能会失败。计划因多种原因而改变。

10-07 15:30