Add column to table and then update it inside transaction

后端 未结 6 1787
北荒
北荒 2020-12-08 12:33

I am creating a script that will be run in a MS SQL server. This script will run multiple statements and needs to be transactional, if one of the statement fails the overall

6条回答
  •  [愿得一人]
    2020-12-08 13:32

    Orthogonal to Remus's comments, what you can do is execute the update in an sp_executesql.

    ALTER TABLE [Table] ADD [Xyz] NVARCHAR(256);
    
    DECLARE @sql NVARCHAR(2048) = 'UPDATE [Table] SET [Xyz] = ''abcd'';';
    EXEC sys.sp_executesql @query = @sql;
    

    We've needed to do this when creating upgrade scripts. Usually we just use GO but it has been necessary to do things conditionally.

提交回复
热议问题