https://docs.microsoft.com/en-us/sql/relational-databases/tables/specify-computed-columns-in-a-table?view=sql-server-2017

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

  1. In Object Explorer, expand the table for which you want to add the new computed column. Right-click Columns and select New Column.

  2. 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).

  3. In the Column Properties tab, expand the Computed Column Specification property.

  4. 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 be SubTotal+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).

  5. Indicate whether the data is persisted by choosing Yes or No from the drop-down for the Is Persisted child property.

  6. On the File menu, click Savetable name.

To add a computed column definition to an existing column

  1. In Object Explorer, right-click the table with the column for which you want to change and expand the Columns folder.

  2. Right-click the column for which you want to specify a computed column formula and click Delete. Click OK.

  3. Add a new column and specify the computed column formula by following the previous procedure to add a new computed column.

Specify Computed Columns in a Table的更多相关文章

  1. 1503 - A PRIMARY KEY must include all columns in the table's partitioning function

    1503 - A PRIMARY KEY must include all columns in the table's partitioning function 错误的原因:表的主键字段必须包含分 ...

  2. 分区实践 A PRIMARY KEY must include all columns in the table's partitioning function

    MySQL :: MySQL 8.0 Reference Manual :: 23 Partitioning https://dev.mysql.com/doc/refman/8.0/en/parti ...

  3. How to get the mapping relationship between two columns in a table

    If a table have column A and B Count(distinct A) as Da Count(distinct B) as Db Count(distinct A, B) ...

  4. [转]What are mode and status columns under gp_segment_configuration table

    February 16, 2017 10:39 Goal In this article we will try to understand and answer to the below two q ...

  5. flask启动常见问题1:sqlalchemy.exc.ArgumentError: Mapper mapped class UserCode->data_system_user_email could not assemble any primary key columns for mapped table 'data_system_user_email'

    我的描述:当我编辑好flask以后,ORM映射数据库完成,启动项目时,发生现象: 解决: 看字面的意思是主键导致的错误,于是我查看了data_system_user_email的键参数配置,发现表没有 ...

  6. SQL Server ->> Computed Column(计算列)

    Computed Column(计算列)是自SQL Server 2005开始就有的特性.计算列的定义是一个表达式.表达式可以是非计算列,常量,函数间的组合.但是不可以是子查询. 计算列数据固化 默认 ...

  7. Element UI table组件源码分析

    本文章从如下图所示的最基本的table入手,分析table组件源代码.本人已经对table组件原来的源码进行削减,源码点击这里下载.本文只对重要的代码片段进行讲解,推荐下载代码把项目运行起来,跟着文章 ...

  8. 封装Vue纵向表头左右结构的table表格

    我们前端开发人员在使用表格的过程中,大概率碰到的都是表格头部在表格的最上边,然后呈一行展示,紧接着就是表格的每一行的每一个单元格来展示具体内容的场景,很少会遇到表格的头部呈纵向一行展示,也就是说表格的 ...

  9. [转] Oracle analyze table 使用总结

    转自:http://www.cnblogs.com/einyboy/archive/2012/08/09/2630321.html analyze table 一般可以指定分析: 表,所有字段,所有索 ...

随机推荐

  1. 洛谷P3389 高斯消元 / 高斯消元+线性基学习笔记

    高斯消元 其实开始只是想搞下线性基,,,后来发现线性基和高斯消元的关系挺密切就一块儿在这儿写了好了QwQ 先港高斯消元趴? 这个算法并不难理解啊?就会矩阵运算就过去了鸭,,, 算了都专门为此写个题解还 ...

  2. Django单元测试简单示例

    对一个功能的验证往往是需要很多多测试用例,可以把测试用例集合在一起执行,这就产生了测试套件TestSuite 的概念,它是用来组装单个测试用例,规定用例的执行的顺序,而且TestSuite也可以嵌套T ...

  3. mybatis之入门

    一.mybatis介绍 是apache旗下的一个开源的顶级ORM框架(做dao层的操作) 开始叫ibatis在2010年经过升级后发布到google code上就改名为mybatis 定位:1.是一个 ...

  4. 【Pyton】【小甲鱼】异常处理:你不可能总是对的

    Exception 1.assertionerror举例 >>> my_list=['小甲鱼是帅哥'] >>> assert len(my_list)>0 & ...

  5. hibernate中cascade和inverse

    原文:http://blog.sina.com.cn/s/blog_7b9edd020100racc.html 这两个属性都用于一多对或者多对多的关系中. 而inverse特别是用于双向关系,在单向关 ...

  6. c++中的构造(包括移动),赋值(包括移动),析构详解

    这五种操作:构造(包括移动),赋值(包括移动),析构其实就是定义了对一个对象进行构造,赋值,析构时的行为.理解这些行为并不复杂,复杂的是理解在继承下这些行为的表现.需要注意的是他们并不会被继承(传统意 ...

  7. liferay中数据库表的解析未完

    页面布局 1:表layout 主要的字段有: 字段 privateLayout 0表示的是公开的页面 字段 layoutId 如果在同一个社区中有很多的界面,layoutId表示各个界面,按照顺序排列 ...

  8. webbench,简单、实用!

    官网:http://home.tiscali.cz/~cz210552/webbench.html   1.下载并安装 # wget http://home.tiscali.cz/~cz210552/ ...

  9. JavaScript循环练习

    1.蓝球弹起的高度篮球从10米高的地方落下,每次弹起的高度是原来的0.3倍,问弹跳10次之后篮球的高度. <script type="text/javascript"> ...

  10. 使用i5ting_toc 预览 markdown 文件

    i5ting_toc__tree https://github.com/i5ting/i5ting_ztree_toc 是去哪儿网前段架构师狼叔编写的一个jQuery插件,用于将markdown 转化 ...