仅当在where子句中使用参数时才出现被零除的错误

mftmpeh8  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(370)

有两个表,外部表a和视图表b。外部表中的数据通过polybase连接到hadoop。视图表是联合所有外部表a和其他外部表(类似于a)。
例如:

A:
--------------------
number  | time
--------------------
0       |2018-09-10
1       |2018-09-10
2       |2018-09-10
--------------------

B:
--------------------
number  | time
--------------------
0       |2018-09-10
1       |2018-09-10
2       |2018-09-10
3       |2018-09-11
--------------------

现在,我不知道为什么下面的sql会使结果不同?
我在这些sql中发现了不同的执行顺序。在情况1和2中,首先执行“选择5.0/number”。在情况3和4中,where子句首先执行。
案例1

DECLARE @date datetime ='2018-09-10';
select 5.0/number  
from A 
where time = @date and number > 0 

Result: 
Cannot execute the query "Remote Query" against OLE DB provider "SQLNCLI11" for linked server "(null)". [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Divide by zero error encountered.

案例2

DECLARE @date datetime ='2018-09-10';
select 5.0/temp.number 
from
(
    select number as number 
    from A
    where time = @date and number > 0 
)temp

Result: 
Cannot execute the query "Remote Query" against OLE DB provider "SQLNCLI11" for linked server "(null)". [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Divide by zero error encountered.

案例3

select 5.0/number 
from A 
where time = '2018-09-10' and number > 0 

Result:
5.0
2.5

案例4(来自b)

DECLARE @date datetime ='2018-09-10';
select 5.0/number  
from B
where time = @date and number > 0 

Result:
5.0
2.5

谢谢!!

ctehm74n

ctehm74n1#

这是否也会产生错误?

DECLARE @date datetime ='2018-09-10';
select 5.0/temp.number 
from
(
    select time, number
    from A
    where number > 0 
)temp
where temp.time = @date

相关问题