2015-03-13 103 views
0

我得到了这个查询,我想更新2行。从这:在Sql Server中简单更新查询

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

我不能体会到什么是错的。我是新来的。

谢谢!

+0

CodTID不能在同一时间1和2! – Galma88 2015-03-13 15:21:13

回答

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

这工作,谢谢@Knightwisp – 2015-03-13 15:36:12

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 
0

不能同时更新两行。如果你想确保两行被更新,你应该包住整个事情在一个事务中像这样:

begin transaction 

    UPDATE Table 
     SET 
      Description = 'Córdoba' 

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

     GO 
1

试试这个:

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