selective insert and update

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bravo
    New Member
    • Jul 2007
    • 43

    selective insert and update

    hi
    how can i insert a record in a table if it doesnt exist already and update the record otherwise ie if record exists then it must be updated and if not exist then inserted.

    is it possible through one query... For example :
    TableOne
    ColumnOne|Colum nTwo|....
    jhon |123455|....

    now a record jhon,10000 comes since jhon already exist so it must be updated
    if steve,45 comes it must be inserted

    Any suggestions.... . Thanks in advance...
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    You can use SQL Server 2008's new MERGE statement if using that version .That allows you to insert, update, or delete data based on certain join conditions in the same statement.

    Comment

    • bravo
      New Member
      • Jul 2007
      • 43

      #3
      Thanks for ur reply DADA,

      but i am using mysql 4.1 is it possible in that .. or i have to first run a query to check the existence of the record then update or insert.. that will be tooo time consuming but i dont have any choice .. can u suggest me a way...

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Originally posted by debasisdas
        You can use SQL Server 2008's new MERGE statement if using that version .That allows you to insert, update, or delete data based on certain join conditions in the same statement.
        Are you advertising for Microsoft software in the FREE MySQL forum? Shame on you.

        Ronald

        Comment

        • debasisdas
          Recognized Expert Expert
          • Dec 2006
          • 8119

          #5
          Oh...

          I am really sorry. I mistook that for SQL Server forum question.

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Originally posted by bravo
            Thanks for ur reply DADA,

            but i am using mysql 4.1 is it possible in that .. or i have to first run a query to check the existence of the record then update or insert.. that will be tooo time consuming but i dont have any choice .. can u suggest me a way...
            You can do it in one query with REPLACE, BUT only and only when the old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, . See the appropriate chapter in the MySQL documentation for your MySQL version HERE

            Ronald

            Comment

            • debasisdas
              Recognized Expert Expert
              • Dec 2006
              • 8119

              #7
              Originally posted by ronverdonk
              Are you advertising for Microsoft software in the FREE MySQL forum? Shame on you.

              Ronald
              No no no ........

              Not at all . I actually mistook this as a SQL Server question in a hurry.

              I am again sorry for that .

              Comment

              Working...