1
是否有可能使用If Else內聯聯表中的值函數。我有一個標量函數,我使用If Else Condition但是,那個查詢需要太多時間來執行,我想將它轉換爲內聯表值函數。請建議我怎樣才能做到這一點。IF內部聯機表值函數
ALTER FUNCTION [dbo].[TestFunctionFindSum]
(
@ProductID bigint,
@TotalType nvarchar(200),
@OwnerUserID bigint,
@OrganizationID bigint,
@BusinessUnitID bigint,
@InventoryID bigint
)
RETURNS decimal(32,9)
AS
BEGIN
-- declare the return variable here
declare @OutputValue decimal(32,9)
Declare @locationValue int =0
---------------------------------------------------------------------
-- Getting Inventory Items Total as per the Total Type supplied
---------------------------------------------------------------------
IF @TotalType = 'QuantityOnHand'
BEGIN
set @OutputValue = isnull((select sum(ii.[QuantityOnHand])
from dbo.InventoryItems ii, Inventory i
where ii.ActiveStatus=1
and ii.ProductID = @ProductID
and ii.InventoryID = i.InventoryID
AND i.OwnerUserGroupID = case @OwnerUserID
when 0 then i.OwnerUserGroupID else @OwnerUserID end
AND i.OrganizationID = case @OrganizationID
when 0 then i.OrganizationID else @OrganizationID end
AND i.BusinessUnitID = case @BusinessUnitID
when 0 then i.BusinessUnitID else @BusinessUnitID end
AND i.InventoryID = case @InventoryID
when 0 then i.InventoryID else @InventoryID end), 0.00)
END
ELSE IF @TotalType = 'QuantityBooked'
BEGIN
set @OutputValue = isnull((select sum(ii.QuantitySold)
from dbo.InventoryItems ii, Inventory i
where ii.ActiveStatus=1
and ii.ProductID = @ProductID
and ii.InventoryID = i.InventoryID
AND i.OwnerUserGroupID = case @OwnerUserID
when 0 then i.OwnerUserGroupID else @OwnerUserID end
AND i.OrganizationID = case @OrganizationID
when 0 then i.OrganizationID else @OrganizationID end
AND i.BusinessUnitID = case @BusinessUnitID
when 0 then i.BusinessUnitID else @BusinessUnitID end
AND i.InventoryID = case @InventoryID
when 0 then i.InventoryID else @InventoryID end), 0.00)
END
ELSE IF @TotalType = 'ProjectedQuantityOnHand'
BEGIN
set @OutputValue = isnull((select (sum(ii.QuantityOnHand) - sum(ii.QuantitySold))
from dbo.InventoryItems ii, Inventory i
where ii.ActiveStatus=1
and ii.ProductID = @ProductID
and ii.InventoryID = i.InventoryID
AND i.OwnerUserGroupID = case @OwnerUserID
when 0 then i.OwnerUserGroupID else @OwnerUserID end
AND i.OrganizationID = case @OrganizationID
when 0 then i.OrganizationID else @OrganizationID end
AND i.BusinessUnitID = case @BusinessUnitID
when 0 then i.BusinessUnitID else @BusinessUnitID end
AND i.InventoryID = case @InventoryID
when 0 then i.InventoryID else @InventoryID end), 0.00)
END
return @OutputValue
END
以上是我的標量函數..任何想法如何找到基於內聯表值函數的記錄。
我試圖
CREATE FUNCTION [dbo].[TestFunctionFindSum](@ProductID bigint,
@TotalType nvarchar(200),
@OwnerUserID bigint,
@OrganizationID bigint,
@BusinessUnitID bigint,
@InventoryID bigint )
RETURNS TABLE
AS RETURN
IF @TotalType = 'QuantityOnHand'
BEGIN
isnull((select sum(ii.[QuantityOnHand])
from dbo.InventoryItems ii, Inventory i
where ii.ActiveStatus=1
and ii.ProductID = @ProductID
and ii.InventoryID = i.InventoryID
AND i.OwnerUserGroupID = case @OwnerUserID
when 0 then i.OwnerUserGroupID else @OwnerUserID end
AND i.OrganizationID = case @OrganizationID
when 0 then i.OrganizationID else @OrganizationID end
AND i.BusinessUnitID = case @BusinessUnitID
when 0 then i.BusinessUnitID else @BusinessUnitID end
AND i.InventoryID = case @InventoryID
when 0 then i.InventoryID else @InventoryID end), 0.00)
END
GO
不,如果因爲你只能有一個說法,你不能使用。你需要在你的select子句中有一個case -statement來挑選正確的總和,也許使用派生表或CTE來更清楚。 –