首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-01-23 
【高手进】如何UPDATE两条完全相同的记录?现在有2条完全相同的记录,但是我只想UPDATE其中的一条。不想删除数

【高手进】如何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

-->生成测试数据:if not object_id('tb') is null  drop table tbgocreate table tb(col varchar(10))goinsert tbselect 'a' union allselect 'a' union allselect 'b' union allselect 'b'go-->SQL2000查询如下: declare @col varchar(10),@newcol varchar(10)update tb set     col=@newcol,    @newcol=case when col=@col then col             else 'new-'+col        end,    @col=colselect * from tb/*col----------new-aanew-bb(4 行受影响)*/ 

热点排行