开发者社区> 问答> 正文

语法不正确,即将开始期待外部更改程序

我有一个脚本文件,该脚本文件执行很多DDL操作-所有这些都作为事务的一部分包含在内,但是在Alter Procedure的Begin语句中出现错误,因为“语法不正确,即将开始预期外部”-有任何方法可以将Alter过程是事务脚本的一部分吗?这是我的脚本:

USE XXXXXX;
BEGIN TRY
    BEGIN TRANSACTION

    DROP INDEX IF EXISTS [IX_NOVId] ON [dbo].[Violations]

    PRINT N'Altering [dbo].[OneToManies]...';
    ALTER TABLE [dbo].[OneToManies] ALTER COLUMN [ChildEntity] NVARCHAR (MAX) NULL;
    ALTER TABLE [dbo].[OneToManies] ALTER COLUMN [ParentEntity] NVARCHAR (MAX) NULL;


    PRINT N'Altering [dbo].[usp_Report_ClosedReports]...';
    ALTER PROCEDURE [dbo].[usp_Report_ClosedReports]
        @EnforcementSectionId INT,
        @IsPreCase            VARCHAR(3) = NULL,
        @FromDate             DATE = '01/01/2017',
        @ToDate               DATE = '01/01/2018'

    AS
         BEGIN
         DECLARE @IsPreCaseBool Bit;
         SET @IsPreCaseBool = CASE WHEN @IsPreCase = '0' THEN 0 ELSE
                              CASE WHEN @IsPreCase = '1' THEN 1 ELSE null END END;

         SELECT
            ReferenceNumber,
            CONVERT(NVARCHAR(10), CaseStartDate, 101) AS CaseStartDate,
            CONVERT(NVARCHAR(10), DateCreated, 101) AS DateCreated,
            CONVERT(NVARCHAR(10), DateUpdated, 101) AS DateUpdated,

            CONVERT(NVARCHAR(10), StatuteOfLimitationsDate, 101) AS StatuteOfLimitationsDate,       

            ApplicablePenalties,
            InvestigatorFullName,
            ContactName,
            CompanyCity,
            CompanyZip,
            CaseStatus,
            EnforcementSectionName,
            ISNULL(ViolationsCount, 0) AS ViolationsCount,
            Program
        FROM (SELECT cs.CaseId,
                        (CASE
                             WHEN [cst].IsPreCase = 1
                             THEN 'I'
                             ELSE 'C'
                         END) + dbo.CIntToChar([cs].CaseId, 5) AS ReferenceNumber,
                        [cs].CaseStartDate,
                        [cs].DateCreated,
                        [cs].DateUpdated,
                        [cs].StatuteOfLimitationsDate,
                        [cs].ApplicablePenalties,
                        inv.FirstName +' '+inv.LastName AS InvestigatorFullName,
                        ISNULL([cnt].FirstName, '')+' '+ISNULL([cnt].LastName, '') AS ContactName,
                        [cnt].Address_City AS CompanyCity,
                        [cnt].Address_Zip AS CompanyZip,
                        [cst].CaseStatusName AS CaseStatus,
                        [ens].EnforcementSectionName,
                        vl.ViolationsCount,
                        [PROG].Program
                 FROM Cases AS [cs]
                      JOIN vw_CasePrograms AS PROG ON PROG.CaseId = [cs].CaseId
                      LEFT JOIN CaseAssignedToInvestigators ctoi ON ctoi.CaseId = cs.CaseId
                      LEFT JOIN Contacts AS [cnt] ON [cnt].ContactId = [cs].CaseCompanyId
                      LEFT JOIN CaseStatus AS [cst] ON [cst].CaseStatusId = [cs].CaseStatusId


                      LEFT JOIN (select Id, UG.GroupId, FirstName, LastName 
                                 from AspNetUsers  AS U
                                 join UserGroup AS UG on UG.UserId = U.Id)  
                                    AS inv ON  (inv.Id = ctoi.UserId  AND CST.IsPreCase <> 1)--AND INV.GroupId = 10)
                                             OR (inv.Id = cs.AssignedToInspectorId AND cst.IsPreCase = 1)-- only on investigation


                      LEFT JOIN EnforcementSections AS [ens] ON [ens].EnforcementSectionId = [cs].EnforcementSectionId
                      LEFT JOIN  (
                             SELECT COUNT(1) AS ViolationsCount,
                                    v.CaseId
                             FROM dbo.Violations v
                             GROUP BY v.CaseId
                      ) vl ON vl.CaseId = cs.CaseId
                 WHERE([cst].IsPreCase = @IsPreCaseBool
                       OR @IsPreCaseBool IS NULL)
                      AND [cst].IsCaseClosed = 1                  
                      AND [cs].DateUpdated BETWEEN @FromDate and @ToDate
                      AND [cs].EnforcementSectionId = @EnforcementSectionId) AS QR
        GROUP BY 
            ReferenceNumber,
            CaseStartDate,
            DateCreated,
            DateUpdated,
            StatuteOfLimitationsDate,
            ApplicablePenalties,
            InvestigatorFullName,
            ContactName,
            CompanyCity,
            CompanyZip,
            CaseStatus,
            EnforcementSectionName,
            ViolationsCount,
            Program
        END;

    PRINT N'Altering [dbo].[SP_ViolationTypesBranched]...';

    ALTER PROCEDURE [dbo].[SP_ViolationTypesBranched]
        (@Types as NVARCHAR(max) = '',
        @Search AS NVARCHAR(MAX) = '')
    AS 
    BEGIN
        select 
             BranchId
            ,BranchName
            ,ViolationTypeName
            ,BranchViolationName
            ,ViolationTypeCode
            ,ViolationTypeId
            ,ViolationTypeSortOrder
            ,UploadedPhotographCategoryGroup
            ,Id = null
            --Sections

        FROM (
            select 
                VT.BranchId,
                BR.Name AS BranchName,
                BR.Name + ' - ' + ViolationTypeName AS BranchViolationName,
                ViolationTypeName,          
                ViolationTypeCode,
                VT.ViolationTypeId,
                ViolationTypeSortOrder ,
                UploadedPhotographCategoryGroup,
                (SELECT 
                    ES.EnforcementSectionName + ', ' 
                 FROM EnforcementSections AS ES 
                 WHERE ES.BranchId = VT.BranchId 
                 FOR XML PATH('')) AS Sections
            from BranchViolationTypes AS VT
            JOIN Branches       AS BR ON BR.BranchId = VT.BranchId
            --JOIN EnforcementSections AS FS ON FS.BranchId = BR.BranchId
            JOIN ViolationTypes AS VV ON VV.ViolationTypeId = VT.ViolationTypeId
        ) AS X
        WHERE dbo.DynoSearch(
            ISNULL(CAST(X.BranchName        AS NVARCHAR(MAX)),'') + 
            ISNULL(CAST(X.ViolationTypeCode AS NVARCHAR(MAX)),'') + 
            ISNULL(CAST(X.ViolationTypeName AS NVARCHAR(MAX)),'') + 
            ISNULL(CAST(x.Sections          AS NVARCHAR(MAX)),''), 
        @Search) = 1


    END

    ALTER TABLE [dbo].[ViolationTypeNOVs] WITH CHECK CHECK CONSTRAINT [FK_dbo.ViolationTypeNOVs_dbo.ViolationTypes_ViolationTypeId];
    PRINT N'Update complete.';  

    ROLLBACK TRAN -- Transaction Success!
END TRY
BEGIN CATCH
    IF @@TRANCOUNT > 0
        ROLLBACK TRAN --RollBack in case of Error

    -- you can Raise ERROR with RAISEERROR() Statement including the details of the exception
    RAISERROR(ERROR_MESSAGE(), ERROR_SEVERITY(), 1)
END CATCH

有什么帮助吗?谢谢。

展开
收起
祖安文状元 2020-01-04 15:23:18 537 0
1 条回答
写回答
取消 提交回答
  • 您不能在批处理周围使用BEGIN TRY和BEGIN CATCH。您可以使用最后一个批处理来检查先前的所有步骤是否成功(例如,通过检查目录视图(例如sys.objects))。然后,您可以确定批处理是否全部成功,然后提交还是回滚。

    我认为您应该这样写:

    Begin transaction ....
    
    ...
    Print N’any think that you like’;
    Go
    
    Alter procedure ....
    ...
    
    2020-01-04 15:23:32
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
复杂升学环境下的语言交互:技术与实践 立即下载
ACE 区域技术发展峰会:Flink Python Table API入门及实践 立即下载
“静态调用链路发现”应用场景分析及实践探索 立即下载

相关实验场景

更多