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

后端 未结 7 1935
日久生厌
日久生厌 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:10

    Try this:

    ALTER TABLE tablename ADD newcolumn type NOT NULL DEFAULT (0)
    Go
    Update tablename SET newcolumn = oldcolumn Where newcolumn = 0
    Go
    

提交回复
热议问题