in

Sql server evaluate all then part of case when


I create this fuction:

CREATE function ml.fn_Temp() returns @ResultTable table(Code int) as
    begin
        declare @Id int = (select 1 / 0)
        return 
    end

I run this quey :

declare @AccFieldKindName int = 9
select
    case 
        when @AccFieldKindName = 7 then 
                (select Code from ml.fn_Temp())
        when @AccFieldKindName = 9 then 67777
    end

at result it encounter with error : Divide by zero error encountered.
I think this is not correct to run code that its’s condition is not true.
why sql run all query. in reality ml.fn_Temp is aheavy query. In each run sql run this function so cause performance problems. Can I change this behaviour, to run only part of case when that it’s condition is true. I don’t want to change this query to If … else .



Source: https://stackoverflow.com/questions/70607144/sql-server-evaluate-all-then-part-of-case-when

Figma Academy — Learn advanced design tactics directly in Figma

Movilti — Your movie review app