t-sql 2012 merge statement insert statement incorrect

  • On the following merge statement, the insert always occurs. The Insert statement should only occur 1 time. Thus can you tell me what is wrong with the statement and what I can do to solve the problem?

    Merge TST.dbo.LockCombination AS LKC1

    USING

    (select LKC.lockID,LKC.seq,A.lockCombo5,A.schoolnumber

    from

    [GIMST0125CampusLockerPopulation] A

    JOIN TST.dbo.School SCH ON A.schoolnumber = SCH.type

    JOIN TST.dbo.Locker LKR ON SCH.schoolID = LKR.schoolID AND A.lockerNumber = LKR.number

    JOIN TST.dbo.Lock LK ON LKR.lockID = LK.lockID

    JOIN TST.dbo.LockCombination LKC ON LK.lockID = LKC.lockID

    and LKC.seq = 1 and A.schoolnumber ='331'

    ) AS LKC2 (lockID,seq,combo,schoolnumber)

    ON

    (

    LKC1.lockID = LKC2.lockID

    and LKC1.seq = 1 and LKC2.seq =5

    )

    WHEN NOT MATCHED and LKC2.schoolnumber ='331'

    THEN INSERT (lockID,seq,combo) VALUES(LKC2.lockID,5,LKC2.combo);

  • I don't think we can help you without create table scripts, representative data inserts and your actual and expected results from the statement execution.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply