Results 1 to 3 of 3

Thread: Help with ON DUPLICATE in MSQSL

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2008
    Posts
    144

    Help with ON DUPLICATE in MSQSL

    The following statement does not run in SQLSERVER 2005

    Code:
    INSERT INTO Schedule (DATE, MEMBER_ID, QTY)
    VALUES ('1/1/2009', 271, 99)
    ON DUPLICATE KEY UPDATE QTY = 88
    Any ideas why?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help with ON DUPLICATE in MSQSL

    Because that is MySQL-specific syntax. If you want to know what SQL Server supports in the way of SQL key words then read the SQL Server documentation. If you want to write an "upsert" in SQL Server 2005 there is no such construct that specifically supports it. You simply write an IF statement with separate INSERT and UPDATE statements. SQL Server 2008 introduces the MERGE command to support upserts.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2008
    Posts
    144

    Re: Help with ON DUPLICATE in MSQSL

    Actually I got this to work.

    Code:
    UPDATE Schedule SET QTY = 88
    WHERE MEMBER_ID = 000 and DATE = '1/1/2009'
    IF @@ROWCOUNT = 0
    INSERT INTO Schedule (DATE, MEMBER_ID, QTY) VALUES ('1/1/2009',000,99)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width