sql server - Replacement for scalar function by inline function -
i have proc uses scalar function, in select statement twice described below.is better performance wise replace inline function when deal millions of records.if should it
create function getcategory ( @shopping_store char(4), @cart_type char(2), @category_type int, @index_cat_type int ) returns int begin if @shopping_store in ('1111','1222','3222') , @cart_type in ('120') return -@category_type else if @shopping_store in ('4333','54322') , @cart_type in ('120') return @index_cat_type else begin if @shopping_store in ('32214','5432','5654') return @category_type else return -@index_cat_type end return @category_type end
all if elses can converted single case expression. lets convert scalar function inline table valued function. performance benefit of should substantial state have millions of rows. took liberty of changing @cart_type char(4) since garethd pointed out can't contain '120'. used explicit multiplication when wanting negative number because easy miss - @ beginning, clear when multiply negative 1.
create function getcategory ( @shopping_store char(4), @cart_type char(4), @category_type int, @index_cat_type int ) returns table return select case when @shopping_store in ('1111','1222','3222') , @cart_type in ('120') -1 * @category_type when @shopping_store in ('4333','54322') , @cart_type in ('120') @index_cat_type when @shopping_store in ('32214','5432','5654') @category_type else -1 * @index_cat_type end categorytype
Comments
Post a Comment