Sqlserver:计算列
CREATE TABLE CREATE TABLE [ database_name . [ owner ] . | owner . ] table_name ( { column_definition | column_name AS computed_column_expression | table_constraint } [ , ... n ] ) 计算 列是物理上并不存储在表中的虚拟列。 计算 列由同一表中的
CREATE TABLE
CREATE TABLE [ database_name.[ owner ] . | owner. ] table_name
( {
| column_name AS computed_column_expression
| } [ ,...n ]
)
计算列是物理上并不存储在表中的虚拟列。计算列由同一表中的其它列通过表达式计算得到。例如,计算列可以这样定义:cost AS price * qty。表达式可以是非计算列的列名、常量、函数、变量,也可以是用一个或多个运算符连接的上述元素的任意组合。表达式不能为子查询。
计算列可用于选择列表、WHERE 子句、ORDER BY 子句或任何其它可使用常规表达式的位置,但下列情况除外:
-
计算列不能用作 DEFAULT 或 FOREIGN KEY 约束定义,也不能与 NOT NULL 约束定义一起使用。但是,如果计算列由具有确定性的表达式定义,并且索引列中允许计算结果的数据类型,则可将该列用作索引中的键列,或用作 PRIMARY KEY 或 UNIQUE 约束的一部分。
例如,如果表中含有整型列 a 和 b,则可以在计算列 a+b 上创建索引。但不能在计算列 a+DATEPART(dd, GETDATE()) 上创建索引,因为在以后的调用中,其值可能发生改变。
-
计算列不能作为 INSERT 或 UPDATE 语句的目标。
说明 表中计算列所使用的列值因行而异,因此每行的计算列值可能不同。
计算列的为空性是由 SQL Server 根据使用的表达式自动确定的。即使只有不可为空的列,大多数表达式的结果也认为是可为空的,因为可能的下溢或溢出也将生成 NULL 结果。使用 COLUMNPROPERTY 函数(AllowsNull 属性)查看表中任何计算列的为空性。通过指定 ISNULL(check_expression, constant),其中常量为替代任何 NULL 结果的非 NULL 值,可为空的表达式 expr 可以转换为不可为空的表达式。
在计算列上创建索引
只要满足下列要求,则可在计算列上定义索引:
-
computed_column_expression 必须是确定性的。如果表达式对一组给定的输入总是返回同样的结果,则该表达式是确定性的。在下列情况中,computed_column_expression 是确定性的:
- 表达式引用的所有函数都是确定和精确的。这包括用户定义的函数和内置函数。有关更多信息,请参见确定性函数和非确定性函数。
- 表达式引用的所有列都来自包含计算列的表。
- 没有列引用从多行拉数据。例如,聚合函数(如 SUM 或 AVG)依靠来自多行的数据,这使得 computed_column_expression 不确定。
COLUMNPROPERTY 函数的 IsDeterministic 属性报告 computed_column_expression 是否确定。
计算列表达式在下列情况中精确:
- 非 float 数据类型表达式。
- 其定义中不使用 float 数据类型。例如,在下列语句中,列 y 为 int 型并且确定,但不精确:
<code>CREATE TABLE t2 (a int, b int, c int, x float, <br> y AS CASE x <br> WHEN 0 THEN a <br> WHEN 1 THEN b <br> ELSE c <br> END)</code>
Copy after login
COLUMNPROPERTY 函数的 IsPrecise 属性报告 computed_column_expression 是否精确。
说明 任何 float 型表达式都被认为是不精确的,不能作为索引键;float 型表达式可以用在索引视图中,但不能作为键。对于计算列同样如此。如果函数、表达式、用户定义的函数或视图定义中包含任何 float 表达式(包括逻辑表达式(比较)),则被认为是不确定的。
- 表达式引用的所有函数都是确定和精确的。这包括用户定义的函数和内置函数。有关更多信息,请参见确定性函数和非确定性函数。
- 执行 CREATE TABLE 语句时,必须将 ANSI_NULL 连接级选项设置为 ON。OBJECTPROPERTY 函数通过 IsAnsiNullsOn 属性报告此选项是否已打开。
- 为计算列定义的 computed_column_expression 不能取值为 text、ntext 或 image 数据类型。
- 对于创建索引的连接和所有尝试使用 INSERT、UPDATE 或 DELETE 语句更改索引值的连接,必须使六个 SET 选项设置为 ON,一个选项设置为 OFF。如果不具有上述选项设置的连接执行了任何 SELECT 语句,优化器将忽略计算列上的索引。
下列选项必须设置为 ON:
- ANSI_NULLS
- ANSI_PADDING
- ANSI_WARNINGS
- ARITHABORT
- CONCAT_NULL_YIELDS_NULL
- QUOTED_IDENTIFIER
除上述 ON 设置外,NUMERIC_ROUNDABORT 选项必须设置为 OFF。有关更多信息,请参见影响结果的 SET 选项。
- ANSI_NULLS
©1988-2004 Microsoft Corporation. 保留所有权利。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

General Matrix Multiplication (GEMM) is a vital part of many applications and algorithms, and is also one of the important indicators for evaluating computer hardware performance. In-depth research and optimization of the implementation of GEMM can help us better understand high-performance computing and the relationship between software and hardware systems. In computer science, effective optimization of GEMM can increase computing speed and save resources, which is crucial to improving the overall performance of a computer system. An in-depth understanding of the working principle and optimization method of GEMM will help us better utilize the potential of modern computing hardware and provide more efficient solutions for various complex computing tasks. By optimizing the performance of GEMM

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

WORD is a powerful word processor. We can use word to edit various texts. In Excel tables, we have mastered the calculation methods of addition, subtraction and multipliers. So if we need to calculate the addition of numerical values in Word tables, How to subtract the multiplier? Can I only use a calculator to calculate it? The answer is of course no, WORD can also do it. Today I will teach you how to use formulas to calculate basic operations such as addition, subtraction, multiplication and division in tables in Word documents. Let's learn together. So, today let me demonstrate in detail how to calculate addition, subtraction, multiplication and division in a WORD document? Step 1: Open a WORD, click [Table] under [Insert] on the toolbar, and insert a table in the drop-down menu.

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

SQL Server database files are usually stored in the following default location: Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data The database file location can be customized by modifying the database file path setting.

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.
