How to Tell if a Computed Column is Deterministic in SQL Server

When you create a computed column in SQL Server, the expression you use for the column will either be deterministic or nondeterministic. This can have implications, such as whether or not you can use it in an index or flag it as “persisted”.

A deterministic column is one that will return the same value for a specific set of input values and given the same state of the database. A nondeterministic column can return a different value even when given the same input even if the database state remains the same. For example, a function that returns the current date is nondeterministic, because it will return a different value each day.

You can use the COLUMNPROPERTY() function with the IsDeterministic argument to find out whether or not a computed column is deterministic.

Continue reading

How to Check if a Computed Column is “Persisted” in SQL Server

When you create a computed column in SQL Server, you have the option of flagging it as “persisted”. A persisted computed column is one that is physically stored in the table. If you don’t specify that it’s persisted, then the column’s value will be calculated each time you run a query against it.

You can query the sys.computed_columns system catalog view to find out whether a computed column is marked as persisted.

Continue reading

Change an Existing Column to a Computed Column in SQL Server (T-SQL Example)

This article demonstrates how to use T-SQL to change an existing column to a computed column in SQL Server.

A computed column is a virtual column that uses an expression to calculate its value. The expression will typically use data from other columns. A computed column is not physically stored in the table unless it’s marked PERSISTED.

Continue reading

Add a Computed Column to an Existing Table in SQL Server

This article demonstrates how to use T-SQL to add a computed column to an existing table in SQL Server.

A computed column is a virtual column that uses an expression to calculate its value. The expression will typically use data from other columns. A computed column is not physically stored in the table unless it’s marked PERSISTED.

Continue reading

How to Use the IDENTITY() Function in SQL Server

In SQL Server, you can use the IDENTITY() function to insert an identity column into a new table.

However, this function is not to be confused with the IDENTITY() property, which is used with the CREATE TABLE and ALTER TABLE statements.

The IDENTITY() function is used only in a SELECT statement with an INTO table clause. So you can use it when transferring data from one table to another, for example.

Continue reading