Is it possible to create a column and insert values ββinto it during the same transaction? This is part of the update script. I found the following method online , but it does not work; I get an error: Invalid column name 'IndexNumber'. . I assume this is because the transaction has not yet created a column, so there is nothing to insert.
Relevant parts of my script:
Print 'Beginning Upgrade' Begin Transaction -- -------------------------------------------------------------------- USE [MyDatabase]; ALTER TABLE [dbo].[Widgets] ADD [IndexNumber] [int] NULL; DECLARE @ind INT SET @ind = 0 UPDATE [dbo].[Widgets] SET @ind = [IndexNumber] = @ind + 1; ALTER TABLE [dbo].[Widgets] ALTER COLUMN [IndexNumber] [int] NOT NULL; -- -------------------------------------------------------------------- Commit tran Print 'Upgrade completed'
The reason [IndexNumber] not an identifier column is because it must be editable.
sql-server tsql alter-table transactions
Nenotlep May 6 '14 at 11:55 a.m. 2014-05-06 11:55
source share