Use to add a field to a table. You can detect if there is a field in the table.
If not, add the field.
/*
Author:nyb
Time:2003/12/29
Fixtime:
AIM: See if there is a field in the table, if not, add
Input:
Call:
Execute Funcaddcolumn ' A ', ' C ', ' int '
Execute Funcaddcolumn ' A ', ' ddd ', ' varchar (100) '
*/
CREATE PROCEDURE Funcaddcolumn
@tablename varchar (128),--table name
@Columnname varchar (128),---column name
@ColumnType varchar (128)--column definition
As
Set @tablename = LTrim (RTrim (@tablename))
Set @Columnname = LTrim (RTrim (@Columnname))
Set @ColumnType = LTrim (RTrim (@ColumnType))
DECLARE @string varchar (8000)
IF not EXISTS (SELECT * from syscolumns where id=object_id (@tablename) and name = @Columnname)
Begin
Select @string = ' ALTER TABLE ' + @tablename + ' ADD [' + LTrim (RTrim (@Columnname)) + '] ' + @ColumnType + ' null '
Print @string
Execute (@string)
End
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.