SQL Server 2008,我可以在 udf 内的 select 语句中引用临时表吗?
我正在尝试对 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他发帖者所提到的,您不能在 UDF 中使用临时表。您可以做的是传递用户定义的表< /a> 到你的函数。
更改代码的快速修复可能是
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.
A quick fix for changing your code could be
看来你运气不好。我在下面创建了一个快速函数,并收到一条明确的编译器消息,指出您无法在函数中引用临时表。我不确定为什么您需要在 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.
无法从函数内访问临时表。我建议改用临时表。为了更好地在数据库中组织这些内容,您可以创建一个名为 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.