“Merge” style operation with literal values?

后端 未结 3 838
深忆病人
深忆病人 2020-12-29 08:00

I have a table containing a student-grade relationship:

Student   Grade   StartDate   EndDate
   1        1    09/01/2009    NULL
   2        2    09/01/2010         


        
相关标签:
3条回答
  • 2020-12-29 08:05

    Simply:

    --Arrange
    
    CREATE TABLE dbo.Product
    (
        Id   INT IDENTITY PRIMARY KEY,
        Name VARCHAR(40),   
    )
    GO
    
    --Act
    
    MERGE INTO dbo.Product AS Target 
    USING 
    (
        --Here is the trick :)
    
        VALUES 
            (1, N'Product A'),
            (2, N'Product B'),
            (3, N'Product C'),
            (4, N'Product D')
    ) 
    AS 
    Source 
    (
         Id,
         Name
    ) 
    
    ON Target.Id= Source.Id
    
    WHEN NOT MATCHED BY TARGET THEN 
    
    INSERT 
    (
          Name
    ) 
    VALUES 
    (
          Name
    );
    
    0 讨论(0)
  • 2020-12-29 08:10

    You can use a MERGE even if you are passing literal values. Here's an example for your issue:

    CREATE PROCEDURE InsertStudentGrade(@Student INT, @Grade INT, @StartDate DATE)
    AS
    BEGIN;
    
        MERGE StudentGrade AS tbl
         USING (SELECT @Student AS Student, @Grade AS Grade, @StartDate AS StartDate) AS row
         ON tbl.Student = Row.Student AND tbl.Grade = row.Grade
        WHEN NOT MATCHED THEN
          INSERT(Student, Grade, StartDate)
           VALUES(row.Student, row.Grade, row.StartDate)
        WHEN MATCHED AND tbl.EndDate IS NULL AND tbl.StartDate != row.StartDate THEN
          UPDATE SET
            tbl.StartDate = row.StartDate;
    
    END;
    
    0 讨论(0)
  • 2020-12-29 08:30

    I prefer the following, it is cleaner and easier to read and modify.

    MERGE Definition.tdSection AS Target
    USING
        (SELECT *
         FROM   ( VALUES
                ( 1, 1, 'Administrator', 1, GETDATE(), NULL, Current_User, GETDATE())
                 ,( 2, 1, 'Admissions', 1, GETDATE(), NULL, Current_User, GETDATE())
                 ,( 3, 1, 'BOM', 1, GETDATE(), NULL, Current_User, GETDATE())
                 ,( 4, 1, 'CRC', 1, GETDATE(), NULL, Current_User, GETDATE())
                 ,( 5, 1, 'ICM', 1, GETDATE(), NULL, Current_User, GETDATE())
                 ,( 6, 1, 'System', 1, GETDATE(), NULL,Current_User, GETDATE())
                 ,( 7, 1, 'Therapy', 1, GETDATE(), NULL, Current_User, GETDATE()) 
                )
                AS s (SectionId
                      ,BusinessProcessId
                      ,Description, Sequence
                      ,EffectiveStartDate
                      ,EffectiveEndDate
                      ,ModifiedBy
                      ,ModifiedDateTime)
         ) AS Source
    ON Target.SectionId = Source.SectionId
    WHEN NOT MATCHED THEN
        INSERT (SectionId
               ,BusinessProcessId
               ,Description
               ,Sequence
               ,EffectiveStartDate
               ,EffectiveEndDate
               ,ModifiedBy
               ,ModifiedDateTime
               )
        VALUES (Source.SectionId
               ,Source.BusinessProcessId
               ,Source.Description
               ,Source.Sequence
               ,Source.EffectiveStartDate
               ,Source.EffectiveEndDate
               ,Source.ModifiedBy
               ,Source.ModifiedDateTime
               );
    
    0 讨论(0)
提交回复
热议问题