在SQL查询where子句中使用decode时出现问题

时间:2014-01-14 19:09:24

标签: sql oracle

我一直在考虑过去几个小时的问题,查询执行解码语句作为我的WHERE CLAUSE的一部分。

我能想到的最好的例子如下:

--This variable is normally populated by a query.
--Debugger confirms this is set to 'US' with no leading or tailing characters
Str_MyVar varchar2(10) := 'US';

-- Query 1 returns 16 rows (incorrect)
Select *
From myTable, table2
Where myTable.value = decode(Str_MyVar, 'US', table2.value, 0)

-- Query 2 returns 1 rows (correct)
Select *
From myTable, table2
Where myTable.value = decode('US', 'US', table2.value, 0)

现在,如果我更改填充Str_MyVar变量的查询,而不是将值'US'转换为1,将所有其他值转换为0并将其存储在数值变量中,则开始工作。

--This variable is normally populated by a query.
--Debugger confirms this is set to 1 with no leading or tailing characters
nbr_Myvar number := 1;

-- Query 1 returns 1 rows (correct)
Select *
From myTable, table2
Where myTable.value = decode(nbr_MyVar, 1, table2.value, 0);

在解码中使用数字数据类型的第二个示例正常工作。问题是为什么?当然我已经围绕这个问题进行了编码,但是我想知道这是一个oracle bug还是DECODE函数的怪癖。数据库是oracle 10.2.0.3

0 个答案:

没有答案
相关问题