当前位置: 代码迷 >> Sql Server >> SqlServer 数据库的一个表中的两行数据交换位置?该如何处理
  详细解决方案

SqlServer 数据库的一个表中的两行数据交换位置?该如何处理

热度:356   发布时间:2016-04-27 14:06:04.0
SqlServer 数据库的一个表中的两行数据交换位置?
求 sql语句。。。
其中主键ID是自增长的。
在线等。。。谢谢!

------解决方案--------------------
SQL code
create table Hong_Props(PropID int,PropGameType int,PropArrea int)insert into Hong_Propsselect 1 ,2 ,1 union allselect 2 ,2 ,6 union allselect 3 ,1 ,7 union allselect 4 ,2 ,3 union allselect 5 ,1 ,4godeclare @i intupdate Hong_Propsset @i = PropGameType,PropGameType = PropArrea,PropArrea = @iselect * from Hong_Propsdrop table Hong_Props/********************PropID      PropGameType PropArrea----------- ------------ -----------1           1            22           6            23           7            14           3            25           4            1(5 行受影响)注意更新的两个字段的数据类型要一致。
------解决方案--------------------
SQL code
create table Hong_Props(PropID int,PropGameType int,PropArrea int)insert into Hong_Propsselect 1 ,2 ,1 union allselect 2 ,2 ,6 union allselect 3 ,1 ,7 union allselect 4 ,2 ,3 union allselect 5 ,1 ,4godeclare @i intupdate Hong_Propsset @i = PropGameType,PropGameType = PropArrea,PropArrea = @iselect * from Hong_Propsdrop table Hong_Props/************************PropID      PropGameType PropArrea----------- ------------ -----------1           1            22           6            23           7            14           3            25           4            1(5 行受影响)注意更新俩字段的数据类型。
------解决方案--------------------
SQL code
create table test(id int identity(1,1),name varchar(10),px int,)goinsert 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          12           B          23           C          34           D          45           E          56           F          6(6 行受影响)***/--当有删除数据存在时,px并不是连续的,注意ID为3和5的px字段delete from test where id = 4select * from test/***id          name       px----------- ---------- -----------1           A          12           B          23           C          3      --3和5不是连续,所以在移动时不能直接+1或-15           E          56           F          6(5 行受影响)***/--将ID为3的数据上移declare @i intdeclare @j intselect @j = px from test where id = 3select top 1 @i = px from test where px < @j order by px descupdate testset px = (case when px = @i then @j else @i end)where px in (@i,@j)select * from test/***id          name       px----------- ---------- -----------1           A          12           B          33           C          2       --ID为3数据排序上移,和ID为2的交换排序5           E          56           F          6(5 行受影响)***/--将ID为2的数据下移declare @i intdeclare @j intselect @j = px from test where id = 2select top 1 @i = px from test where px > @j order by pxupdate testset px = (case when px = @i then @j else @i end)where px in (@i,@j)select * from test/***id          name       px----------- ---------- -----------1           A          12           B          5       --ID为2的数据排序下移,和ID为5的交换排序3           C          25           E          36           F          6(5 行受影响)***/drop table test
------解决方案--------------------

美女啊,加个排序字段(Sort)就行,设置排序的值就行了。
  相关解决方案