gpt4 book ai didi

sql - 除以零错误仅当在where子句中使用参数时

转载 作者:行者123 更新时间:2023-12-02 20:33:21 27 4
gpt4 key购买 nike

有两个表,外部表A View 表B
外部表中的数据通过Polybase连接到Hadoop。
View 表将所有外部表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
--------------------

Now, I don't know why below SQL make results different?



而且我在这些SQL中发现了不同的执行顺序。在情况1和2中,首先执行“选择5.0 /数字”。在情况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

谢谢!!

最佳答案

这还会产生错误吗?

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

关于sql - 除以零错误仅当在where子句中使用参数时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52273337/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com