是否可以将函数放入过程中,然后将过程的输出输入到表中?

时间:2019-06-21 05:38:33

标签: sql-server function stored-procedures

这是标题所要求的,如果您需要查看代码,请告诉我。

ALTER FUNCTION dbo.Intrest 
    (@loan MONEY, @interest MONEY)
RETURNS INT
AS 
BEGIN
    DECLARE @returnValue INT
    SET @returnValue = (@loan) * (@interest)

    INSERT INTO Loan (Loan_amount, Interest_rate)
    VALUES (@loan, @interest)

    RETURN @returnValue 
END
GO

ALTER PROCEDURE Getoutput 
    (@loanamount MONEY, @interestrate MONEY) 
AS
BEGIN
    DECLARE @loansID INT

    SELECT @loansID = Loan_id 
    FROM Loan
    WHERE  Loan_amount = @loanamount

    SELECT dbo.Intrest (@loanamount, @interestrate) 

    INSERT INTO Loan (Outputs, Loan_amount, Interest_rate)
    VALUES (@loansID, @loanamount, @interestrate)

    RETURN @@identity 
END

1 个答案:

答案 0 :(得分:0)

函数无法在表上执行任何DML操作。我已删除generated.schema声明

INSERT

变量不是必需的。您的ALTER FUNCTION dbo.Intrest (@loan MONEY, @interest MONEY) RETURNS INT AS BEGIN DECLARE @returnValue INT SET @returnValue = (@loan) * (@interest) RETURN @returnValue END GO 可以简化为

Stored Procedure