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 godeclare @i intupdate Hong_Props set @i = PropGameType,PropGameType = PropArrea,PropArrea = @iselect * from Hong_Propsdrop table Hong_Props/********************PropID PropGameType PropArrea ----------- ------------ ----------- 1 1 2 2 6 2 3 7 1 4 3 2 5 4 1(5 行受影响) 注意更新的两个字段的数据类型要一致。
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 godeclare @i intupdate Hong_Props set @i = PropGameType,PropGameType = PropArrea,PropArrea = @iselect * from Hong_Propsdrop table Hong_Props/************************PropID PropGameType PropArrea ----------- ------------ ----------- 1 1 2 2 6 2 3 7 1 4 3 2 5 4 1(5 行受影响)注意更新俩字段的数据类型。
LS的是两列吧,LZ要的是两行?
--trycreate table Hong_Props( PropID int identity(1,1),PropGameType int,PropArrea int ) insert into Hong_Props select 2 ,1 union all select 2 ,6 union all select 1 ,7 union all select 2 ,3 union all select 1 ,4 goselect PropID+0 as PropID,PropGameType,PropArrea into #temp from Hong_Props where PropID in (1,2) update #temp set PropID = (case when PropID=1 then 2 else 1 end)delete from Hong_Props where PropID in (1,2)set identity_insert Hong_Props on insert into Hong_Props(PropID,PropGameType,PropArrea) select PropID,PropGameType,PropArrea from #temp set identity_insert Hong_Props offselect * from Hong_Props order by PropIDdrop table Hong_Props,#temp/*********************PropID PropGameType PropArrea ----------- ------------ ----------- 1 2 6 2 2 1 3 1 7 4 2 3 5 1 4(5 行受影响)
说实话,貌似LZ走错地方了。既然有button,在button中写方法啊, 添加字段 sql='alter table tb add col int' 设定id最小的那个字段的col初始值为1,后面累加。 当button_click时,还是update col就行了。然后,在网页上显示时加上个order by col
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
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
godeclare @i intupdate Hong_Props
set @i = PropGameType,PropGameType = PropArrea,PropArrea = @iselect * from Hong_Propsdrop table Hong_Props/********************PropID PropGameType PropArrea
----------- ------------ -----------
1 1 2
2 6 2
3 7 1
4 3 2
5 4 1(5 行受影响)
注意更新的两个字段的数据类型要一致。
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
godeclare @i intupdate Hong_Props
set @i = PropGameType,PropGameType = PropArrea,PropArrea = @iselect * from Hong_Propsdrop table Hong_Props/************************PropID PropGameType PropArrea
----------- ------------ -----------
1 1 2
2 6 2
3 7 1
4 3 2
5 4 1(5 行受影响)注意更新俩字段的数据类型。
--trycreate table Hong_Props(
PropID int identity(1,1),PropGameType int,PropArrea int
)
insert into Hong_Props
select 2 ,1 union all
select 2 ,6 union all
select 1 ,7 union all
select 2 ,3 union all
select 1 ,4
goselect PropID+0 as PropID,PropGameType,PropArrea into #temp from Hong_Props where PropID in (1,2)
update #temp
set PropID = (case when PropID=1 then 2 else 1 end)delete from Hong_Props where PropID in (1,2)set identity_insert Hong_Props on
insert into Hong_Props(PropID,PropGameType,PropArrea)
select PropID,PropGameType,PropArrea from #temp
set identity_insert Hong_Props offselect * from Hong_Props order by PropIDdrop table Hong_Props,#temp/*********************PropID PropGameType PropArrea
----------- ------------ -----------
1 2 6
2 2 1
3 1 7
4 2 3
5 1 4(5 行受影响)
。。我表述不清可能。。
我在描述一下我的需求啊!
我现在在asp网页用到一个GridView,里面从数据库表中取出很多标题。
现在我想实现可以通过按钮来对标题进行上移下移操作。因为我的主键是自增长的,所以我想在数据库的表中加一个int型字段。然后如果点击上移,这个字段就+1,下移就-1.可是我不知道怎么实现我是菜鸟。谢谢大家。
。。我表述不清可能。。
我在描述一下我的需求啊!
我现在在asp网页用到一个GridView,里面从数据库表中取出很多标题。
现在我想实现可以通过按钮来对标题进行上移下移操作。因为我的主键是自增长的,所以我想在数据库的表中加一个int型字段。然后如果点击上移,这个字段就+1,下移就-1.可是我不知道怎么实现我是菜鸟。谢谢大家。
说实话,貌似LZ走错地方了。既然有button,在button中写方法啊,
添加字段
sql='alter table tb add col int'
设定id最小的那个字段的col初始值为1,后面累加。
当button_click时,还是update col就行了。然后,在网页上显示时加上个order by col
然后在前台程序页面里自由点击排序,点击保存时,重新读取GridView的全部内容,
覆盖写入目标表即可.
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)就行,设置排序的值就行了。