--要删除的列名
DECLARE @fieldname sysname
SET @fieldname='id'
--删除处理
DECLARE @command nvarchar(2000),@whereand nvarchar(2000)
SELECT @command=N'ALTER TABLE ? DROP COLUMN '
+QUOTENAME(@fieldname),
@whereand=N'AND EXISTS(SELECT * FROM syscolumns WHERE id=o.id AND name='
+QUOTENAME(@fieldname,N'''')+N')'
EXEC sp_MSforeachtable @command1=@command,@whereand=@whereand
(2)删除所有表中指定类型的字段。
--要删除的列的数据类型
DECLARE @fieldtype sysname
SET @fieldtype='varchar'
--删除处理
DECLARE hCForEach CURSOR GLOBAL
FOR
SELECT N'ALTER TABLE '+QUOTENAME(o.name)
+N' DROP COLUMN '+QUOTENAME(c.name)
FROM sysobjects o,syscolumns c,systypes t
WHERE o.id=c.id
AND OBJECTPROPERTY(o.id,N'IsUserTable')=1
AND c.xusertype=t.xusertype
AND t.name=@fieldtype
EXEC sp_MSforeach_Worker @command1=N'?'