在create function中使用聚合函数

时间:2013-05-23 04:26:32

标签: sql-server tsql

我使用下面的代码来创建函数,但是当我想执行它时会发生错误

CREATE FUNCTION getFactorPriceFunction
(
    @factorCode BIGINT
)
RETURNS bigint
AS BEGIN
    RETURN 
    (
        select SUM(price*coun) as total from CustomerFactor inner join CustomerFactorDetails 
        on CustomerFactor.code=CustomerFactorDetails.factorCode inner join ProductDetails on
        ProductDetails.code=CustomerFactorDetails.productDetailsCode
        where factorCode=@factorCode AND final=1
    )
END 

执行:

 select total from getFactorPriceFunction(100)

错误:

  

无效的对象名称'getFactorPriceFunction'

2 个答案:

答案 0 :(得分:0)

  1. 它被称为dbo.getFactorPriceFunction
  2. 调用如下:

    select dbo.getFactorPriceFunction(100) as total
    

答案 1 :(得分:-1)

由于它是scalar value函数,因此应该像

一样执行
select dbo.getFactorPriceFunction(100)

如果你要退回一张桌子,那么它应该是

select total from dbo.getFactorPriceFunction(100)