I have the following SP, its has 3 update statements in it. Each I EXEC this SP i get an error \"Msg 208, Level 16, State 1, Procedure sp_Rating_Comments, Line 41 Invalid o
You start the SP with ;WITH RatingLines ...
which connects to the first UPDATE
statement, not the other ones. This construct creates a CTE that is only visible to the first statement that follows it. More explanation can be found in the TN for WITH common_table_expression (Transact-SQL). In particular this excerpt from Remarks highlights this:
A CTE must be followed by a single SELECT, INSERT, UPDATE, MERGE, or DELETE statement that references some or all the CTE columns.
To have this table known for all statements in your SP, create a table variable or a temporary table for the RatingLines
instead.
Outline using a temporary table would be as follows:
Select RDA.[CTS] AS [CTS]
,RDA.[B_KEY] AS [B_KEY]
,RDA.[H_KEY] AS [H_KEY]
,RDA.[RT_ID] AS [RT_ID]
,RDA.[RT_AVGRATING] AS [RT_AVGRATING]
,RDDA.[RTD_COMMENT] AS [RTD_COMMENT]
INTO #RatingLines -- Create #RatingLines as temporary table
From [DynNavHRS].[HRSDB].[HTL_RATING_ALL_DA] RDA
Join [DynNavHRS].[HRSDB].[HTL_RATING_DETAIL_ALL_DA] RDDA
ON RDA.RT_ID =RDDA.RT_ID
AND RDDA.[RTD_COMMENT] <> ''
AND RDA.[B_KEY]='19214642';
-- Throughout the rest of the SP, use #RatingLines as your ratings table; eg:
...
INNER JOIN #RatingLines RL1
...
-- At the end of the SP, drop the temporary table
DROP TABLE #RatingLines;