我正在为我的网络库存系统LanSweeper中的报告构建查询。这是向我展示我想要完成的事情的最小例子。
Select Distinct Top 1000000 tblAssets.AssetName,
(Select Case When tblRegistry.Value = 2 Then 'Notify Only'
When tblRegistry.Value = 3 Then 'Download Notify'
When tblRegistry.Value = 4 Then 'Scheduled'
When tblRegistry.Value = 5 Then 'Download and Custom'
Else tblRegistry.Value End As RegistryValue
From tblAssets As TA Left Join tblRegistry On tblRegistry.AssetID = TA.AssetID
Where tblRegistry.Valuename = 'AUoptions' And TA.AssetID = tblAssets.AssetID)
As AUOption
From tblAssets
问题是表tblRegistry
是我告诉我的系统库存的注册表项和值的集合。由于我正在为其他报告清点几个键,因此该表中包含字符串和整数的混合。我没有问题从表中拉出我想要的值。我的问题源于试图给出数值的友好字符串。在上述情况下,Windows计算机上的自动更新设置。
我得到的错误是:
Conversion failed when converting the nvarchar value 'C62E10' to data type int
这很有意义,因为它显然不是整数。似乎tblRegistry.Value
正在评估所有CASE
,而不是WHERE
clase Where tblRegistry.Valuename = 'AUoptions'
1}过滤掉的CASE
} p>
有没有办法从{{1}}语句中过滤出非数值?
答案 0 :(得分:1)
因此,tblRegistry.Value
为nvarchar
,但有些值是数字。
如果在case
中使用整数,则强制DBMS转换为您的类型,在某些情况下这是不可能的。请改用varchar,因此必须比较字符串:
Select Distinct Top 1000000 tblAssets.AssetName,
(Select Case When tblRegistry.Value = '2' Then 'Notify Only'
When tblRegistry.Value = '3' Then 'Download Notify'
When tblRegistry.Value = '4' Then 'Scheduled'
When tblRegistry.Value = '5' Then 'Download and Custom'
Else tblRegistry.Value End As RegistryValue
From tblAssets As TA Left Join tblRegistry On tblRegistry.AssetID = TA.AssetID
Where tblRegistry.Valuename = 'AUoptions' And TA.AssetID = tblAssets.AssetID)
As AUOption
From tblAssets