Click here to Skip to main content
14,427,367 members
   

Database

 
PinnedHOW TO ANSWER A QUESTION PinPopular
Chris Maunder16-Jul-09 4:10
cofounderChris Maunder16-Jul-09 4:10 
PinnedHow to get an answer to your question PinPopular
Chris Maunder10-Nov-05 17:30
cofounderChris Maunder10-Nov-05 17:30 
QuestionOracle SQL query to MS-SQL Pin
Member 951930620-Jan-20 23:37
MemberMember 951930620-Jan-20 23:37 
AnswerRe: Oracle SQL query to MS-SQL Pin
Richard Deeming21-Jan-20 2:43
communityengineerRichard Deeming21-Jan-20 2:43 
Questionsplit time then group by 15 min mongodb Pin
Member 1300557210-Jan-20 22:56
MemberMember 1300557210-Jan-20 22:56 
QuestionMessage Closed Pin
8-Jan-20 0:12
Memberqweitmen8-Jan-20 0:12 
QuestionSQL Query with a Loop Pin
Bobby Underwood6-Jan-20 5:58
MemberBobby Underwood6-Jan-20 5:58 
AnswerRe: SQL Query with a Loop Pin
#realJSOP6-Jan-20 6:04
mva#realJSOP6-Jan-20 6:04 
AnswerRe: SQL Query with a Loop Pin
Bobby Underwood6-Jan-20 6:11
MemberBobby Underwood6-Jan-20 6:11 
AnswerRe: SQL Query with a Loop Pin
#realJSOP6-Jan-20 6:16
mva#realJSOP6-Jan-20 6:16 
GeneralRe: SQL Query with a Loop Pin
Bobby Underwood6-Jan-20 6:22
MemberBobby Underwood6-Jan-20 6:22 
AnswerRe: SQL Query with a Loop Pin
Jörgen Andersson7-Jan-20 6:52
communityengineerJörgen Andersson7-Jan-20 6:52 
QuestionMessage Closed Pin
2-Jan-20 1:00
Memberaskinranks2-Jan-20 1:00 
QuestionMessage Closed Pin
2-Jan-20 0:59
Memberaskinranks2-Jan-20 0:59 
Questionincorrect syntax near begin expecting external for Alter Procedure Pin
simpledeveloper27-Dec-19 10:23
Membersimpledeveloper27-Dec-19 10:23 
Hi - I have a script file which does lot DDL Operations - it all included as part of the Transaction but I am getting an Error at the Begin statement of Alter Procedure as "incorrect syntax near begin expecting external" - is there any way to put the Alter Procedure as part of the Transaction Script? Here is my Script for it:
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

Any help please? Thank you.
AnswerRe: incorrect syntax near begin expecting external for Alter Procedure Pin
Jörgen Andersson28-Dec-19 0:14
communityengineerJörgen Andersson28-Dec-19 0:14 
QuestionAdding multiple columns with condition Pin
simpledeveloper26-Dec-19 15:58
Membersimpledeveloper26-Dec-19 15:58 
AnswerRe: Adding multiple columns with condition Pin
Mycroft Holmes26-Dec-19 20:58
MemberMycroft Holmes26-Dec-19 20:58 
AnswerRe: Adding multiple columns with condition Pin
Jörgen Andersson27-Dec-19 4:40
communityengineerJörgen Andersson27-Dec-19 4:40 
GeneralRe: Adding multiple columns with condition Pin
simpledeveloper27-Dec-19 6:29
Membersimpledeveloper27-Dec-19 6:29 
GeneralRe: Adding multiple columns with condition Pin
Jörgen Andersson27-Dec-19 6:32
communityengineerJörgen Andersson27-Dec-19 6:32 
GeneralRe: Adding multiple columns with condition Pin
simpledeveloper27-Dec-19 9:44
Membersimpledeveloper27-Dec-19 9:44 
QuestionMessage Closed Pin
25-Dec-19 5:46
MemberMember 1469959425-Dec-19 5:46 
QuestionMessage Closed Pin
25-Dec-19 5:46
MemberMember 1469959425-Dec-19 5:46 
QuestionCrystal Report to SQL server 2012 database Pin
Member 1447460719-Dec-19 11:39
MemberMember 1447460719-Dec-19 11:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.