首页 文章

UDF表UDV还是标量UDF?

提问于
浏览
0

我会尽力使这个问题比我最后的惨败更好 . 我得到了可怕的“无法找到列”dbo“或用户定义的函数或聚合”dbo.PriMonthAvgPrice“,或者名称不明确 . <

我试图找到上个月的平均销售价格 . 这是我的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

这是我查询中的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'

我的BASIC理解是我创建了一个标量函数 . 然而,从我一直在阅读的关于我的错误,这个错误在TVF上返回 . 这是真的?我在此处理之前创建了一个SVF,只是确定了上个月的结束日期,因此它不像我在UDF中创建查询那样涉及 .

我需要将其更改为TVF吗?如果是这样,当我必须连接多个表时,如何合并SELECT *?

提前致谢 .

亚伦

1 回答

  • 1

    您没有显示from子句,但是您在其中创建了该函数的数据库是什么?

    如果您完全限定名称(包括数据库),它是否有效?

    你有独立测试功能:

    select [dbo].[PriMonthAvgPrice] ('01/01/2011', '02/01/2011', 'test')
    

    注意:当然你会使用一些应返回结果的实际值 .

    请运行此并告诉我们返回的值:

    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
    

相关问题