序号 用户编号 用户地址
1 001 台东路1号
2 002 台东路2号
3 006 台东路6号
4 007 台东路7号
... ... ....
9 004 台东路4号
10 005 台东路5号
... ...
我想编号004、005 放在006的前面
如下:
序号 用户编号 用户地址
1 001 台东路1号
2 002 台东路2号
3 004 台东路4号
4 005 台东路5号
5 006 台东路6号
6 007 台东路7号
... ... ...
9 ... ...
10 ... ...
答:
select identity(int) 序号,用户编号,用户地址 into #t from t order by 用户编号
select * from #t
问:把表
序号 用户编号 用户地址
1 001 台东路1号
2 002 台东路2号
3 004 台东路4号
4 005 台东路5号
5 006 台东路6号
6 007 台东路7号
中的用户编号是006 007 移到 用户编号 是004前面呢?
也就说得到这样的表:
序号 用户编号 用户地址
1 001 台东路1号
2 002 台东路2号
3 006 台东路6号
4 007 台东路7号
5 004 台东路4号
6 005 台东路5号
答:
---创建数据表
create table [dbo].[table] (
[id] [int] identity (1, 1) not null ,--序号
[usercode] [nvarchar] (10) collate chinese_prc_ci_as null ,--用户代码
[address] [nvarchar] (50) collate chinese_prc_ci_as null --地址
) on [primary]
go
---插如数据
insert into table
select ''001'',''台东路1号'' union all
select ''002'',''台东路2号'' union all
select ''006'',''台东路6号'' union all
select ''007'',''台东路7号'' union all
select ''004'',''台东路4号'' union all
select ''005'',''台东路5号''
---update sql语句 如果id是标识列的话 就有更新usercode,address了 如下::
update table set usercode=''00''+cast(a.id as char(1)),address=''台东路''+cast(a.id as
char(1))+''号''
from table join
(select id from table where id<>cast(usercode as int)) a
on table.id=a.id
