
















转摘CSDN
比如:
数据表中有字段,原先排列是这样:
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

此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。