数据库中删除列
我的数据库db中每表都有一个rowguid字段,我想删除这列,请问sql怎么写! 问题点数:20、回复次数:9Top
1 楼Softlee81307(孔腎)回复于 2005-03-08 10:04:44 得分 0
alter table 表 drop column rowguidTop
2 楼redstorm11(沙漠冰河)回复于 2005-03-08 10:08:05 得分 0
我是说删除所有表中的那个字段,
alter table 表 drop column rowguid 不是每个表都要写一次了,
我想能不能有一条语句就把那个字段全部删除。Top
3 楼LBYYBL(o_o)回复于 2005-03-08 10:14:58 得分 5
declare @id int
declare @nob int
declare @tname nvarchar(100)
select id=identity(int,1,1),name into #t from dbname..sysobjects where xtype='U'
set @id=1
set @nob=(select max(id) from #t)
set @tname=(select name from #t where id=@id)
while @id<=@nob
begin
exec('alter table '+@tname+' drop column rowguid')
set @id=@id+1
set @tname=(select name from #t where id=@id)
endTop
4 楼flybox728(淮予)回复于 2005-03-08 10:15:11 得分 2
用循环吧
select a.name,b.name from sysobjects a ,syscolumns b where a.id=b.id and a.xtype='u' and b.name='rowguid'
这样可以找出全部有 rowguid 字段的表
然后再在循环里
alter table 表 drop column rowguid
Top
5 楼Softlee81307(孔腎)回复于 2005-03-08 10:18:56 得分 5
declare @name varchar(20),@s varchar(1000)
declare kk cursor for
select bb=object_name(id) from syscolumns where objectproperty(id,'isUserTable')=1
and object_name(id)<>'dtproperties' and name='rowguid'
open kk
fetch next from kk into @name
while @@fetch_status=0
begin
set @s='alter table '+@name+' drop column rowguid '
exec(@s)
fetch next from kk into @name
end
close kk
deallocate kkTop
6 楼wzjcntlqs(要做钱的主人)回复于 2005-03-08 10:29:29 得分 1
SELECT top 100 PERCENT
TABLE_CODE=d.name,
COLUMNS_CODE=a.name
FROM syscolumns a
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
where a.name='字段名'
order by a.id,a.colorder
得到所有的表名,再执行删除字段Top
7 楼rfq(任凤泉)回复于 2005-03-08 10:38:25 得分 1
ALTER procedure dropCol
@strcol varchar(100)
as
declare cur_opentab cursor
for
select a.name from sysobjects a,syscolumns b where a.id=b.id and OBJECTPROPERTY(a.id,'IsUserTable')=1 and b.name=@strcol
declare @strtab varchar(100),@str varchar(100)
set @str=''
set @strtab=''
open cur_opentab
fetch next from cur_opentab into @strtab
while @@fetch_status=0
begin
set @str='alter table '+@strtab+' drop column '+@strcol
print @str
exec(@str)
set @str=''
fetch next from cur_opentab into @strtab
end
close cur_opentab
deallocate cur_opentab
GOTop
8 楼lengxiaowei(小伟)回复于 2005-03-08 11:45:26 得分 3
exec sp_msforeachtable @command1=N'
declare @s nvarchar(4000),@tbname sysname
select @s=N'''',@tbname=N''?''
if exists(select 1 from syscolumns where id=object_id(@tbname) and name=''ddd'')
if exists(select 1 from (select count(*) as tcount from syscolumns
where id=object_id(@tbname)) a where a.tcount>1)
exec(N''alter table ''+@tbname+'' drop column ddd '')
else
exec(N''drop table ''+@tbname)'Top
9 楼LoveLwn(小P)回复于 2005-03-08 11:59:19 得分 3
declare @id int
declare @nob int
declare @tname nvarchar(100)
select id=identity(int,1,1),name into #t from dbname..sysobjects where xtype='U'
set @id=1
set @nob=(select max(id) from #t)
set @tname=(select name from #t where id=@id)
while @id<=@nob
begin
exec('alter table '+@tname+' drop column rowguid')
set @id=@id+1
set @tname=(select name from #t where id=@id)
end
Top




