Pablo Calderón
Pablo Calderón

Reputation: 31

Simple update query in Sql Server

I got this Query, I want to update 2 rows. From this:

---------------------
CodTID    Description
1         Córdoba
2         Corrientes
---------------------

To this:

---------------------
CodTID    Description
1         Corrientes
2         Córdoba
---------------------

This is my Query:

   UPDATE Table
    SET   
      Description = 'Córdoba'
      , Descrition = 'Corrientes' 
    WHERE 
      CodTID = '1'
      AND CodTID = '2'  

    GO

I Can't realize what is wrong. I'm new on this.

Thanks!

Upvotes: 0

Views: 693

Answers (4)

Knightwisp
Knightwisp

Reputation: 325

Separate the UPDATE statements.

UPDATE [tableName]
   SET Description = 'Corrientes'
 WHERE CodTID = 1;

UPDATE [tableName]
   SET Description = 'Córdoba'
 WHERE CodTID = 2;

Wrap it in a transaction to make sure they both happen together.

OR in one statement:

 UPDATE [TableName]
    SET Description = (CASE CodTID WHEN 1 THEN 'Corrientes' 
                                   WHEN 2 THEN 'Córdoba' END)
  WHERE CodTID IN (1, 2);

Upvotes: 0

Mike Shepard
Mike Shepard

Reputation: 18156

Try this:

UPDATE Table
    SET   
      Description = case CodTID when 1 then 'Corrientes'  else 'Córdoba'   end
    WHERE 
      CodTID in (1,2)
    GO

Upvotes: 1

benjamin moskovits
benjamin moskovits

Reputation: 5458

You cannot update two rows simultaneously. If you want to make sure that both rows are updated you should wrap the whole thing in a transaction as so:

begin transaction

    UPDATE Table
        SET   
          Description = 'Córdoba'

        WHERE 
          CodTID = '1'
update table
set Descrition = 'Corrientes' 
       where CodTID = '2'  
commit

        GO

Upvotes: 0

Pரதீப்
Pரதீப்

Reputation: 93694

Try this

UPDATE Table
SET    Description = CASE
                       WHEN CodTID = '1' THEN (SELECT TOP 1 Description
                                               FROM   Table
                                               WHERE  CodTID = 2)
                       WHEN CodTID = '2' THEN (SELECT TOP 1 Description
                                               FROM   Table
                                               WHERE  CodTID = 1)
                       ELSE Description
                     END 

Upvotes: 2

Related Questions