Transferred from: http://www.maomao365.com/?p=5277
Summary:
Here's how to add a new field to a table in the "on-line system".
System deployment scripts, adding columns to the method:
In system script publishing, how to modify and add a stored procedure custom function view, we usually use the following steps to write such a script
1 Determine if the object (stored procedure custom Function view) exists and how it exists we delete the object (stored procedure custom Function view)
2 new Object
However, when adding system fields (columns), we cannot use the Delete table and then do the script processing again.
Then we usually use the first to determine whether the column exists, if it does not exist to create the column, if it exists, modify the column properties <span style= "Color:red;font-weight:bold;" > (Modify Column Properties--note whether historical data is affected) </span>
Cases:
IF not EXISTS(SELECT * fromSYS. COLUMNSWHERE object_id = object_id(N'Table name') andNAME= 'Column Name')---Determine if the column existsbegin---Add ColumnsAlter Table [Table name] Add column [Column Name] [column Type]EndElsebegin---Modify Column PropertiesAlter Table [Table name] Add column [Column Name] [column Type]End
The advantage of writing SQL deployment scripts in the above way is that SQL scripts can be executed multiple times without error messages, which prevents the database that has manually added columns from generating deployment error messages
MSSQL SQL Server System update, how to add table fields correctly