Sql 一起使用的标量函数超时

Sql 一起使用的标量函数超时,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,单独使用以下任一函数(dbo.getCorrectInventoryKey(Inventory\u Key)和dbo.getEntityFullName2(Inventory\u Key,'))),但如果同时使用(dbo.getEntityFullName2(dbo.getCorrectInventoryKey(Inventory\u Key),'))),查询将永远不会返回 CREATE function [dbo].[getEntityFullName2] (@inventory_Key

单独使用以下任一函数(
dbo.getCorrectInventoryKey(Inventory\u Key)和dbo.getEntityFullName2(Inventory\u Key,'))
),但如果同时使用(
dbo.getEntityFullName2(dbo.getCorrectInventoryKey(Inventory\u 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


您确定函数getCorrectInventoryKey在任何情况下都返回非空值吗?看起来它至少有可能返回NULL(基于CASE语句的ELSE子句),在这种情况下,函数getEntityFullName2将进入一个无限循环。如果@inventory\u Key参数为空,则

set @dIndex = charIndex('-',@inventory_Key,@dIndex+1)
将@dIndex设置为NULL,并且不会执行以下代码:

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

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


您确定函数getCorrectInventoryKey在任何情况下都返回非空值吗?看起来它至少有可能返回NULL(基于CASE语句的ELSE子句),在这种情况下,函数getEntityFullName2将进入一个无限循环。如果@inventory\u Key参数为空,则

set @dIndex = charIndex('-',@inventory_Key,@dIndex+1)
将@dIndex设置为NULL,并且不会执行以下代码:

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

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


这是不相关的,因为无论上下文如何,它都不会返回,但是函数的使用在顶部被注明。。。dbo.getEntityFullName2(dbo.getCorrectInventoryKey(Inventory_Key),“.”)将这两个函数放在一起使用可能永远不会产生结果。我猜如果你让它一直运行到时间的尽头,它仍然不会结束。您可能需要重新思考整个解决方案。如果这不是一个选项,您可能希望尝试在表中执行第一个函数,然后在该结果集中运行第二个函数。只是一个建议。天哪,我不明白为什么这个问题被否决了。完全合理的问题,并提供了足够的细节。下面是一个可能的答案。@KevinSuchlicki我的假设是它太本地化了。@Zane我看到的消息是“作为一个不真实的问题而关闭的”。但是感谢您提供一个可能的解释。我猜它相当狭窄,但只比这里讨论的大多数特定于应用程序的问题稍微窄一点。这无关紧要,因为无论上下文如何,它都不会返回,但函数的使用在顶部被注明。。。dbo.getEntityFullName2(dbo.getCorrectInventoryKey(Inventory_Key),“.”)将这两个函数放在一起使用可能永远不会产生结果。我猜如果你让它一直运行到时间的尽头,它仍然不会结束。您可能需要重新思考整个解决方案。如果这不是一个选项,您可能希望尝试在表中执行第一个函数,然后在该结果集中运行第二个函数。只是一个建议。天哪,我不明白为什么这个问题被否决了。完全合理的问题,并提供了足够的细节。下面是一个可能的答案。@KevinSuchlicki我的假设是它太本地化了。@Zane我看到的消息是“作为一个不真实的问题而关闭的”。但是感谢您提供一个可能的解释。我猜它是相当狭窄的,但仅比这里的大多数应用程序特定问题略窄。谢谢!哈哈,这发生在我们所有人身上。谢谢!哈哈,这发生在我们所有人身上。