久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

COALESCE - 保證短路?

COALESCE - guaranteed to short-circuit?(COALESCE - 保證短路?)
本文介紹了COALESCE - 保證短路?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

來自這個問題, 關于使用的簡潔答案COALESCE 來簡化復雜的邏輯樹.我考慮過短路的問題.

From this question, a neat answer about using COALESCE to simplify complex logic trees. I considered the problem of short circuiting.

例如,在大多數語言的函數中,參數會被完全評估,然后被傳遞到函數中.在 C:

For instance, in functions in most languages, arguments are fully evaluated and are then passed into the function. In C:

int f(float x, float y) {
    return x;
}

f(a, a / b) ; // This will result in an error if b == 0

這似乎不是 SQL Server 中 COALESCE函數"的限制:

That does not appear to be a limitation of the COALESCE "function" in SQL Server:

CREATE TABLE Fractions (
    Numerator float
    ,Denominator float
)

INSERT INTO Fractions VALUES (1, 1)
INSERT INTO Fractions VALUES (1, 2)
INSERT INTO Fractions VALUES (1, 3)
INSERT INTO Fractions VALUES (1, 0)
INSERT INTO Fractions VALUES (2, 0)
INSERT INTO Fractions VALUES (3, 0)

SELECT Numerator
    ,Denominator
    ,COALESCE(
        CASE WHEN Denominator = 0 THEN 0 ELSE NULL END,
        CASE WHEN Numerator <> 0 THEN Numerator / Denominator ELSE NULL END,
        0
    ) AS TestCalc
FROM Fractions

DROP TABLE Fractions

如果它在 Denominator = 0 時評估第二種情況,我希望看到如下錯誤:

If it were evaluating the second case when Denominator = 0, I would expect to see an error like:

Msg 8134, Level 16, State 1, Line 1
Divide by zero error encountered.

我發現了一些提及 與 Oracle 相關.還有一些使用 SQL 服務器.當您包含用戶定義的函數時,看起來短路可能會中斷.

I found some mentions related to Oracle. And some tests with SQL Server. Looks like the short-circuiting might break down when you include user-defined functions.

那么,ANSI 標準是否應該保證這種行為?

So, is this behavior supposed to be guaranteed by the ANSI standard?

推薦答案

我剛剛看了鏈接的文章,可以確認 COALESCE 和 ISNULL 的短路都可能失敗.

I just had a look at the linked article and can confirm short circuiting can fail for both COALESCE and ISNULL.

如果涉及任何子查詢,它似乎會失敗,但它適用于標量函數和硬編碼值.

It seems to fail if you have any sub-query involved, but it works fine for scalar functions and hard coded values.

例如

DECLARE @test INT
SET @test = 1
PRINT 'test2'
SET @test = COALESCE(@test, (SELECT COUNT(*) FROM sysobjects))
SELECT 'test2', @test
-- OUCH, a scan through sysobjects

COALESCE 根據 ANSI 標準實現.它只是 CASE 語句的簡寫.ISNULL 不是 ANSI 標準的一部分.第 6.9 節似乎沒有明確要求短路,但它確實暗示應該返回 when 語句中的第一個 true 子句.

COALESCE is implemented according to the ANSI standard. It is simply a shorthand for a CASE statement. ISNULL is not part of the ANSI standard. Section 6.9 does not seem to require short circuiting explicitly, but it does imply that the first true clause in the when statement should be returned.

這是一些適用于基于標量的函數的證明(我在 SQL Server 2005 上運行它):

Here is some proof that is works for scalar based functions (I ran it on SQL Server 2005):

CREATE FUNCTION dbo.evil
(
)
RETURNS int
AS
BEGIN
    -- Create an huge delay
    declare @c int
    select @c = count(*) from sysobjects a
    join sysobjects b on 1=1
    join sysobjects c on 1=1
    join sysobjects d on 1=1
    join sysobjects e on 1=1
    join sysobjects f on 1=1
    return @c / 0
END
go

select dbo.evil()
-- takes forever

select ISNULL(1,  dbo.evil())
-- very fast

select COALESCE(1,  dbo.evil())
-- very fast

這里有一些證據表明 CASE 的底層實現將執行子查詢.

Here is some proof that the underlying implementation with CASE will execute sub queries.

DECLARE @test INT
SET @test = 1
select
    case
        when @test is not null then @test
        when @test = 2 then (SELECT COUNT(*) FROM sysobjects)
        when 1=0 then (SELECT COUNT(*) FROM sysobjects)
        else (SELECT COUNT(*) FROM sysobjects)
    end
-- OUCH, two table scans. If 1=0, it does not result in a table scan.

這篇關于COALESCE - 保證短路?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Sql server table usage statistics(Sql server 表使用情況統計)
Relative path in t sql?(t sql中的相對路徑?)
Getting the last record in SQL in WHERE condition(在 WHERE 條件下獲取 SQL 中的最后一條記錄)
Query to get XML output for hierarchical data using FOR XML PATH in SQL Server(在 SQL Server 中使用 FOR XML PATH 查詢以獲取分層數據的 XML 輸出)
T-SQL IF statement embedded in a sum() function(嵌入在 sum() 函數中的 T-SQL IF 語句)
Table vs Temp Table Performance(表與臨時表性能)
主站蜘蛛池模板: 中文字字幕一区二区三区四区五区 | 日本网站免费在线观看 | 精品毛片 | 国产一极毛片 | 成人免费三级电影 | 北条麻妃一区二区三区在线观看 | 九九九视频精品 | 精品欧美 | 久久久久久国产精品免费免费狐狸 | 欧美成人一区二区三区片免费 | 国产精品久久久久久久久久三级 | 好姑娘影视在线观看高清 | 日本不卡一区二区三区 | 亚洲精品一区中文字幕乱码 | 亚洲精品视频免费观看 | 国产激情一区二区三区 | 国产在线播 | 人人色视频 | 亚洲在线一区二区 | 久久精品中文字幕 | 第四色影音先锋 | www.4hu影院 | 亚洲精品成人av久久 | 欧洲精品一区 | 日本不卡一区 | av黄色在线观看 | 国产精品久久久精品 | 免费观看av | 久久大陆 | 国产一区不卡 | 久久精品网 | 99精品久久久 | 欧美国产中文字幕 | 国产黄色在线观看 | 亚洲精品电影在线观看 | 亚洲视频一区在线 | 午夜爽爽爽男女免费观看 | 一区二区视屏 | 久久精品亚洲欧美日韩精品中文字幕 | 亚洲免费一区二区 | 国产一区二区在线免费观看 |