简体   繁体   English

具有多个OR条件的Where子句中的错误

[英]error in Where clause with multiple OR conditions

Why does the query below not work? 为什么下面的查询不起作用? I need to get data for each of the following numbers: 我需要获取以下每个数字的数据:

select * 
from dbo.BAS_GDW_TMO_PARCEL_EBILL_ALL_ARCHIVE_AUDIT_RESULT_TBL
where TMO_TRACK_NBR in (964855234, 141329655, 138150364, '3F3857')

You are mixing types in the IN . 您正在IN中混合类型。 An IN list supports values of only one type. IN列表仅支持一种类型的值。 If you mix strings and numbers, then the strings are converted to numbers. 如果您混合使用字符串和数字,则字符串将转换为数字。 And you get an error because the string does not represent a valid number. 而且您会收到错误消息,因为字符串不代表有效数字。

Don't mix types. 不要混合类型。 Use the appropriate type for comparisons. 使用适当的类型进行比较。 If TMO_TRACK_NBR is a string, use strings for all the values: 如果TMO_TRACK_NBR是字符串,则对所有值使用字符串:

select * 
from dbo.BAS_GDW_TMO_PARCEL_EBILL_ALL_ARCHIVE_AUDIT_RESULT_TBL
where TMO_TRACK_NBR in ('964855234', '141329655', '138150364', '3F3857');

According to hte SQL IN documentation https://docs.microsoft.com/en-us/sql/t-sql/language-elements/in-transact-sql 根据SQL IN文档https://docs.microsoft.com/zh-cn/sql/t-sql/language-elements/in-transact-sql

Is a list of expressions to test for a match. 是要测试匹配项的表达式列表。 All expressions must be of the same type as test_expression . 所有表达式必须与test_expression具有相同的类型

You are testing integer values (964855234) and string ( '3F3857' ) in the IN clause. 您正在IN子句中测试整数值(964855234)和字符串('3F3857')。

You have to check wich datatype is the TMO_TRACK_NBR column and test data with the right datatype 您必须检查数据类型是否为TMO_TRACK_NBR列并使用正确的数据类型测试数据

All the data in the in clause should be of the same type in子句中的所有数据应为同一类型

So it should be either this 所以应该是这个

select * 
from dbo.BAS_GDW_TMO_PARCEL_EBILL_ALL_ARCHIVE_AUDIT_RESULT_TBL
where TMO_TRACK_NBR in ('964855234', '141329655', '138150364', '3F3857')

or this 或这个

select * 
from dbo.BAS_GDW_TMO_PARCEL_EBILL_ALL_ARCHIVE_AUDIT_RESULT_TBL
where TMO_TRACK_NBR in (964855234, 141329655, 138150364, 3F3857)

Since the TMO_TRACK_NBR can contain an F it tells me its not an INT. 由于TMO_TRACK_NBR可以包含F,因此它告诉我它不是INT。 If you don't put the quotes around the other numbers as well the IN operation will try to convert them all to INT (including the '3F3857'). 如果您也不要将引号放在其他数字周围,则IN操作将尝试将所有引号都转换为INT(包括“ 3F3857”)。

select * 
from dbo.BAS_GDW_TMO_PARCEL_EBILL_ALL_ARCHIVE_AUDIT_RESULT_TBL 
where TMO_TRACK_NBR in ('964855234', '141329655', '138150364', '3F3857')

Next time placing the error should be good to. 下次放置错误应该很好。 I think you got: 我想你得到了:

Conversion failed when converting the varchar value '3F3857' to data type int.

Definetely, the reaseon is that you use data of two types in your condition. 明确地,原因是您在条件中使用两种类型的数据。

    select * 
    from dbo.BAS_GDW_TMO_PARCEL_EBILL_ALL_ARCHIVE_AUDIT_RESULT_TBL
    where TMO_TRACK_NBR in (
964855234 --int
, 141329655 --int
, 138150364 --int
, '3F3857' --varchar
)

Just make it like this: 就是这样:

select * 
from dbo.BAS_GDW_TMO_PARCEL_EBILL_ALL_ARCHIVE_AUDIT_RESULT_TBL
where TMO_TRACK_NBR in (
'964855234' --varchar
, '141329655' --varchar
, '138150364' --varchar
, '3F3857' --varchar
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM