函数返回值中的sql函数参数

时间:2018-04-27 06:50:45

标签: sql sql-server sql-server-2008 tsql sql-function

sql视图如何将函数的返回值返回到函数

select 

dbo.fn_CalculatePrice
(
p.Price, 
p.CargoPrice, 
p.Factor, 
p.PointRatio,
null, 
p.OperactivePromoCode, 
SELECT IncludingCargoPrice FROM [dbo].[fn_Parameter](),
SELECT IncludingProductKdv FROM [dbo].[fn_Parameter]()) AS Price 
From Product as p

[dbo].[fn_CalculatePrice] func parameters
(
    @Price money,
    @CargoPrice money,
    @CatFactor decimal(8,4), 
    @PointRatio decimal(8,4),
    @HBFactor decimal(8,4),
    @PromoCode nvarchar(50),
    @includingCargoPrice bit,
    @includingProductKdv bit
)

fn_Parameter func return table
{
IncludingCargoPrice bit,
IncludingProductKdv bit,
}

错误:关键字“SELECT”

附近的语法不正确

1 个答案:

答案 0 :(得分:0)

您可以使用以下两种方法中的任何一种:

1)将每个参数确定为标量变量。

DECLARE @IncludingCargoPrice BIT = (SELECT IncludingCargoPrice FROM [dbo].fn_Parameter())
DECLARE @IncludingProductKdv BIT = (SELECT IncludingProductKdv FROM [dbo].fn_Parameter())

SELECT
    dbo.fn_CalculatePrice(
        p.Price, 
        p.CargoPrice, 
        p.Factor, 
        p.PointRatio,
        null, 
        p.OperactivePromoCode, 
        @IncludingCargoPrice,
        @IncludingProductKdv) AS Price
From 
    Product as p

2)SELECT直接在您的查询中(必须确保参数表只包含一行,或者每个产品将按参数行多次应用该函数)。您可以使用WHEREINNER JOIN中的条件(而不是我作为示例编写的CROSS JOIN)过滤行。

SELECT
    dbo.fn_CalculatePrice(
        p.Price, 
        p.CargoPrice, 
        p.Factor, 
        p.PointRatio,
        null, 
        p.OperactivePromoCode, 
        X.IncludingCargoPrice,
        X.IncludingProductKdv) AS Price
From 
    Product as p
    CROSS JOIN [dbo].fn_Parameter() AS X