Mass Table Updates Question

  • I am trying to do a mass update for a specific column in a table. I need to change the value of all rows with the "ProblemType" column of 32 to 12. I am trying to use this procedure but it keeps updating the entire table, not just the rows with ProblemType of 32. Where am I going wrong? PLEASE HELP!

    use [database]

    UPDATE Table1

    SET ProblemType = 12

    WHERE EXISTS (SELECT * FROM Table1 AS v

    WHERE v.problemType = 32)

    SQL Padre
    aka Robert M Bishop

    "Do or do not, there is no try" -- Yoda

  • You don't see a subquery.

    use [database]

    UPDATE Table1

    SET ProblemType = 12

    WHERE problemType = 32

    The way you had it written, you were updating all rows.

  • I am trying to do a mass update for a specific column in a table. I need to change the value of all rows with the "ProblemType" column of 32 to 12. I am trying to use this procedure but it keeps updating the entire table, not just the rows with ProblemType of 32. Where am I going wrong? PLEASE HELP!

    use [database]

    UPDATE Table1

    SET ProblemType = 12

    WHERE EXISTS (SELECT * FROM Table1 AS v

    WHERE v.problemType = 32)

    i think it is more simple with the code. I am not sure if you trying to achieve something else. Try this.

    Update Table1

    set ProblemType = 12

    where Problemtype = 32

    U need to change it to 12 where it is 32.

  • Sorry Steve, I think we both wrote on same time.

  • Thanks! Not sure where I got the "sub query idea from" but make sense to use just simple coding.

    😀

    SQL Padre
    aka Robert M Bishop

    "Do or do not, there is no try" -- Yoda

  • You might need to use it in case of update statement from two different tables. It really depends on what you are trying to achieve.

  • Let me ask this follow up if I can. If I wanted to change multiple ProblemTypes to 12 would the code be as follows

    update Table1

    SET ProblemType = 12

    WHERE ProblemType = 32 or ProblemType = 3 or ProblemType = 46, etc

    or would it be the AND connector?

    SQL Padre
    aka Robert M Bishop

    "Do or do not, there is no try" -- Yoda

  • Or if you want multiple ones.

    Be sure that you use () to block out your ORs and ANDs. AND would match nothing since (I assume) you can't have a single row with ProblemType=32 and =3.

    No worries on the double post, glad we both answered. Try not to quote if you don't need to. More stuff to scroll through.

Viewing 8 posts - 1 through 7 (of 7 total)

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