2013-05-16 19 views
1

無論使用單獨以下功能標量函數表現良好(dbo.getCorrectInventoryKey(Inventory_Key) and dbo.getEntityFullName2(Inventory_Key, '.')),但一起使用時(dbo.getEntityFullName2(dbo.getCorrectInventoryKey(Inventory_Key), '.'))查詢永遠不會返回......一起使用超時

CREATE function [dbo].[getEntityFullName2] (@inventory_Key varchar(100),@delim varchar(5)) returns varchar(1000) as 
begin 

    declare 
    @continue bit,   /* loop controller */ 
    @dIndex  int,   /* delimeter index character position */ 
    @key  varchar(100), /* temp var used to hold one key through the loop */ 
    @retStr  varchar(1000) /* return string (full item name) */ 


    /* set the default values for our local variables */ 
    set @continue = 1 
    set @dIndex = 0 
    set @retStr = '' 


    /* loop until we make it through all items in the key */ 
    while(@continue = 1) begin 
     /* return the next index of the '-' character */ 
     set @dIndex = charIndex('-',@inventory_Key,@dIndex+1) 

     /* if we didn't find another occurance, then we know we are done looping */ 
     if(@dIndex = 0) begin 
      set @dIndex = len(@inventory_Key) 
      set @continue = 0 
     end 

     /* set the current key value */ 
     set @key = left(@inventory_Key,@dIndex) 
     /* check to see if we need to strip off a right '-' character */ 
     set @key = case right(@key,1) when '-' then left(@key,len(@key)-1) else @key end 

     /* concat our return string with the value from the DB */ 
     set @retStr = @retStr + isNull((select entity from dbo.tbl_Inventory_Entities where inventory_key LIKE (@key)),'[not defined]') + @delim 
    end 

    /* trim off the extra '-' character we inserted */ 
    set @retStr = left(@retStr,len(@retStr)-len(@delim)) 

    /* return the value */ 
    return @retStr 
end 

CREATE FUNCTION [dbo].[getCorrectInventoryKey](@key varchar(100)) 
RETURNS varchar(100) 
AS 
BEGIN 

DECLARE @returnkey varchar(100) 

SELECT @returnkey = CASE 
WHEN @key LIKE ('1001-2002-3003-4016%') OR @key LIKE ('1010-2002-3003-4016%') OR @key LIKE ('1020-2002-3003-4016%') THEN '1001-2002-3003-4016' 
WHEN (@key LIKE ('1001-2002-3003-4005') OR @key LIKE ('1001-2002-3003-4006')) OR (@key LIKE ('1010-2002-3003-4005') OR @key LIKE ('1010-2002-3003-4006')) OR (@key LIKE ('1020-2002-3003-4005') OR @key LIKE ('1020-2002-3003-4006')) THEN '1001-2002-3003' 
WHEN @key LIKE ('1001-2002-3004%') OR @key LIKE ('1010-2002-3004%') OR @key LIKE ('1020-2002-3004%') THEN '1001-2002-3004' 
WHEN @key LIKE ('1001-2002-3005%') OR @key LIKE ('1010-2002-3005%') OR @key LIKE ('1020-2002-3005%') THEN '1001-2002-3005' 
WHEN @key LIKE ('1001-2002-3007%') OR @key LIKE ('1010-2002-3007%') OR @key LIKE ('1020-2002-3007%') THEN '1001-2002-3007' 
WHEN @key LIKE ('1001-2001-3001%') OR @key LIKE ('1010-2001-3001%') OR @key LIKE ('1020-2001-3001%') THEN '1001-2001-3001' 
WHEN @key LIKE ('1001-2002-3006%') OR @key LIKE ('1010-2002-3006%') OR @key LIKE ('1020-2002-3006%') THEN '1001-2002-3006' 
WHEN @key LIKE ('1001-2001-3020%') OR @key LIKE ('1010-2001-3020%') OR @key LIKE ('1020-2001-3020%') THEN '1001-2001-3020' 
WHEN @key LIKE ('1003-2010%') THEN '1003-2010' 
ELSE NULL 
END 

RETURN @returnkey 

END 
+1

查詢在哪裏? –

+0

這是無關緊要的,因爲無論它永遠不會返回的上下文,但是一起使用函數在頂部被注意到...... dbo.getEntityFullName2(dbo.getCorrectInventoryKey(Inventory_Key),'。') –

+1

您在頂部使用這兩個函數彼此之間可能永遠不會產生結果。我猜如果你讓這個運行直到時間結束,它仍然不會完成。你可能會想重新考慮你的整個解決方案。如果這不是一個選項,您可能需要嘗試在表中執行第一個函數,然後在該結果集上運行第二個函數。只是一個建議。 – Zane

回答

1

你確定你的函數getCorrectInventoryKey在每種情況下都返回非空值嗎?它看起來至少是可能它返回NULL(基於CASE語句的ELSE子句),在這種情況下,您的函數getEntityFullName2將進入無限循環。如果@inventory_Key參數爲NULL,則

set @dIndex = charIndex('-',@inventory_Key,@dIndex+1) 

將設置@dI​​ndex爲null,下面的代碼不能執行:

if(@dIndex = 0) begin 
    set @dIndex = len(@inventory_Key) 
    set @continue = 0 
end 

變化

set @dIndex = charIndex('-',@inventory_Key,@dIndex+1) 

set @dIndex = ISNULL(charIndex('-',@inventory_Key,@dIndex+1), 0) 
+0

*臉掌*謝謝! –

+0

大聲笑,它發生在我們所有人身上。 –