日期:2014-05-18  浏览次数:20563 次

SqlServer 数据库的一个表中的两行数据交换位置?
求 sql语句。。。
其中主键ID是自增长的。
在线等。。。谢谢!

------解决方案--------------------
SQL code

create table Hong_Props(
PropID int,PropGameType int,PropArrea int
)
insert into Hong_Props
select 1 ,2 ,1 union all
select 2 ,2 ,6 union all
select 3 ,1 ,7 union all
select 4 ,2 ,3 union all
select 5 ,1 ,4
go

declare @i int

update Hong_Props
set @i = PropGameType,PropGameType = PropArrea,PropArrea = @i

select * from Hong_Props

drop table Hong_Props

/********************

PropID      PropGameType PropArrea
----------- ------------ -----------
1           1            2
2           6            2
3           7            1
4           3            2
5           4            1

(5 行受影响)


注意更新的两个字段的数据类型要一致。

------解决方案--------------------
SQL code

create table Hong_Props(
PropID int,PropGameType int,PropArrea int
)
insert into Hong_Props
select 1 ,2 ,1 union all
select 2 ,2 ,6 union all
select 3 ,1 ,7 union all
select 4 ,2 ,3 union all
select 5 ,1 ,4
go

declare @i int

update Hong_Props
set @i = PropGameType,PropGameType = PropArrea,PropArrea = @i

select * from Hong_Props

drop table Hong_Props

/************************

PropID      PropGameType PropArrea
----------- ------------ -----------
1           1            2
2           6            2
3           7            1
4           3            2
5           4            1

(5 行受影响)

注意更新俩字段的数据类型。

------解决方案--------------------
SQL code

create table test
(
id int identity(1,1),
name varchar(10),
px int,
)
go
insert into test values('A',1)
insert into test values('B',2)
insert into test values('C',3)
insert into test values('D',4)
insert into test values('E',5)
insert into test values('F',6)
go

--为删除前,排序字段px是连续的
select * from test
/***
id          name       px
----------- ---------- -----------
1           A          1
2           B          2
3           C          3
4           D          4
5           E          5
6           F          6

(6 行受影响)
***/
--当有删除数据存在时,px并不是连续的,注意ID为3和5的px字段
delete from test where id = 4
select * from test
/***
id          name       px
----------- ---------- -----------
1           A          1
2           B          2
3           C          3      --3和5不是连续,所以在移动时不能直接+1或-1
5           E          5
6           F          6

(5 行受影响)
***/
--将ID为3的数据上移
declare @i int
declare @j int
select @j = px from test where id = 3
select top 1 @i = px from test where px < @j order by px desc
update test
set px = (case when px = @i then @j else @i end)
where px in (@i,@j)

select * from test
/***
id          name       px
----------- ---------- -----------
1           A          1
2           B          3
3           C          2       --ID为3数据排序上移,和ID为2的交换排序
5           E          5
6           F          6

(5 行受影响)
***/

--将ID为2的数据下移
declare @i int
declare @j int
select @j = px from test where id = 2
select top 1 @i = px from test where px > @j order by px
update test
set px = (case when px = @i then @j else @i end)
where px in (@i,@j)

select * from test
/***
id          name       px
----------- ---------- -----------
1           A          1
2           B          5       --ID为2的数据排序下移,和ID为5的交换排序
3           C          2
5           E          3
6           F          6

(5 行受影响)
***/

drop table test

------解决方案--------------------

美女啊,加个排序字段(Sort)就行,设置排序的值就行了。