日期:2014-05-18 浏览次数:20854 次
;WITH Liang AS
(
SELECT * ,rowid=ROW_NUMBER() OVER(PARTITION BY xx ORDER BY id)
FROM tb
)
UPDATE Liang SET
xx = xx
WHERE rowid = 1;
------解决方案--------------------
UPDATE TOP(1) tb SET xx=xx WHERE xx=xxx
------解决方案--------------------
--借助一个子增列 alter table tb add id int identity(1,1) go --更新需要更新的那个 update tb set col=.... where id=.. go --去掉该子增列 alter table tb drop column id
------解决方案--------------------
DECLARE @T TABLE( COL1 VARCHAR(10) ) INSERT INTO @T SELECT 'A' UNION ALL SELECT 'A' UPDATE TOP(1) @T SET COL1='B' WHERE COL1='A' SELECT * FROM @T /* B A */
------解决方案--------------------
create table test_top(id int, name varchar(10))
insert into test_top
select '1','aa' union all
select '1','aa'
1种
set rowcount 1
update test_top set name='tt'
set rowcount 0
2种
;with ttttt as
(
select * , row = row_number() over (order by id ) from test_top
)
update ttttt set name='x' where row =1
------解决方案--------------------
-- =========================================
-- -----------t_mac 小编-------------
---希望有天成为大虾----
-- =========================================
IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb
GO
CREATE TABLE tb(a int,b int)
go
insert into tb
select 1,2 union all
select 1,2
go
set rowcount 1
update tb
set a=3,b=5
where a=1 and b=2
set rowcount 0
select * from tb
/*------------
a b
----------- -----------
3 5
1 2
-------*/
------解决方案--------------------
你用的是SQL Server 2000吧?
那么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
------解决方案--------------------
--2005方法
-- =========================================
-- -----------t_mac 小编-------------
---希望有天成为大虾----
-- =========================================
IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb
GO
CREATE TABLE tb(a int,b int)
go
insert into tb
select 1,2 union all
select 1,2
go
update top(1) tb
set a=3,b=5
where a=1 and b=2
select * from tb
(1 行受影响)
a b
----------- -----------
3 5
1 2
--2000方法
-- =========================================
-- -----------t_mac 小编-------------
---希望有天成为大虾----
-- =========================================
IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb
GO
CREATE TABLE tb(a int,b int)
go
insert into tb
select 1,2 union all
select 1,2
go
set rowcount 1
update tb
set a=3,b=5
where a=1 and b=2
set rowcount 0
select * from tb
/*------------
a b
----------- -----------
3 5
1 2
-------*/
------解决方案--------------------