MSSQL SQL Server System update, how to add table fields correctly

Source: Internet
Author: User
Tags mssql

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

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.