如何将连接查询的结果存储在存储过程中的单个变量中

时间:2016-04-08 07:31:44

标签: sql-server-2008 procedure

CREATE PROCEDURE Test12 
     @varwi_name nvarchar(50)
AS
BEGIN 
    DECLARE @newbalance nvarchar(400)

    BEGIN
        SELECT 
            @newbalance = (SELECT 
                               (CAST(a.currentBal as INT) * CAST( b.monthlyInterestRate AS INT)) / 100  
                           FROM 
                               USR_0_CLOS_TransDet_TXN a, USR_0_CLOS_LoanReg_TXN b ON a.wi_name = b.wi_name)
       WHERE 
           wi_name = @varwi_name 
END
GO

我收到错误

  

关键字' ON'附近的语法不正确。严重性15测试12

1 个答案:

答案 0 :(得分:1)

  • 您需要ON子句的JOIN关键字
  • 错误放置(和)
  • 额外的BEGIN

应该看起来:

CREATE PROCEDURE Test12
    @varwi_name nvarchar(50)
AS
BEGIN 

    DECLARE @newbalance nvarchar(400);

    SELECT
        @newbalance = (
                       SELECT
                        (CAST(a.currentBal AS int) * CAST(b.monthlyInterestRate AS int)) / 100
                       FROM
                        USR_0_CLOS_TransDet_TXN a
                        JOIN USR_0_CLOS_LoanReg_TXN b ON a.wi_name = b.wi_name
                       WHERE
                        wi_name = @varwi_name
                      );

END;
GO