如果记录中有id,并且是连续的 update a set a.index=b.index from tb a,tb b where a.id=b.id-1 and a.id=xxx
--看下以下例子,可能有帮助 /*表 table_1 列 id(int) name(varcher) time(datetime) 有10行数据, 2到4行 和 5到7行的数据调换位置并且 时间(time)行 不变 */ update table_1 set name=a.name,time=a.time from ( select id+3,name,time from table_1 where id between 2 and 4 union all select id-3,name,time from table_1 where id between 5 and 7 ) a where table_1.id=a.id--或使用 update table_1 set id=(case when id between 2 and 4 then id+3 when id between 5 and 7 then id-3 else id end)
FlySQL 不对啊 希望实现这样的操作;假设有表: table1: Index Name NameKey 1 A a_01 2 B b_02 3 C c_03 4 D d_04交换第一,第二条记录的Index字段变为: table1: Index Name NameKey 2 A a_01 1 B b_02 3 C c_03 4 D d_04
如果不连续 可以用row_number()over加个列 再按照1楼的方法
意思就是除了第一个之外 其它的index都加1 第一个就用最大的indexdeclare @maxid int select @maxid = max(index) from tablename update tablename set index = @maxid where index=1update tablename set index = index+1 where index<>@maxid
--和例子中的相似: --假设表名是tb,而且记录中有id字段,并且是连续的declare @id int --当前记录的id set @id=3update tb set Index=a.Index from ( --这是当前记录,使其id=@id-1,弄成交换后的上条记录 select @id-1 as id,Index from tb where id=@id union all --这是上条记录,使其id=@id,弄成交换后的当前记录 select @id as id,Index from tb where id=@id-1 ) a where tb.id=a.id
--上面的up的操作,down的操作稍微改一下就可以了,具体如下: --假设表名是tb,而且记录中有id字段,并且是连续的declare @id int --当前记录的id set @id=3update tb set Index=a.Index from ( --这是当前记录,使其id=@id+1,弄成交换后的下条记录 select @id+1 as id,Index from tb where id=@id union all --这是下条记录,使其id=@id,弄成交换后的当前记录 select @id as id,Index from tb where id=@id+1 ) a where tb.id=a.id
我需要在C++程序中写sql语句“declare @id int --当前记录的id set @id=3”这样可定义变量行不通
阿土伯:: 这句中“select @id+1 as id,Index from tb where id=@id”的Index 是什么意思??
这个@id是我任意虚拟的一个值, declare @id int --当前记录的id set @id=3 上面这两句你可以忽略,只要把在下面的查询语句的@id,用你要传递的id替换就可以了
--可简化为下面,使id=3的记录和上面id=2的记录交换Index字段 --和例子中的相似: --假设表名是tb,而且记录中有id字段,并且是连续的update tb set Index=a.Index from ( --这是当前记录,使其id=3-1,弄成交换后的上条记录 select 3-1 as id,Index from tb where id=3 union all --这是上条记录,使其id=3,弄成交换后的当前记录 select 3 as id,Index from tb where id=3-1 ) a where tb.id=a.id
不是 没有ID 这个字段 只有Index 字段,只是Index字段交换
可以考虑建个临时表 或者 cte
我要交换的记录的Index值可以得到,那不管是跟下一条记录还是上一条记录交换都可以知道这条记录的Index:Index-1 或是 index + 1;
--传入Index=3,使其和上面Index=2的记录交换Index值 update tb set Index=a.Index from ( select Index-1 as index from tb where Index=3 union all select 3 as Index from tb where Index=3-1 ) a where tb.Index=a.Index
--创建上移的过程 create procedure up(@id int) as begin declare @up_id int declare @temp_id int set @up_id = @id -1 select @temp_id=max(index)+1 from tablename update tablename set index = @temp_id where index=@up_id update tablename set index = @up_id where index= @id update tablename set index = @id where index= @temp_id end --执行过程 将第三行向上移 exec up 3
--创建下移的过程 create procedure down(@id int) as begin declare @down_id int declare @temp_id int set @down_id = @id +1 select @temp_id=max(index)+1 from tablename update tablename set index = @temp_id where index=@down_id update tablename set index = @down_id where index= @id update tablename set index = @id where index= @temp_id end --执行过程 将第三行向下移 exec down 3
update a
set a.index=b.index
from tb a,tb b
where a.id=b.id-1
and a.id=xxx
/*表 table_1
列 id(int) name(varcher) time(datetime) 有10行数据, 2到4行 和 5到7行的数据调换位置并且 时间(time)行 不变
*/
update table_1 set name=a.name,time=a.time
from
(
select id+3,name,time from table_1 where id between 2 and 4
union all
select id-3,name,time from table_1 where id between 5 and 7 ) a
where table_1.id=a.id--或使用
update table_1 set id=(case when id between 2 and 4 then id+3 when id between 5 and 7 then id-3 else id end)
FlySQL 不对啊 希望实现这样的操作;假设有表:
table1:
Index Name NameKey
1 A a_01
2 B b_02
3 C c_03
4 D d_04交换第一,第二条记录的Index字段变为:
table1:
Index Name NameKey
2 A a_01
1 B b_02
3 C c_03
4 D d_04
select @maxid = max(index) from tablename
update tablename
set index = @maxid
where index=1update tablename
set index = index+1
where index<>@maxid
--假设表名是tb,而且记录中有id字段,并且是连续的declare @id int --当前记录的id
set @id=3update tb set Index=a.Index
from
(
--这是当前记录,使其id=@id-1,弄成交换后的上条记录
select @id-1 as id,Index from tb where id=@id
union all
--这是上条记录,使其id=@id,弄成交换后的当前记录
select @id as id,Index from tb where id=@id-1
) a
where tb.id=a.id
--假设表名是tb,而且记录中有id字段,并且是连续的declare @id int --当前记录的id
set @id=3update tb set Index=a.Index
from
(
--这是当前记录,使其id=@id+1,弄成交换后的下条记录
select @id+1 as id,Index from tb where id=@id
union all
--这是下条记录,使其id=@id,弄成交换后的当前记录
select @id as id,Index from tb where id=@id+1
) a
where tb.id=a.id
我需要在C++程序中写sql语句“declare @id int --当前记录的id set @id=3”这样可定义变量行不通
就是你上面要交换的Index字段
declare @id int --当前记录的id
set @id=3
上面这两句你可以忽略,只要把在下面的查询语句的@id,用你要传递的id替换就可以了
--和例子中的相似:
--假设表名是tb,而且记录中有id字段,并且是连续的update tb set Index=a.Index
from
(
--这是当前记录,使其id=3-1,弄成交换后的上条记录
select 3-1 as id,Index from tb where id=3
union all
--这是上条记录,使其id=3,弄成交换后的当前记录
select 3 as id,Index from tb where id=3-1
) a
where tb.id=a.id
不是 没有ID 这个字段 只有Index 字段,只是Index字段交换
可以考虑建个临时表 或者 cte
我要交换的记录的Index值可以得到,那不管是跟下一条记录还是上一条记录交换都可以知道这条记录的Index:Index-1 或是 index + 1;
我不是实现上下移动,是在界面上操作数据库中的一条记录能上下移动(也就是这条记录跟他的相邻的记录的Index字段能交换)
update tb set Index=a.Index
from
(
select Index-1 as index from tb where Index=3
union all
select 3 as Index from tb where Index=3-1
) a
where tb.Index=a.Index
--创建上移的过程
create procedure up(@id int)
as
begin
declare @up_id int
declare @temp_id int
set @up_id = @id -1
select @temp_id=max(index)+1 from tablename
update tablename
set index = @temp_id
where index=@up_id
update tablename
set index = @up_id
where index= @id
update tablename
set index = @id
where index= @temp_id
end --执行过程 将第三行向上移
exec up 3
--创建下移的过程
create procedure down(@id int)
as
begin
declare @down_id int
declare @temp_id int
set @down_id = @id +1
select @temp_id=max(index)+1 from tablename
update tablename
set index = @temp_id
where index=@down_id
update tablename
set index = @down_id
where index= @id
update tablename
set index = @id
where index= @temp_id
end --执行过程 将第三行向下移
exec down 3