在同一存储过程中使用3个更新?``商场错误''

问题描述

您启动的SP;WITH ratingLines ...连接到第一个UPDATE语句,而不连接其他语句。此构造创建一个CTE,该CTE仅对它后面的第一条语句可见。可以在TN中找到有关WITH common_table_expression(Transact-SQL)的更多说明。特别是摘录自“备注”的摘录突出了这一点:

CTE之后必须是引用某些或所有CTE列的单个SELECT,INSERT,UPDATE,MERGE或DELETE语句。

若要使该表为您SP中的所有语句所知,请为它创建一个表变量或一个临时表ratingLines

使用临时表的大纲如下:

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;

解决方法

我有以下SP,它具有3条更新语句。每个执行此SP的程序,我都会收到一个错误“消息208,级别16,状态1,过程sp_Rating_Comments,第41行无效的对象名称’RatingLines’。”。此错误位于第二条更新语句中。当我隐藏第二条和第三条更新语句时,我的代码工作正常。知道如何将这3个Update语句一起使用。

Alter PROCEDURE [HRSDB].[sp_Rating_Comments]

-- @BookingNr varchar(25)

--,@Company varchar(30)

AS

BEGIN

  SET NOCOUNT ON;

     ;WITH RatingLines AS   -- Get the important columns from both HRSDB tables

    (

      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]



        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'  -- Just to test with one rec

    )



-- First Table:

  UPDATE     [DynNavHRS].[dbo].[HRS$Agency Header]

  SET        [Booking Rating] = '1'

  FROM       [DynNavHRS].[dbo].[HRS$Agency Header] AH

  INNER JOIN RatingLines   RL1

  ON         RL1.[B_KEY] = AH.[Reservation No_]

  WHERE      RL1.[RTD_COMMENT] <> ''   -- If the Booking have a comment in DB2

  AND        [Booking Rating] = '0' ;  -- in order to avoide scanning all line



-- Second Table:

  UPDATE     [DynNavHRS].[dbo].[HRS$Correction Agency Header]

  SET        [Booking Rating] = '1'

  FROM       [DynNavHRS].[dbo].[HRS$Correction Agency Header] CL

  INNER JOIN RatingLines   RL2

  ON         RL2.[B_KEY] = CL.[Reservation No_]

  WHERE      RL2.[RTD_COMMENT] <> ''   -- If the Booking have a comment in DB2

  AND        [Booking Rating] = '0'  ; -- in order to avoide scanning all line



-- Third Table:

  UPDATE     [DynNavHRS].[dbo].[HRS$Agency Display Line]

  SET        [Booking Rating] = '1'

  FROM       [DynNavHRS].[dbo].[HRS$Agency Display Line] DL

  INNER JOIN RatingLines   RL3

  ON         RL3.[B_KEY] = DL.[Reservation No_]

  WHERE      RL3.[RTD_COMMENT] <> ''   -- If the Booking have a comment in DB2

  AND        [Booking Rating] = '0'   ;-- in order to avoide scanning all line



COMMIT

END