转摘_修改表结构,让表中的字段按英文字母的顺序重新排列
转摘CSDN
比如:
数据表中有字段,原先排列是这样:
B A D C E F
现在要求用SQL修改表结构让它重新排列,按英文字母排,这样:
A B C D E F
比如:
数据表中有字段,原先排列是这样:
B A D C E F
现在要求用SQL修改表结构让它重新排列,按英文字母排,这样:
A B C D E F
--遍历表
go
sp_configure 'allow updates',1
go
RECONFIGURE WITH OVERRIDE
go
declare @SQL varchar(4000)
declare @TableName varchar(30)
declare sTableName Cursor for select [name] from sysobjects where xtype='u' and name<>'dtproperties'
Open sTableName
fetch next from sTableName into @TableName
while @@fetch_status=0
begin
--更新
set @SQL='update syscolumns set colid=(select count(*) from syscolumns a where a.name<=syscolumns.name and a.id=syscolumns.id),colorder=(select count(*) from syscolumns a where a.name<=syscolumns.name and a.id=syscolumns.id) where id=object_id('''+@TableName+''')'
print(@SQL)
exec(@SQL)
fetch next from sTableName into @TableName
end
close sTableName
deallocate sTableName
go
sp_configure 'allow updates',0
go
RECONFIGURE WITH OVERRIDE
go
sp_configure 'allow updates',1
go
RECONFIGURE WITH OVERRIDE
go
declare @SQL varchar(4000)
declare @TableName varchar(30)
declare sTableName Cursor for select [name] from sysobjects where xtype='u' and name<>'dtproperties'
Open sTableName
fetch next from sTableName into @TableName
while @@fetch_status=0
begin
--更新
set @SQL='update syscolumns set colid=(select count(*) from syscolumns a where a.name<=syscolumns.name and a.id=syscolumns.id),colorder=(select count(*) from syscolumns a where a.name<=syscolumns.name and a.id=syscolumns.id) where id=object_id('''+@TableName+''')'
print(@SQL)
exec(@SQL)
fetch next from sTableName into @TableName
end
close sTableName
deallocate sTableName
go
sp_configure 'allow updates',0
go
RECONFIGURE WITH OVERRIDE