IF Else inside Inline Table 值函数
IF Else inside Inline Table Valued Functions
是否可以在内联 table 值函数中使用 If Else。我有一个标量函数,我正在使用 If Else Condition 但是,该查询需要太多时间来执行,我想将其转换为内联 table 值函数。请建议我该怎么做。
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
以上是我的标量函数。知道如何根据内联 table 值函数查找记录。
我在尝试什么
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
您可以使用 CASE 语句,
CREATE FUNCTION [dbo].[TestFunctionFindSum](@ProductID bigint,
@TotalType nvarchar(200),
@OwnerUserID bigint,
@OrganizationID bigint,
@BusinessUnitID bigint,
@InventoryID bigint )
RETURNS TABLE
AS RETURN
SELECT
CASE WHEN @TotalType = 'QuantityOnHand' THEN
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)
WHEN @TotalType = 'QuantityBooked' THEN
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)
WHEN @TotalType = 'ProjectedQuantityOnHand' THEN
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 AS OutputValue
GO
是否可以在内联 table 值函数中使用 If Else。我有一个标量函数,我正在使用 If Else Condition 但是,该查询需要太多时间来执行,我想将其转换为内联 table 值函数。请建议我该怎么做。
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
以上是我的标量函数。知道如何根据内联 table 值函数查找记录。
我在尝试什么
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
您可以使用 CASE 语句,
CREATE FUNCTION [dbo].[TestFunctionFindSum](@ProductID bigint,
@TotalType nvarchar(200),
@OwnerUserID bigint,
@OrganizationID bigint,
@BusinessUnitID bigint,
@InventoryID bigint )
RETURNS TABLE
AS RETURN
SELECT
CASE WHEN @TotalType = 'QuantityOnHand' THEN
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)
WHEN @TotalType = 'QuantityBooked' THEN
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)
WHEN @TotalType = 'ProjectedQuantityOnHand' THEN
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 AS OutputValue
GO