一個SQL數據庫表我有一個插入值從表到另一個表的存儲過程,見下圖:如何更新基於條件
USE [DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[InsertAverageRunTimings]
(
@LatesVersionTested varchar(50) = NULL
)
AS
BEGIN
INSERT INTO TestingToolTestPerfomanceBenchmark(LatestVersionTested, TestCode, TestSequence, AverageRuntime)
SELECT LatestVersionTested, TestCode, TestSequence, AverageRuntime
FROM #temptable2
WHERE LatestVersionTested NOT IN (SELECT LatestVersionTested FROM TestingToolTestPerfomanceBenchmark)
END
Temptable2和TestingToolTestPerfomanceBenchmark有這樣的信息:
LatestVersionTested TestSequence AverageRuntime
3.21 - 48 Update 78 1 5.34
3.21 - 48 Update 78 15 6.32
3.21 - 48 Update 78 16 5.93
3.21 - 48 Update 78 17 21.09
3.21 - 48 Update 78 18 0.64
3.21 - 48 Update 78 19 0.15
3.21 - 48 Update 78 20 0.12
3.21 - 48 Update 78 21 0.12
3.21 - 48 Update 78 22 0.91
3.21 - 48 Update 78 25 13.93
3.21 - 48 Update 95 1 1.40
3.21 - 48 Update 95 15 0.74
3.21 - 48 Update 95 16 1.27
3.21 - 48 Update 95 17 5.27
3.21 - 48 Update 95 18 3.62
3.21 - 48 Update 95 19 2.53
3.21 - 48 Update 95 20 2.52
3.21 - 48 Update 95 21 2.33
3.21 - 48 Update 95 22 0.61
3.21 - 48 Update 95 25 2.05
3.21 - 48 Update 93 1 1.58
3.21 - 48 Update 93 15 0.85
3.21 - 48 Update 93 16 1.45
3.21 - 48 Update 93 17 5.18
3.21 - 48 Update 93 18 3.48
3.21 - 48 Update 93 19 2.50
3.21 - 48 Update 93 20 2.48
3.21 - 48 Update 93 21 2.16
3.21 - 48 Update 93 22 0.70
3.21 - 48 Update 93 25 2.36
如果在TestingToolTestPerfomanceBenchmark表中的LatestVersionTested列中未找到temptable2中LatestVersionTested列中的值,則存儲過程將插入這些值。
我想實現的是,如果LatestVersionTested兩個表中是相同的,在temptable2的AverageRuntime值必須更換AverageRuntime在TestingToolTestPerfomanceBenchmark表中的值
如何修改我的存儲過程來實現這一目標?
這是TestingToolTestPerfomanceBenchmark主鍵?我認爲這不僅僅是'LatestVersionTested',還有'TestCode',但是,在你的WHERE子句中,你只是不想找到'LatestVersionTested',你應該添加所有PK字段以避免重複? –
@SergioPrats主鍵是TestSequence – Immortal