I am trying to do a cursor and such in one of my INNER JOINS which is part of a bigger SQL statement. I am getting the following errors:
Msg 156, Level 15, State 1, Procedure Balance_Query, Line 61
Incorrect syntax near the keyword 'DECLARE'.
Msg 137, Level 15, State 1, Procedure Balance_Query, Line 71
Must declare the variable '@RMSTRANCDE'.
Msg 137, Level 15, State 1, Procedure Balance_Query, Line 79
Must declare the variable '@RMSTRANCDE'.
Msg 137, Level 15, State 2, Procedure Balance_Query, Line 81
Must declare the variable '@Sum_of_rmstranamt'.
Msg 156, Level 15, State 1, Procedure Balance_Query, Line 106
Incorrect syntax near the keyword 'AS'.
INNER JOIN
(
DECLARE CheckCodes CURSOR FOR
SELECT RMSFILENUM,
RMSTRANCDE,
rmstranamt AS rmstranamt
FROM RFINANL
WHERE RMSTRANCDE <> '10'
OPEN CheckCodes
FETCH FROM CheckCodes INTO @RMSTRANCDE, @rmstranamt
CASE WHEN rf.rmstrancde IN ('50','51','52','53')Then
@Sum_of_rmstranamt = @Sum_of_rmstranamt + -ABS(rf.rmstranamt)
WHEN rf.rmstrancde IN ('55','56','57','58') Then
@Sum_of_rmstranamt = @Sum_of_rmstranamt + ABS(rf.rmstranamt)
END
FETCH NEXT FROM CheckCodes INTO @RMSTRANCDE, @rmstranamt
SELECT @Sum_of_rmstranamt AS rmstranamt
END
) AS rf ON rf.RMSFILENUM = rm.RMSFILENUM
Msg 156, Level 15, State 1, Procedure Balance_Query, Line 61
Incorrect syntax near the keyword 'DECLARE'.
Msg 137, Level 15, State 1, Procedure Balance_Query, Line 71
Must declare the variable '@RMSTRANCDE'.
Msg 137, Level 15, State 1, Procedure Balance_Query, Line 79
Must declare the variable '@RMSTRANCDE'.
Msg 137, Level 15, State 2, Procedure Balance_Query, Line 81
Must declare the variable '@Sum_of_rmstranamt'.
Msg 156, Level 15, State 1, Procedure Balance_Query, Line 106
Incorrect syntax near the keyword 'AS'.
INNER JOIN
(
DECLARE CheckCodes CURSOR FOR
SELECT RMSFILENUM,
RMSTRANCDE,
rmstranamt AS rmstranamt
FROM RFINANL
WHERE RMSTRANCDE <> '10'
OPEN CheckCodes
FETCH FROM CheckCodes INTO @RMSTRANCDE, @rmstranamt
CASE WHEN rf.rmstrancde IN ('50','51','52','53')Then
@Sum_of_rmstranamt = @Sum_of_rmstranamt + -ABS(rf.rmstranamt)
WHEN rf.rmstrancde IN ('55','56','57','58') Then
@Sum_of_rmstranamt = @Sum_of_rmstranamt + ABS(rf.rmstranamt)
END
FETCH NEXT FROM CheckCodes INTO @RMSTRANCDE, @rmstranamt
SELECT @Sum_of_rmstranamt AS rmstranamt
END
) AS rf ON rf.RMSFILENUM = rm.RMSFILENUM