我有以下查询:
SELECT c004, mesosafe, CAST(SUBSTRING (c000 ,1 , 5) as int) as sortorder, c001, c000, c002
FROM DE_DATA.dbo.t309
WHERE c001 IS NOT NULL
AND c002 = 1
AND mesocomp = 'EMTD'
AND mesoyear IN (
SELECT MAX(mesoyear) AS currmesoyear
FROM DE_DATA.dbo.t001
)
AND CAST(SUBSTRING (c000 ,1 , 5) as int) < 101
ORDER BY c000 ASC;
此查询失败,因为无法转换 的某些值,c000
因为它们看起来像008-
而不是00052-
。在这里mesoyear
问(mesoyear IN ...)。如果我单独查询这个 SQL 部分,我会得到1344
结果。
另一方面,此查询有效:
SELECT c004, mesosafe, CAST(SUBSTRING (c000 ,1 , 5) as int) as sortorder, c001, c000, c002
FROM DE_DATA.dbo.t309
WHERE c001 IS NOT NULL
AND c002 = 1
AND mesocomp = 'EMTD'
AND mesoyear IN (
'1344'
)
AND CAST(SUBSTRING (c000 ,1 , 5) as int) < 101
ORDER BY c000 ASC;
那么硬编码值和 SQL 查询有什么区别呢?
编辑:
我认为原因是子查询的评估晚于主查询。是真的吗?我能做些什么呢?