Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - How to ALTER COLUMN on a computed column

Tags:

sql

sql-server

I'm working with SQL Server 2008. Is it possible to alter a computed column without actually dropping the column and then adding it again (which I can get to work)? For example, I have this table:

CREATE TABLE [dbo].[Prices](
[Price] [numeric](8,3) NOT NULL,
[AdjPrice] AS [Price] / [AdjFactor],
[AdjFactor] [numeric](8,3) NOT NULL)

Later realizing that I have a potential divide by zero error I want to alter the [Adjprice] column to handle this, but if I just drop the column and add it again, I lose the column order.

I want to do something like:

ALTER TABLE dbo.[Prices]
ALTER COLUMN [AdjPrice] AS (CASE WHEN [AdjFactor] = 0 THEN 0 ELSE [Price] / [AdjFactor] END)

But this isn't correct. If this is possible, or there is another solution, I would appreciate the help.

like image 916
Bill Heine Avatar asked Feb 16 '10 16:02

Bill Heine


People also ask

Can we update a computed column in SQL?

If we update the records in the SQL table, and computed columns calculate updated value once we retrieve data again. However, we cannot update or insert values in the virtual computed columns in SQL Server.

Can we alter computed column in SQL Server?

However, it is a usual perception that you can alter any computed column with the help of SQL Server management Studio (SSMS) without dropping it. Incorrect syntax near the keyword 'As'. Frankly speaking there is no way you can alter any computed column without dropping it.

Is it possible to modify a datatype of a column when column contains data?

You can modify the data type of a column in SQL Server by using SQL Server Management Studio or Transact-SQL. Modifying the data type of a column that already contains data can result in the permanent loss of data when the existing data is converted to the new type.

How do you modify an existing column?

SQL query to change the column type in SQL Server databaseTbl_name: Specify the table name. Col_name: Specify the column name whose datatype you want to change. The col_name must be specified after the ALTER COLUMN keyword. Datatype: Specify the new datatype and length of the column.


2 Answers

Unfortunately, you cannot do this without dropping the column first.

From MSDN:

ALTER COLUMN
Specifies that the named column is to be changed or altered. ALTER COLUMN is not allowed if the compatibility level is 65 or lower. For more information, see sp_dbcmptlevel (Transact-SQL).

  • The modified column cannot be any one of the following:

    • A computed column or used in a computed column.
like image 121
Nick Craver Avatar answered Sep 21 '22 11:09

Nick Craver


if you must maintain order, copy the data into a duplicate table, then rebuild the table to keep your column order, then copy the data from the duplicate table back in.

Just be sure to do this when there is no activity going on.

like image 24
Jason Avatar answered Sep 20 '22 11:09

Jason