替换UDF的临时表

时间:2015-06-08 15:03:03

标签: sql-server tsql

我尝试创建一个临时表变量,但不知道如何将它传递给过程,以便过程可以将操作插入@temp_table

CREATE FUNCTION dbo.function1 (
@DocId int
)
RETURNS @ret TABLE 
  ( 
  [Id] int ,
  [DocId] int ,
  [EatId] int ,
  [WasteCold] decimal(5, 4) ,
  [WasteHot] decimal(5, 4) ,
  [NetMass] decimal(10, 3) ,
  [Yield] decimal(10, 3) ,
  [EmbeddedRecipeId] int)
AS
BEGIN

  CREATE TABLE #RecipeTempTable(
    [Id] int NOT NULL,
    [DocId] int NULL,
    [EatId] int NULL,
    [WasteCold] decimal(5, 4) NULL,
    [WasteHot] decimal(5, 4) NULL,
    [NetMass] decimal(10, 3) NULL,
    [Yield] decimal(10, 3) NULL,
    [EmbeddedRecipeId] int NULL)

    EXEC FullListOfProducts @DocId
    INSERT @ret
    SELECT * FROM #RecipeTempTable8
    RETURN
END

1 个答案:

答案 0 :(得分:1)

您无法从函数调用存储过程(按设计)。不要这样做,只需让函数的调用者设置临时表并调用存储过程,然后只使用你正在使用函数的临时表。所以,而不是:

select *
    from dbo.function1(@DocId) f
    join <some other table> t on <some criteria>

做的:

CREATE TABLE #RecipeTempTable(
    [Id] int NOT NULL,
    [DocId] int NULL,
    [EatId] int NULL,
    [WasteCold] decimal(5, 4) NULL,
    [WasteHot] decimal(5, 4) NULL,
    [NetMass] decimal(10, 3) NULL,
    [Yield] decimal(10, 3) NULL,
    [EmbeddedRecipeId] int NULL)

    EXEC FullListOfProducts @DocId

select *
        from #RecipeTempTable f
        join <some other table> t on <some criteria>
相关问题