自动侦测表中有没有该字段,既可以避免删除出错,也可以提高程序的灵活性。
/*
author:nyb
time :2003/12/29
fixtime :
aim :看表中是否有这个字段,如果有,删除
input :
调用:
execute FuncDropColumn 'PSZ31','Z31No'
*/
CREATE PROCEDURE FuncDropColumn
@tablename varchar(128), --表名
@Columnname varchar(128)--列名
as
set @tablename = ltrim(rtrim(@tablename))
set @Columnname = ltrim(rtrim(@Columnname))
declare @string varchar(8000)
IF EXISTS( select * from syscolumns where id=object_id(@tablename) and name = @Columnname )
begin
select @string =' ALTER TABLE '+ @tablename + ' DROP COLUMN [' + ltrim(rtrim(@Columnname)) + ']'
print @string
execute(@string)
end
GO
本文地址:http://com.8s8s.com/it/it19730.htm