1

Можно ли использовать, если только внутри встроенная функция таблицы. У меня есть скалярная функция, в которой я использую If Else Condition, но этот запрос занимает слишком много времени для выполнения, что я хочу преобразовать в качестве функции таблицы Inline. пожалуйста, предложите способ, как я могу это сделать.IF Else in Inline 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 

выше - моя скалярная функция .. любая идея, как найти запись, основанную на встроенной табличной функции.

То, что я пытался

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 
+0

Нет, вы не можете использовать, если, потому что вы можете иметь только один оператор. Вам нужно иметь аргумент case в предложении select для выбора правильной суммы, возможно, с производной таблицей или CTE, чтобы сделать ее более понятной. –

ответ

1

Вы можете использовать саз как,

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