自動偵測表中有沒有該字段,既可以避免刪除出錯,也可以提高程序的靈活性。
/*
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