Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. update values in a table in single query

update values in a table in single query

Scheduled Pinned Locked Moved Database
databasehelpquestionannouncement
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nainakarri
    wrote on last edited by
    #1

    Hi I have an employee table with column gender F and M. Now i want to update the gender values of all the employee as F with M and M with F. But this should happen in a single query. How can i do this. Thanks for your help. Regards Naina

    Naina

    M L G 3 Replies Last reply
    0
    • N nainakarri

      Hi I have an employee table with column gender F and M. Now i want to update the gender values of all the employee as F with M and M with F. But this should happen in a single query. How can i do this. Thanks for your help. Regards Naina

      Naina

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      You cannot do it in a single query, you need 3 updates, move F to X, move M to F, move X to M you can wrap the 3 queries in a transaction to insure integrity is maintained.

      Never underestimate the power of human stupidity RAH

      L 1 Reply Last reply
      0
      • N nainakarri

        Hi I have an employee table with column gender F and M. Now i want to update the gender values of all the employee as F with M and M with F. But this should happen in a single query. How can i do this. Thanks for your help. Regards Naina

        Naina

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        BEGIN TRANSACTION

        CREATE TABLE Person
        (
        Name VARCHAR(MAX)
        ,Gender CHAR(1)
        )
        INSERT INTO Person VALUES
        ('Pete', 'M')
        ,('John', 'M')
        ,('Mary', 'F')
        ,('Dude', 'M')
        ,('Mary', 'F')

        SELECT *
        FROM Person

        UPDATE Person
        SET Gender = CASE Gender WHEN 'M' THEN 'F'
        ELSE 'M'
        END

        SELECT *
        FROM Person

        ROLLBACK

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        G 1 Reply Last reply
        0
        • L Lost User

          BEGIN TRANSACTION

          CREATE TABLE Person
          (
          Name VARCHAR(MAX)
          ,Gender CHAR(1)
          )
          INSERT INTO Person VALUES
          ('Pete', 'M')
          ,('John', 'M')
          ,('Mary', 'F')
          ,('Dude', 'M')
          ,('Mary', 'F')

          SELECT *
          FROM Person

          UPDATE Person
          SET Gender = CASE Gender WHEN 'M' THEN 'F'
          ELSE 'M'
          END

          SELECT *
          FROM Person

          ROLLBACK

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          G Offline
          G Offline
          gvprabu
          wrote on last edited by
          #4

          -- Multiple Update using Joins

          -- Create temp table for Teste Update Statement
          CREATE TABLE EmpDtls(Code VARCHAR(10), Name VARCHAR(100), DeptCode TINYINT)
          CREATE TABLE DeptDtls(DeptCode TINYINT, DeptName VARCHAR(50), EmpCount INT)

          -- Insert Sample Values
          INSERT INTO EmpDtls(Code, Name, DeptCode)
          VALUES('Emp1','Venkat', 1), ('Emp2','Prabu', 2), ('Emp3','Kumar', 1), ('Emp4','Karthick', 3),
          ('Emp5','Amith', 2),('Emp6','HariKrishna', 2)

          INSERT INTO DeptDtls(DeptCode, DeptName)
          VALUES(1, 'IT'),(2, 'Sales'), (3,'HR'), (4, 'Accounts')

          -- Check the sample values
          SELECT Code, Name, DeptCode FROM EmpDtls
          SELECT DeptCode, DeptName,EmpCount FROM DeptDtls

          -- Update Employees Count
          UPDATE D SET D.EmpCount= E.EmpCount
          FROM DeptDtls D
          INNER JOIN (SELECT DeptCode, COUNT(Code) 'EmpCount' FROM EmpDtls GROUP BY DeptCode) E ON E.DeptCode=D.DeptCode

          SELECT DeptCode, DeptName,EmpCount FROM DeptDtls

          -- Drop Table
          IF OBJECT_ID('EmpDtls') IS NOT NULL DROP TABLE EmpDtls
          IF OBJECT_ID('DeptDtls') IS NOT NULL DROP TABLE DeptDtls

          1 Reply Last reply
          0
          • M Mycroft Holmes

            You cannot do it in a single query, you need 3 updates, move F to X, move M to F, move X to M you can wrap the 3 queries in a transaction to insure integrity is maintained.

            Never underestimate the power of human stupidity RAH

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Mycroft Holmes wrote:

            You cannot do it

            You can actually do it. SQL is a set based language, changes made in a query are either fully committed or fully rolled back, there is no partial update. So, Eddy's solution will actually work.

            M 1 Reply Last reply
            0
            • L Lost User

              Mycroft Holmes wrote:

              You cannot do it

              You can actually do it. SQL is a set based language, changes made in a query are either fully committed or fully rolled back, there is no partial update. So, Eddy's solution will actually work.

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              Eddie got my 5, I didn't think of using case statement in the update clause.

              Never underestimate the power of human stupidity RAH

              1 Reply Last reply
              0
              • N nainakarri

                Hi I have an employee table with column gender F and M. Now i want to update the gender values of all the employee as F with M and M with F. But this should happen in a single query. How can i do this. Thanks for your help. Regards Naina

                Naina

                G Offline
                G Offline
                gvprabu
                wrote on last edited by
                #7

                Hi Naina, Check the Script, U can use CASE Statement in UPDATE.

                CREATE TABLE #EmpDtls(ID INT, EmpGender CHAR(1))
                INSERT INTO #EmpDtls (ID, EmpGender) VALUES (1,'M'),(2,'F'),(3,'M')

                SELECT ID, EmpGender FROM #EmpDtls
                -- Update Statement
                UPDATE #EmpDtls SET EmpGender = (CASE WHEN EmpGender='M' THEN 'F' WHEN EmpGender='F' THEN 'M' END)

                SELECT ID, EmpGender FROM #EmpDtls

                Regards, GVPrabu

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups