在多语句函数SQL Server

时间:2018-05-04 16:23:43

标签: function sql-server-2008 parameters

我在SQL Server中为汽车租赁数据库创建一个简单的功能时遇到了问题。

CREATE FUNCTION F_late_price 
    (@from_date DATETIME, 
     @to_date DATETIME,
     @fee_per_day INT) 
RETURNS @Late_Customers_fees TABLE                  
                    (Id_Cust INT, 
                     Date_of_Rent_End_Due DATETIME,
                     Date_of_Rent_End DATETIME,
                     Total_fee AS (DATEDIFF(DAY, Date_of_Rent_End_Due, Date_of_Rent_End)) * @fee_per_day
                    )                                                                  
AS                                                                  
BEGIN 
    IF @from_date <= @to_date 
    BEGIN
        INSERT INTO @Late_Customers_fees
            SELECT 
                Id_Cust, Date_of_Rent_End_Due, Date_of_Rent_End
            FROM 
                F_late_customers(@from_date, @to_date)
    END

    RETURN                                                         
END

F_late_customers函数传递两个日期,并返回一个表格,其中包含截止日期超过截止日期的客户:

CREATE FUNCTION F_late_customers 
    (@from_date DATETIME,
     @to_date DATETIME)
RETURNS @Late_Customers_details TABLE                  
                        (Id_Cust INT,
                         F_Name NVARCHAR(16),
                         L_Name NVARCHAR(16),
                         Date_of_Rent_Start DATETIME,
                         Date_of_Rent_End_Due DATETIME,
                         Date_of_Rent_End DATETIME
                        )                    
AS 
BEGIN                                     
    IF @from_date <= @to_date 
    BEGIN
        INSERT INTO @Late_Customers_details
            SELECT  
                dbo.Customers.Id_Cust,
                dbo.Customers.F_Name, dbo.Customers.L_Name, 
                dbo.Rents.Date_Of_Rent_Start, 
                dbo.Rents.Date_Of_Rent_End_Due, dbo.Rents.Date_Of_Rent_End
            FROM    
                dbo.Customers 
            INNER JOIN 
                dbo.Rents ON dbo.Customers.Id_Cust = dbo.Rents.ID_Cust
            WHERE  
                (dbo.Rents.Date_Of_Rent_End_Due) < (dbo.Rents.Date_Of_Rent_End)
    END

    RETURN
END

但是F_late_price函数引发了一个错误,即在create table语句中使用@fee_per_day作为参数。

有什么方法可以解决这个问题吗?

任何帮助将不胜感激

谢谢

1 个答案:

答案 0 :(得分:1)

您不能在表声明中使用它:

CREATE FUNCTION  F_late_price 
    (@from_date DATETIME, 
     @to_date DATETIME,
     @fee_per_day INT) 
RETURNS @Late_Customers_fees TABLE                  
                        (Id_Cust INT, 
                         Date_of_Rent_End_Due DATETIME,
                         Date_of_Rent_End DATETIME,
                         Total_fee AS DECIMAL(18,2)   
                        )                                                                  
AS                                                                  
BEGIN 
    IF @from_date <= @to_date 
    BEGIN
        INSERT INTO @Late_Customers_fees
            SELECT 
                Id_Cust, Date_of_Rent_End_Due, Date_of_Rent_End, 
                (DATEDIFF(DAY, Date_of_Rent_End_Due, Date_of_Rent_End)) * @fee_per_day AS TotalFee
            FROM 
                F_late_customers(@from_date, @to_date)
    END

    RETURN                                                         
END

这样的事情:

TotalFee

E/TestLoader: Could not find class: "my.package.MyClassTest" 数据类型可能不是您需要的,因此您可能需要更改它。