详细一点,最好给出你的数据结构和部分数据
你可以参考下面的思路:select 关键字,要排序的字段 into #temp
from 你的表 order by 要排序的字段 --如果降序,加descupdate 你的表 set 要排序的字段=b.要排序的字段
from 你的表 a,#temp b
where a.关键字=b.关键字

解决方案 »

  1.   

    select a, c, b into Y表 from X表(原有字段顺序为a,b,c)
    drop table X表
    sp_rename X表, Y表
      

  2.   

    select 关键字,要排序的字段 into #temp
    from 你的表 order by 要排序的字段 --如果降序,加descupdate 你的表 set 要排序的字段=b.要排序的字段
    from 你的表 a,#temp b
    where a.关键字=b.关键字
    邹建兄的很好啊
      

  3.   

    应该说表里的字段是没有序的,但是,你要排序的话,可以在查询的时候order by 
    或group by等等等。
      

  4.   

    create proc addcolumn
    @tablename varchar(30),  --表名
    @colname varchar(30),    --要加的列名
    @coltype varchar(100),   --要加的列类型
    @colid int               --加到第几列
    asdeclare @colid_max int    
    declare @sql varchar(1000) --动态sql语句
    --------------------------------------------------
    if not exists(select  1 from sysobjects 
                  where name = @tablename and xtype = 'u')
      begin
      raiserror 20001 '没有这个表'
      return -1
      end
    --------------------------------------------------
    if exists(select 1 from syscolumns
              where id = object_id(@tablename) and name = @colname)
      begin
      raiserror 20002 '这个表已经有这个列了!'
      return -1
      end
    --------------------------------------------------
    --保证该表的colid是连续的
    select @colid_max = max(colid) from syscolumns where id=object_id(@tablename)if @colid > @colid_max or @colid < 1 
       set @colid = @colid + 1
    --------------------------------------------------
    set @sql = 'alter table '+@tablename+' add '+@colname+' '+@coltype 
    exec(@sql)select @colid_max = colid 
    from syscolumns where id = object_id(@tablename) and name = @colname
    if @@rowcount <> 1
      begin
      raiserror 20003 '加一个新列不成功,请检查你的列类型是否正确'
      return -1
      end
    --------------------------------------------------
    --打开修改系统表的开关
    EXEC sp_configure 'allow updates',1  RECONFIGURE WITH OVERRIDE--将新列列号暂置为-1
    set @sql = 'update syscolumns
                set colid = -1
                where id = object_id('''+@tablename+''') 
                      and colid = '+cast(@colid_max as varchar(10))
    exec(@sql)--将其他列的列号加1
    set @sql = 'update syscolumns
                set colid = colid + 1
                where id = object_id('''+@tablename+''') 
                      and colid >= '+cast(@colid as varchar(10))
    exec(@sql)--将新列列号复位
    set @sql = 'update syscolumns
                set colid = '+cast(@colid as varchar(10))+'
                where id = object_id('''+@tablename+''') 
                      and name = '''+@colname +''''
    exec(@sql)
    --------------------------------------------------
    --关闭修改系统表的开关
    EXEC sp_configure 'allow updates',0  RECONFIGURE WITH OVERRIDE
    go
    调用方法:
    exec addcolumn '表名','新列名','新列类型',加到第几个位置
    如:
    exec addcolumn 'test','id2','char(10)',2
    表示将id2这个列加到表test的第二个位置,类型是char(10)。