[英]UDF Table UDV or Scalar UDF?
I will do my best to make this question better than my last fiasco. 我将尽我所能使这个问题比上一次惨败更好。 I am getting the dreaded >"cannot find either column "dbo" or the user-defined function or aggregate "dbo.PriMonthAvgPrice", or the name is ambiguous.<
我收到了令人恐惧的>“找不到列“ dbo”或用户定义的函数或聚合“ dbo.PriMonthAvgPrice”,或者名称不明确。
I am attempting to find the avg sales price from the previous month. 我正在尝试查找上个月的平均销售价格。 Here is my UDF:
这是我的UDF:
USE [WoodProduction]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[PriMonthAvgPrice]
(
-- Add the parameters for the function here
@endofmonth datetime,
@begofmonth datetime,
@PlantCode varchar
)
RETURNS decimal (10,2)
AS
BEGIN
-- Declare the return variable here
DECLARE @MonthEndAvgPrice decimal (10,2)
-- Add the T-SQL statements to compute the return value here
SELECT @MonthEndAvgPrice =
(
select
sum(Actual_Sales_Dollars/Actual_Volume)
FROM
woodproduction.dbo.plywood_layup_sales pls
WHERE
Production_Date between @begofmonth and @endofmonth
and actual_volume <> 0
and @PlantCode = pls.Plant_Code
)
-- Return the result of the function
RETURN @MonthEndAvgPrice
END
This is my SELECT statement from my query: 这是我查询中的SELECT语句:
SELECT
DISTINCT
P.[Plant_Number]
,p.plant_name
,pls.plant_code
,(pls.[Budget_Realization]) AS 'BR'
,(pls.[Actual_Volume] ) AS 'AV'
,(pls.[Budget_Volume]) AS 'BV'
--,sum (dpb.[Gross_Production_Per_Hr]) AS 'GPB'
,(p.Production_Volume) AS 'PV'
,CASE
WHEN coalesce (pls.[Actual_Volume],0) = 0 and
coalesce (pls.[Actual_Sales_Dollars],0) = 0
THEN 0
ELSE (pls.[Actual_Sales_Dollars]/pls.[Actual_Volume])
END
AS 'AP'
,pls.production_date
,[dbo].[PriMonthAvgPrice](@endofmonth,@begofmonth, pls.plant_code) AS 'PriMoAvgPrice'
My BASIC understanding is that I HAVE created a Scalar Function. 我对BASIC的理解是,我已经创建了一个标量函数。 From what I've been reading about my error however, This error returns on TVF's.
但是,从我一直在阅读的有关我的错误的内容来看,该错误会在TVF上返回。 Is this true?
这是真的? I created a SVF prior to this dealing with just determining a prior month end date so it wasn't as involved as this one where I create the query in the UDF.
在此之前,我创建了一个SVF,用于确定上个月的结束日期,因此它不像我在UDF中创建查询所涉及的那样。
Do I need to change this to a TVF? 我需要将其更改为TVF吗? And if so, how do I incorporate SELECT * when I have to join multiple tables along with this?
如果是这样,当我必须同时将多个表联接在一起时,如何合并SELECT *?
Thanks in advance. 提前致谢。
Aaron 亚伦
You don't show the from clause, but is the database you created the function in part of it? 您没有显示from子句,但是您创建函数的数据库是它的一部分吗?
Does it work if you fully qualify the name (include the database)? 如果完全限定名称(包括数据库),是否可以正常工作?
Have you independently tested the function with: 您是否通过以下方式独立测试了该功能:
select [dbo].[PriMonthAvgPrice] ('01/01/2011', '02/01/2011', 'test')
Note: of course you would use some actual values that should return a result. 注意:当然,您会使用一些实际值,这些结果应该返回结果。
Please run this and tell us the values returned: 请运行此命令并告诉我们返回的值:
SELECT Actual_Sales_Dollars,Actual_Volume, pls.PLant_code
FROM woodproduction.dbo.plywood_layup_sales pls
WHERE Production_Date between '09-01-2011' and '09-30-2011'
and actual_volume <> 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.