如何根据条件更新SQL数据库表

时间:2017-09-13 09:57:54

标签: sql-server

我有一个存储过程,可以将表中的值插入另一个表中,如下所示:

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值必须替换TestingToolTestPerfomanceBenchmark表值中的AverageRuntime

如何修改存储过程以实现此目的?

2 个答案:

答案 0 :(得分:1)

CREATE PROCEDURE [dbo].[InsertAverageRunTimings]

(
@LatesVersionTested varchar(50) =  NULL
)
AS
BEGIN

    IF EXISTS (SELECT LatestVersionTested  FROM TestingToolTestPerfomanceBenchmark WHERE LatestVersionTested= @LatesVersionTested)
    BEGIN 
    UPDATE TestingToolTestPerfomanceBenchmark SET AverageRuntime= a.AverageRuntime 
    FROM #temptable2 a
    WHERE LatestVersionTested =@LatesVersionTested

    END ELSE BEGIN 

    INSERT INTO TestingToolTestPerfomanceBenchmark( LatestVersionTested, TestCode, TestSequence, AverageRuntime)
    SELECT  LatestVersionTested, TestCode, TestSequence, AverageRuntime
    FROM #temptable2
    WHERE  LatestVersionTested = @LatesVersionTested

  END

END

答案 1 :(得分:0)

如果您的存储过程插入了您需要的所有记录,则不应存在任何不存在的记录。

然而,您不仅要查看最新版本,因为它不是唯一的,而是最新版本和测试序列的组合。

您的INSERT没有考虑测试序列,是否只能一次性插入版本测试序列?

如果这是真的,你可以这样做:

CREATE PROCEDURE [dbo].[InsertAverageRunTimings]

    @LatesVersionTested varchar(50) =  NULL

    AS
    BEGIN

        /*Insert missing records*/
       INSERT INTO TestingToolTestPerfomanceBenchmark
            ( 
                LatestVersionTested, 
                TestCode, 
                TestSequence, 
                AverageRuntime
            )
        SELECT  LatestVersionTested, 
                TestCode, 
                TestSequence, 
                AverageRuntime
        FROM    #temptable2
        WHERE   LatestVersionTested  NOT IN 
        (
            SELECT  LatestVersionTested 
            FROM    TestingToolTestPerfomanceBenchmark
        )
        /*Update the records so the runtimes match as all records are now inserted*/
        UPDATE      PB
        SET         PB.AverageRuntime                   =   T2.AverageRunTime
        FROM        TestingToolTestPerfomanceBenchmark  AS  PB
        INNER JOIN  #TempTable2                         AS  T2  ON  PB.LatestVersionTested = T2.LatestVersionTested AND PB.TestSequence     = T2.TestSequence   
        WHERE       PB.AverageRuntime                   <>  T2.AverageRuntime;


     END