当前位置: 代码迷 >> Sql Server >> 【】怎么UPDATE两条完全相同的记录
  详细解决方案

【】怎么UPDATE两条完全相同的记录

热度:65   发布时间:2016-04-27 19:10:35.0
【高手进】如何UPDATE两条完全相同的记录?
现在有2条完全相同的记录,但是我只想UPDATE其中的一条。
不想删除数据。
请高手解答
谢谢

------解决方案--------------------
SQL code
;WITH Liang AS (    SELECT * ,rowid=ROW_NUMBER() OVER(PARTITION BY xx ORDER BY id)    FROM tb)UPDATE Liang SET    xx = xxWHERE rowid = 1;
------解决方案--------------------
SQL code
UPDATE TOP(1) tb SET   xx=xxWHERE xx=xxx
------解决方案--------------------
SQL code
--借助一个子增列alter table tb add id int identity(1,1)go--更新需要更新的那个update tb set col=.... where id=..go--去掉该子增列alter table tb drop column id
------解决方案--------------------
SQL code
DECLARE @T TABLE(COL1 VARCHAR(10))INSERT INTO @TSELECT 'A' UNION ALLSELECT 'A'UPDATE TOP(1) @T SET COL1='B' WHERE COL1='A'SELECT * FROM @T/*BA*/
------解决方案--------------------
SQL code
create table test_top(id int, name varchar(10))insert into test_topselect '1','aa' union allselect '1','aa'1种set rowcount 1update test_top set name='tt'set rowcount 02种;with ttttt as (    select * , row = row_number() over (order by id ) from test_top)update ttttt set name='x' where row =1
------解决方案--------------------
SQL code
-- =========================================-- -----------t_mac 小编-------------   ---希望有天成为大虾---- -- =========================================IF OBJECT_ID('tb') IS NOT NULL  DROP TABLE tbGOCREATE TABLE tb(a int,b int)goinsert into tbselect 1,2 union allselect 1,2 goset rowcount 1update tb set a=3,b=5where a=1 and b=2set rowcount 0select  * from tb/*------------a           b----------- -----------3           51           2-------*/
------解决方案--------------------
你用的是SQL Server 2000吧?

那么
SQL code
 row_number、 update top (1)
------解决方案--------------------
SQL> select *
2 from t
3 ;

A B
---------- -----
1 a
1 a
2 c


SQL> update t
2 set b='b'
3 where rowid=(select min(rowid) from t where a=1 and b='a')
4 ;

已更新 1 行。

SQL> select *
2 from t
3 ;

A B
---------- -----
1 b
1 a
2 c
------解决方案--------------------
SQL code
--2005方法 -- =========================================-- -----------t_mac 小编-------------   ---希望有天成为大虾---- -- =========================================IF OBJECT_ID('tb') IS NOT NULL  DROP TABLE tbGOCREATE TABLE tb(a int,b int)goinsert into tbselect 1,2 union allselect 1,2 goupdate top(1) tb set a=3,b=5where a=1 and b=2select * from tb (1 行受影响)a           b----------- -----------3           51           2--2000方法-- =========================================-- -----------t_mac 小编-------------   ---希望有天成为大虾---- -- =========================================IF OBJECT_ID('tb') IS NOT NULL  DROP TABLE tbGOCREATE TABLE tb(a int,b int)goinsert into tbselect 1,2 union allselect 1,2 goset rowcount 1update tb set a=3,b=5where a=1 and b=2set rowcount 0select  * from tb/*------------a           b----------- -----------3           51           2-------*/
------解决方案--------------------
SQL code
  相关解决方案