Sql 服务器中的简单更新查询

Simple update query in Sql Server

我收到这个查询,我想更新 2 行。来自:

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

为此:

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

这是我的查询:

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

    GO

我不知道哪里出了问题。我是新手。

谢谢!

试试这个

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 

您不能同时更新两行。如果你想确保两行都更新,你应该将整个事情包装在一个事务中:

begin transaction

    UPDATE Table
        SET   
          Description = 'Córdoba'

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

        GO

试试这个:

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

分离更新语句。

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

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

将其包装在事务中以确保它们同时发生。

或在一个语句中:

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