How to create a column and copy another column to the newly created

I am trying to perform a simple task, create a new column in a table and immediately try to copy the value of another column in the same table to the new created column, but I got

Invalid column name "COMMENT_TMP". mistake

SQL

Invalid column name "COMMENT_TMP".

ALTER TABLE TASK_COMMENT ADD COMMENT_TMP text; UPDATE TASK_COMMENT SET TASK_COMMENT.COMMENT_TMP = COMMENT; 
+6
sql-server tsql copy
source share
2 answers

First you need to send the ALTER packet to the server before executing UPDATE . Add GO after ALTER statement

+6
source share

Add the batch separator and table name to the update statement.

 ALTER TABLE TASK_COMMENT ADD COMMENT_TMP text; GO UPDATE TASK_COMMENT SET COMMENT_TMP = COMMENT; 
+8
source share

All Articles