Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

TSQL - Erstellen Sie eine gespeicherte Prozedur in einer Transaktionsanweisung

versuchen Sie es mit der create procedure in EXEC('...') , etwa so:

Begin Try
Begin Transaction 
    -- do a bunch of add/alter tables here
    -- do a bunch of data manipulation/population here

    -- create a stored proc
  EXEC ('create procedure dbo.test
  as
  begin
    select * from some_table
  end')
Commit  
End Try
Begin Catch
    Rollback  
    Declare @Msg nvarchar(max)
    Select @Msg=Error_Message();
    RaisError('Error Occured: %s', 20, 101,@Msg) With Log;
End Catch

GO