Add column to table and then update it inside transaction

后端 未结 6 1785
北荒
北荒 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:11

    GO is not a T-SQL command. Is a batch delimiter. The client tool (SSM, sqlcmd, osql etc) uses it to effectively cut the file at each GO and send to the server the individual batches. So obviously you cannot use GO inside IF, nor can you expect variables to span scope across batches.

    Also, you cannot catch exceptions without checking for the XACT_STATE() to ensure the transaction is not doomed.

    Using GUIDs for IDs is always at least suspicious.

    Using NOT NULL constraints and providing a default 'guid' like '{00000000-0000-0000-0000-000000000000}' also cannot be correct.

    Updated:

    • Separate the ALTER and UPDATE into two batches.
    • Use sqlcmd extensions to break the script on error. This is supported by SSMS when sqlcmd mode is on, sqlcmd, and is trivial to support it in client libraries too: dbutilsqlcmd.
    • use XACT_ABORT to force error to interrupt the batch. This is frequently used in maintenance scripts (schema changes). Stored procedures and application logic scripts in general use TRY-CATCH blocks instead, but with proper care: Exception handling and nested transactions.

    example script:

    :on error exit
    
    set xact_abort on;
    go
    
    begin transaction;
    go
    
    if columnproperty(object_id('Code'), 'ColorId', 'AllowsNull') is null
    begin
        alter table Code add ColorId uniqueidentifier null;
    end
    go
    
    update Code 
      set ColorId = '...'
      where ...
    go
    
    commit;
    go
    

    Only a successful script will reach the COMMIT. Any error will abort the script and rollback.

    I used COLUMNPROPERTY to check for column existance, you could use any method you like instead (eg. lookup sys.columns).

    0 讨论(0)
  • 2020-12-08 13:14

    Have you tried it without the GO?

    Normally you should not mix table changes and data changes in the same script.

    0 讨论(0)
  • 2020-12-08 13:16

    I think you can use a ";" to terminate and execute eachn individual command, rather than GO.

    Note that GO is not part of Transact-SQL:

    http://msdn.microsoft.com/en-us/library/ms188037.aspx

    0 讨论(0)
  • 2020-12-08 13:22

    I almost agree with Remus but you can do this with SET XACT_ABORT ON and XACT_STATE

    Basically

    • SET XACT_ABORT ON will abort each batch on error and ROLLBACK
    • Each batch is separated by GO
    • Execution jumps to the next batch on error
    • Use XACT_STATE() will test if the transaction is still valid

    Tools like Red Gate SQL Compare use this technique

    Something like:

    SET XACT_ABORT ON
    GO
    BEGIN TRANSACTION
    GO
    
    IF COLUMNPROPERTY(OBJECT_ID('Color'), 'CodeID', ColumnId) IS NULL
       ALTER TABLE Color ADD CodeID [uniqueidentifier] NULL
    GO
    
    IF XACT_STATE() = 1
      UPDATE Color
      SET CodeID= 'B6D266DC-B305-4153-A7AB-9109962255FC'
      WHERE [Name] = 'Red'
    GO
    
    IF XACT_STATE() = 1
     COMMIT TRAN
    --else would be rolled back
    

    I've also removed the default. No value = NULL for GUID values. It's meant to be unique: don't try and set every row to all zeros because it will end in tears...

    0 讨论(0)
  • 2020-12-08 13:25

    Another alternative, if you don't want to split the code into separate batches, is to use EXEC to create a nested scope/batch as here

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题