我试图根据几个更新增量来更新表。
我的文档审核表目前如下所示:
| ID | Review Date | Provisional Review Date | Final Review Date | Provisional Review Timeframe (hours) | Final Review Timeframe (hours) |
|----|---------------------|-------------------------|---------------------|--------------------------------------|--------------------------------|
| 1 | 2018-03-12 00:00:00 | 2018-03-10 00:00:00 | 2018-03-11 00:00:00 | 24 | 48 |
存储过程( insertDocumentReview )如下所示:
CREATE PROCEDURE [dbo].[insertDocumentReview]
--parameters
@id int,
@ReviewDate datetime2 = null,
@ProvisionalReviewDate datetime2 = null,
@FinalReviewDate datetime2 = null,
AS
BEGIN
UPDATE [DocumentReview]
SET ReviewDate = @ReviewDate,
ProvisionalReviewDate = @ProvisionalReviewDate,
FinalReviewDate = @FinalReviewDate,
--the following columns are calculated based on the values of the columns populated above
[ProvisionalReviewTimeframe] = (DATEDIFF(SECOND, [ProvisionalReviewDate], [ReviewDate]) / 3600.0),
[lReviewTimeframe] = (DATEDIFF(SECOND, [FinalReviewDate], [ReviewDate]) / 3600.0),
WHERE [Id] = @id
END
然后我执行存储过程4次,将不同的参数设置为null
,如下所示:
-- 1) Insert Final Review Date
EXEC insertDocumentReview 1, NULL, NULL, '2018-03-14 10:19:10'
-- 2) Insert Provisional Review Date
EXEC insertDocumentReview 1, NULL, '2018-03-13 12:19:10', NULL
-- 3) Insert Review Date
EXEC insertDocumentReview 1, '2018-03-15 10:19', NULL, NULL
-- 4) Insert all dates
EXEC insertDocumentReview 1, '2018-03-15 10:19', '2018-03-13 12:19:10', '2018-03-14 10:19:10'
我需要知道为什么计算在执行3时不计算。
答案 0 :(得分:2)
这是全有或全无的行为。您不能从同一执行中引用值并获得新值。
示例:
create table t(x INT, y INT);
INSERT INTO t VALUES (5, 10);
update t
SET x = y,
y = x;
select * from T;
<强> DBFiddle Demo 强>
-- Before
x = 5, y = 10
-- After
x = 10, y = 5
-- in step 3 you want this to work like this
x= 10, y = 10
值已交换,但在第3步中,您希望从prev列获取新值,但不会发生这种情况。
我建议尝试:
UPDATE [DocumentReview]
SET ReviewDate = @ReviewDate,
ProvisionalReviewDate = @ProvisionalReviewDate,
FinalReviewDate = @FinalReviewDate,
[ProvisionalReviewTimeframe]
= (DATEDIFF(SECOND, [ProvisionalReviewDate], @ReviewDate) / 3600.0),
[lReviewTimeframe]
= (DATEDIFF(SECOND, [FinalReviewDate], @ReviewDate) / 3600.0),
WHERE [Id] = @id