Add a column to a table with a default value equal to the value of an existing column

后端 未结 7 1930
日久生厌
日久生厌 2020-12-05 03:41

How to add a column to a SQL Server table with a default value that is equal to value of an existing column?

I tried this T-SQL statement:

ALTER TABL         


        
相关标签:
7条回答
  • 2020-12-05 04:28

    You can use computed column to insert new column in a table based on an existing column value

    ALTER TABLE dbo.TableName ADD NewColumn AS (OldColumn) PERSISTED;
    

    OR, if you want to make some changes to the value based on existing column value, use

    ALTER TABLE dbo.TableName ADD NewColumn AS (OldColumn * 1.5) PERSISTED;
    
    0 讨论(0)
提交回复
热议问题