Specify Computed Columns in a Table
Before You Begin
Limitations and Restrictions
-
A computed column cannot be used as a DEFAULT or FOREIGN KEY constraint definition or with a NOT NULL constraint definition. However, if the computed column value is defined by a deterministic expression and the data type of the result is allowed in index columns, a computed column can be used as a key column in an index or as part of any PRIMARY KEY or UNIQUE constraint. For example, if the table has integer columns a and b, the computed column a + b may be indexed, but computed column a + DATEPART(dd, GETDATE()) cannot be indexed, because the value might change in subsequent invocations.
-
A computed column cannot be the target of an INSERT or UPDATE statement.
Security
Permissions
Requires ALTER permission on the table.
Using SQL Server Management Studio
To add a new computed column
-
In Object Explorer, expand the table for which you want to add the new computed column. Right-click Columns and select New Column.
-
Enter the column name and accept the default data type (nchar(10)). The Database Engine determines the data type of the computed column by applying the rules of data type precedence to the expressions specified in the formula. For example, if the formula references a column of type money and a column of type int, the computed column will be of type money because that data type has the higher precedence. For more information, see Data Type Precedence (Transact-SQL).
-
In the Column Properties tab, expand the Computed Column Specification property.
-
In the (Formula) child property, enter the expression for this column in the grid cell to the right. For example, in a
SalesTotal
column, the formula you enter might beSubTotal+TaxAmt+Freight
, which adds the value in these columns for each row in the table.Important
When a formula combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence. If the conversion is not a supported implicit conversion, the error "
Error validating the formula for column column_name.
" is returned. Use the CAST or CONVERT function to resolve the data type conflict. For example, if a column of type nvarchar is combined with a column of type int, the integer type must be converted to nvarchar as shown in this formula('Prod'+CONVERT(nvarchar(23),ProductID))
. For more information, see CAST and CONVERT (Transact-SQL). -
Indicate whether the data is persisted by choosing Yes or No from the drop-down for the Is Persisted child property.
-
On the File menu, click Savetable name.
To add a computed column definition to an existing column
-
In Object Explorer, right-click the table with the column for which you want to change and expand the Columns folder.
-
Right-click the column for which you want to specify a computed column formula and click Delete. Click OK.
-
Add a new column and specify the computed column formula by following the previous procedure to add a new computed column.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2018-03-12 write data to xml