Sql Server中的简单更新查询

时间:2015-03-13 15:18:27

标签: sql-server sql-update

我收到了这个查询,我想更新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

我无法意识到出了什么问题。我是新来的。

谢谢!

4 个答案:

答案 0 :(得分:2)

试试这个

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 

答案 1 :(得分:1)

试试这个:

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

答案 2 :(得分:0)

您无法同时更新两行。如果你想确保两行都被更新,你应该将整个事物包装在一个事务中,如下所示:

begin transaction

    UPDATE Table
        SET   
          Description = 'Córdoba'

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

        GO

答案 3 :(得分:0)

分开UPDATE语句。

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);