T-SQL:事务和 try-catch 块中的 ALTER VIEW 错误

T-SQL: ALTER VIEW error within a transaction and try-catch block

我正在使用 SQL Server 2005 和 SQL Server 2005 Management Studio Express。我有一个具有以下方案的脚本:

USE [MyDatabase]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

BEGIN TRANSACTION;
GO

BEGIN TRY
    ALTER VIEW dbo.MyView
    AS
    SELECT ...
    GO
    ALTER TABLE ...
    GO
    UPDATE dbo.MyTable ...
    GO
    INSERT INTO dbo.AnotherTable ...
    GO
END TRY
BEGIN CATCH
    SELECT 
        ERROR_NUMBER() AS ErrorNumber
        ,ERROR_SEVERITY() AS ErrorSeverity
        ,ERROR_STATE() AS ErrorState
        ,ERROR_PROCEDURE() AS ErrorProcedure
        ,ERROR_LINE() AS ErrorLine
        ,ERROR_MESSAGE() AS ErrorMessage;

    IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION;
END CATCH;

IF @@TRANCOUNT > 0
    COMMIT TRANSACTION;
GO

问题是我在 ALTER VIEW dbo.MyView 块中收到错误: "Incorrect syntax near reserved word 'VIEW'."

这没有给我任何语法错误,试试这个。我刚刚删除了动态 sql.

末尾的 "Go"
USE [MyDatabase]
go
SET ANSI_NULLS ON
go
SET QUOTED_IDENTIFIER ON
go
BEGIN TRANSACTION
go
BEGIN TRY
EXEC sp_executesql N'ALTER VIEW dbo.MyView
AS
SELECT * FROM ....; '
EXEC sp_executesql N'ALTER TABLE ...;'
EXEC sp_executesql N'UPDATE ....;'
END TRY
BEGIN CATCH
SELECT 
    ERROR_NUMBER() AS ErrorNumber
    ,ERROR_SEVERITY() AS ErrorSeverity
    ,ERROR_STATE() AS ErrorState
    ,ERROR_PROCEDURE() AS ErrorProcedure
    ,ERROR_LINE() AS ErrorLine
    ,ERROR_MESSAGE() AS ErrorMessage;

IF @@TRANCOUNT > 0
    ROLLBACK TRANSACTION;
END CATCH;
IF @@TRANCOUNT > 0
COMMIT TRANSACTION;
GO