用alter table 只能把字段加在中间,你要是想加在不同的位置, 有下面的方法: 1)建一个视,让它的字段按你的要求排序 2)用select 字段的顺序 into 表1 from 原表 把原表删除,再重命名表1为原表名,
alter table 只能把字段加在最后
--开启系统表写功能,不然无法创建存储过程 exec sp_configure 'allow updates',1 reconfigure with override goif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_addfield]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[p_addfield] GO/*--在指定位置添加字段
添加字段到表中的指定位置 注意,字段定义必须符合 alter table add ...的规定 插入的位置从0开始,到字段数目-1结束 如果超过这个范围,则在表的尾部添加字段 一次只能添加一个字段,和 alter table 的限制完全一样--邹建 2004.07(引用请保留此信息)--*//*--使用示例 --测试表 create table tb(a int) --添加字段 exec p_addfield 'tb','id int',0 --在最前面添加字段 exec p_addfield 'tb','id1 int',0 exec p_addfield 'tb','id2 int',2 --在第2个字段前面添加字段 --显示添加结果 select * from tb --删除测试 drop table tb --*/ create proc p_addfield @tbname sysname, @fd_define nvarchar(1000), --字段定义(必须是合法的字段定义) @colid int=-1 --添加位置:0--fieldcount-1,0表示在第一个字段前插入,-1或效的colid表示插入在最后 as declare @s nvarchar(4000)if isnull(objectproperty(object_id(@tbname),'IsUserTable'),0)=0 begin select 错误='无效的表名' return end--添加字段 set @s='alter table ['+replace(@tbname,']',']]')+'] add '+@fd_define exec(@s)--如果是添加在所有字段的未尾,则直接返回即可 if not exists(select 1 from syscolumns where id=object_id(@tbname) and colid between 1 and @colid+1) return--开启系统表写开关 exec sp_configure 'allow updates',1 reconfigure with overrideset xact_abort on begin tran update syscolumns set colid=colid+1 where id=object_id(@tbname) and colid>@colid update syscolumns set colid=@colid+1 where id=object_id(@tbname) and colid=( select max(colid) from syscolumns where id=object_id(@tbname)) commit tran--关闭系统表写开关 exec sp_configure 'allow updates',0 reconfigure with override go --关闭系统表写功能 exec sp_configure 'allow updates',0 reconfigure with override go
--关闭系统表写开关 exec sp_configure 'allow updates',0 reconfigure with override go --关闭系统表写功能 exec sp_configure 'allow updates',0 reconfigure with override go 为什么一模一样写两次?
to: haoK(haoK.Y) ,看东西要仔细--开启系统表写功能,不然无法创建存储过程 exec sp_configure 'allow updates',1 reconfigure with override go
是啊,我说的不对吗?两个 --关闭系统表写功能 exec sp_configure 'allow updates',0 reconfigure with override 一个是对应于sp内部的--开启系统表写开关 exec sp_configure 'allow updates',1 reconfigure with override另一个是对应于sp上面的--开启系统表写功能,不然无法创建存储过程 exec sp_configure 'allow updates',1 reconfigure with override go 故言之,一个实在存储过程里写的, 一个是在外面写的zjcxc(邹建) 兄,理解错你的意思了吗 ???
有下面的方法:
1)建一个视,让它的字段按你的要求排序
2)用select 字段的顺序 into 表1 from 原表
把原表删除,再重命名表1为原表名,
exec sp_configure 'allow updates',1 reconfigure with override
goif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_addfield]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_addfield]
GO/*--在指定位置添加字段
添加字段到表中的指定位置
注意,字段定义必须符合 alter table add ...的规定
插入的位置从0开始,到字段数目-1结束
如果超过这个范围,则在表的尾部添加字段
一次只能添加一个字段,和 alter table 的限制完全一样--邹建 2004.07(引用请保留此信息)--*//*--使用示例
--测试表
create table tb(a int) --添加字段
exec p_addfield 'tb','id int',0 --在最前面添加字段
exec p_addfield 'tb','id1 int',0
exec p_addfield 'tb','id2 int',2 --在第2个字段前面添加字段 --显示添加结果
select * from tb --删除测试
drop table tb
--*/
create proc p_addfield
@tbname sysname,
@fd_define nvarchar(1000), --字段定义(必须是合法的字段定义)
@colid int=-1 --添加位置:0--fieldcount-1,0表示在第一个字段前插入,-1或效的colid表示插入在最后
as
declare @s nvarchar(4000)if isnull(objectproperty(object_id(@tbname),'IsUserTable'),0)=0
begin
select 错误='无效的表名'
return
end--添加字段
set @s='alter table ['+replace(@tbname,']',']]')+'] add '+@fd_define
exec(@s)--如果是添加在所有字段的未尾,则直接返回即可
if not exists(select 1 from syscolumns where id=object_id(@tbname) and colid between 1 and @colid+1)
return--开启系统表写开关
exec sp_configure 'allow updates',1 reconfigure with overrideset xact_abort on
begin tran
update syscolumns set colid=colid+1
where id=object_id(@tbname)
and colid>@colid update syscolumns set colid=@colid+1
where id=object_id(@tbname)
and colid=(
select max(colid) from syscolumns
where id=object_id(@tbname))
commit tran--关闭系统表写开关
exec sp_configure 'allow updates',0 reconfigure with override
go
--关闭系统表写功能
exec sp_configure 'allow updates',0 reconfigure with override
go
以前参考这段脚本,思路总局限在 建临时表-〉导入数据-〉改表名 这种方法上。
从没想过直接改动syscolumns表的colid,今天又学了招,哈哈...
exec sp_configure 'allow updates',0 reconfigure with override
go
--关闭系统表写功能
exec sp_configure 'allow updates',0 reconfigure with override
go
为什么一模一样写两次?
exec sp_configure 'allow updates',1 reconfigure with override
go
--关闭系统表写功能
exec sp_configure 'allow updates',0 reconfigure with override
一个是对应于sp内部的--开启系统表写开关
exec sp_configure 'allow updates',1 reconfigure with override另一个是对应于sp上面的--开启系统表写功能,不然无法创建存储过程
exec sp_configure 'allow updates',1 reconfigure with override
go
故言之,一个实在存储过程里写的,
一个是在外面写的zjcxc(邹建) 兄,理解错你的意思了吗
???