SQL Server 2008,我可以在 udf 内的 select 语句中引用临时表吗?

发布于 2025-01-06 21:06:36 字数 1188 浏览 3 评论 0原文

我正在尝试对 udf 中的临时表运行选择查询。我找不到说明这是不允许的文档,但是当我将 tblDailyPricingAndVol 更改为 #dailyPricingAndVolBySymbol (当然是我的临时表)时,以下存储过程将无法编译。如果影响任何内容,临时表会在更高级别创建(在使用此函数的存储过程之前的存储过程中)...提前感谢

编辑: udf 只是作为调用它的存储过程的帮助程序。我试图用它查询临时表,因为它每次运行时都会被调用数千次。它检索然后聚合的数据位于包含数百万行的表中。因此,我将数据削减为数百条记录,放入临时表中。这将大大加快该功能的速度,尽管它仍然需要相当长的时间来运行。

ALTER FUNCTION dbo.PricingVolDataAvailableToDateProvided
    (@Ticker nchar(10),
     @StartDate DATE,
     @NumberOfDaysBack int)
    RETURNS nchar(5)
AS
    BEGIN

    DECLARE @Result nchar(5)
    DECLARE @RecordCount int

    SET @RecordCount = (SELECT COUNT(TradeDate) AS Expr1
        FROM (SELECT TOP (100) PERCENT TradeDate
           FROM tblDailyPricingAndVol WHERE (Symbol = @Ticker) AND (TradeDate IN
                 (SELECT TOP (@NumberOfDaysBack) CAST(TradingDate AS DATE) AS Expr1
                  FROM tblTradingDays
                  WHERE (TradingDate <= @StartDate)
                  ORDER BY TradingDate DESC))
                  ORDER BY TradeDate DESC) AS TempTable)

    IF @RecordCount = @NumberOfDaysBack
        SET @Result = 'True'
    ELSE
        SET @Result = 'False'

    RETURN @Result

    END

I'm trying to run a select query on a temporary table within a udf. I can't find documentation stating this isn't allowed, yet the below stored procedure won't compile when I change tblDailyPricingAndVol to #dailyPricingAndVolBySymbol (my temporary table of course. The temp table is created at a higher level (in a stored procedure before the stored procedure that uses this function) if that affects anything... thanks in advance.

Edit:
The udf is meant to just be a helper for the stored procedure that calls it.. I'm trying to query a temporary table with it due to the fact that it'll get called thousands of times each time it runs. The data that it retrieves and then aggregates is in a table with millions of rows. So I pare down the data into several hundred records, into the temporary table. This will speed the function up dramatically, even though it'll still take a fair bit of time to run.

ALTER FUNCTION dbo.PricingVolDataAvailableToDateProvided
    (@Ticker nchar(10),
     @StartDate DATE,
     @NumberOfDaysBack int)
    RETURNS nchar(5)
AS
    BEGIN

    DECLARE @Result nchar(5)
    DECLARE @RecordCount int

    SET @RecordCount = (SELECT COUNT(TradeDate) AS Expr1
        FROM (SELECT TOP (100) PERCENT TradeDate
           FROM tblDailyPricingAndVol WHERE (Symbol = @Ticker) AND (TradeDate IN
                 (SELECT TOP (@NumberOfDaysBack) CAST(TradingDate AS DATE) AS Expr1
                  FROM tblTradingDays
                  WHERE (TradingDate <= @StartDate)
                  ORDER BY TradingDate DESC))
                  ORDER BY TradeDate DESC) AS TempTable)

    IF @RecordCount = @NumberOfDaysBack
        SET @Result = 'True'
    ELSE
        SET @Result = 'False'

    RETURN @Result

    END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿忘初心 2025-01-13 21:06:36

正如其他发帖者所提到的,您不能在 UDF 中使用临时表。您可以做的是传递用户定义的表< /a> 到你的函数。

用户定义的表类型

在SQL Server 2008中,用户定义的表类型是用户定义的类型
表示表结构的定义。您可以使用
用户定义的表类型来声明存储的表值参数
过程或函数,或声明您想要的表变量
在批处理中或在存储过程或函数的主体中使用。

更改代码的快速修复可能是

CREATE TYPE DailyPricingAndVolBySymbolType AS TABLE (<Columns>)
DECLARE @DailyPricingAndVolBySymbol DailyPricingAndVolBySymbolType

INSERT INTO @DailyPricingAndVolBySymbol SELECT * FROM #DailyPricingAndVolBySymbol

ALTER FUNCTION dbo.PricingVolDataAvailableToDateProvided (
  @DailyPricingAndVolBySymbol DailyPricingAndVolBySymbolType READONLY
  @Ticker nchar(10),
  @StartDate DATE,
  @NumberOfDaysBack int
) ...

As been mentioned by other posters, you can't use a temporary table in an UDF. What you can do is pass a User-Defined Table to your function.

User-Defined Table Types

In SQL Server 2008, a user-defined table type is a user-defined type
that represents the definition of a table structure. You can use a
user-defined table type to declare table-valued parameters for stored
procedures or functions, or to declare table variables that you want
to use in a batch or in the body of a stored procedure or function.

A quick fix for changing your code could be

CREATE TYPE DailyPricingAndVolBySymbolType AS TABLE (<Columns>)
DECLARE @DailyPricingAndVolBySymbol DailyPricingAndVolBySymbolType

INSERT INTO @DailyPricingAndVolBySymbol SELECT * FROM #DailyPricingAndVolBySymbol

ALTER FUNCTION dbo.PricingVolDataAvailableToDateProvided (
  @DailyPricingAndVolBySymbol DailyPricingAndVolBySymbolType READONLY
  @Ticker nchar(10),
  @StartDate DATE,
  @NumberOfDaysBack int
) ...
故事还在继续 2025-01-13 21:06:36

看来你运气不好。我在下面创建了一个快速函数,并收到一条明确的编译器消息,指出您无法在函数中引用临时表。我不确定为什么您需要在 UDF 中引用临时表,这并不是 UDF 的真正精神。您能说明您计划如何调用此 UDF 吗?也许我们可以帮助重构。

在此处输入图像描述

Looks like you're out of luck. I created a quick function below and got an explicit compiler message that says you can't reference temp tables in a function. I'm not sure why you would need to reference temp tables within a UDF, that's not really the spirit of UDF. Could you show how you were planning to call this UDF? Maybe we could help on that refactor.

enter image description here

飞烟轻若梦 2025-01-13 21:06:36

无法从函数内访问临时表。我建议改用临时表。为了更好地在数据库中组织这些内容,您可以创建一个名为 Staging 的模式、一个名为 Staging.dailyPricingAndVolBySymbol 的表,并从 UDF 中调用它。

Temp tables cannot be accessed from within a function. I suggest using a staging table instead. To better organize these in your DB you could create a schema called Staging, a table called Staging.dailyPricingAndVolBySymbol, and call that from your UDF.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文