开发者

2 part stored proc is successful when run piecemeal, unsuccessful when run all at once

See code below. When I run parts #1 and #2 separately, they work as planned: I get the expected 'Y' value in the [InsertFlag] field.

But when I run them together by executing the entire sp, I do not get the expected 'Y' value.

My best guess is that regardless of the order in which I place these two statements, #2 executes first. This populates [SCD_Type2_EndDate], and when #1 tests that field for NULL, it fails and therefore doesn't update the [InsertFlag] field.

If my guess is correct then I need to find a way to force these two statements to run in order. I tried adding 'GO' after the 1st statement but receive the following error:

Msg 102, Level 15, State 1, Procedure etl_Update_SetExpiryDate_adName, Line 19 Incorrect syntax near ';'. Msg 102, Level 15, State 1, Line 23 Incorrect syntax near 'END'.

So, how can I fix this?

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE etl_Update_SetExpiryDate_adName
AS
BEGIN
 SET NOCOUNT ON;

--1.
UPDATE 
 amlrr.wtName
SET
 InsertFlag = 'Y'
FROM 
 amlrr.wtName w开发者_StackOverflow社区tN
INNER JOIN amlrr.adName adN
 ON wtN.StageID = adN.StageID
WHERE 
 adN.SCD_Type2_EndDate IS NULL
AND
 wtN.FullName <> adN.FullName

GO
--2.

UPDATE 
 amlrr.adName
SET
 SCD_Type2_EndDate = getdate()
FROM 
 amlrr.adName adN
INNER JOIN amlrr.wtName wtN
 ON adN.StageID = wtN.StageID
WHERE 
 adN.SCD_Type2_EndDate IS NULL
AND
 adN.FullName <> wtN.FullName








END
GO


Remove the GO after first update statement.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE etl_Update_SetExpiryDate_adName
AS
BEGIN
 SET NOCOUNT ON;

--1.
UPDATE 
 amlrr.wtName
SET
 InsertFlag = 'Y'
FROM 
 amlrr.wtName wtN
INNER JOIN amlrr.adName adN
 ON wtN.StageID = adN.StageID
WHERE 
 adN.SCD_Type2_EndDate IS NULL
AND
 wtN.FullName <> adN.FullName

--2.

UPDATE 
 amlrr.adName
SET
 SCD_Type2_EndDate = getdate()
FROM 
 amlrr.adName adN
INNER JOIN amlrr.wtName wtN
 ON adN.StageID = wtN.StageID
WHERE 
 adN.SCD_Type2_EndDate IS NULL
AND
 adN.FullName <> wtN.FullName
END
GO
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜