Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9022971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:38:51+00:00 2026-06-16T05:38:51+00:00

I have written the following stored procedure that uses a Date Dimension table to

  • 0

I have written the following stored procedure that uses a Date Dimension table to calculate the next occurrence of a date given a begin date and a frequency.

I am getting some odd syntax errors that seem to occur whenever I have an IF BEGIN SET IF SET END sequence.

In order to help debug, I have gone back in and added unneeded BEGIN and END statements around the one line IF statements.

Am I missing some type of BEGIN – END or IF – ELSE matching rule here? (I’ve marked the lines that the parser is complaining about with comments.)

CREATE PROCEDURE dbo.GetNextScheduledBusinessDayOccurance
(
  @BeginDate DATE,
  @Frequency CHAR(1)
)
AS
BEGIN
    DECLARE @Date DATETIME,
    @currentDate DATE,
    @weekOfYear INT,
    @dayOfWeek varchar(9),
    @weekOfMonth tinyint,
    @month varchar(2),
    @dayOfQuarter tinyint,
    @currentDayOfQuarter tinyint,
    @quarterOfNextOccurance tinyint,
    @yearOfNextOccurance int;

    SET @currentDate = CONVERT(Date, GETDATE())
    IF (@BeginDate > @currentDate)-- If it hasn't started yet
    BEGIN
        SET @Date = (SELECT TOP(1) [DATE] FROM dbo.dim_Date WHERE [DATE] >= @BeginDate AND [Date] > @currentDate AND (IsBusinessDay = 1) ORDER BY [DATE])
    END
    ELSE
        BEGIN
            IF @Frequency = 'A' --Annually
            BEGIN
                SET @Date = (SELECT TOP(1) [DATE] FROM dbo.dim_Date WHERE ([DATE] > @currentDate AND [Month] >= DATEPART(MONTH, @BeginDate) AND [Day] >= DATEPART(DAY, @BeginDate) AND IsBusinessDay = 1) ORDER BY [Date])
            END
            ELSE IF @Frequency = 'B' --Biweekly
                BEGIN
                    SET @weekOfYear = (SELECT WeekOfYear FROM dbo.dim_Date WHERE [Date] = @BeginDate)
                    SET @dayOfWeek = (SELECT [DayOfWeek] FROM Dbo.dim_Date WHERE [Date] >= @BeginDate AND IsBusinessDay = 1)
                    SET @Date = ((SELECT TOP(1) [Date] FROM dbo.dim_Date WHERE ([DATE] > @currentDate AND [WeekOfYear]%2 = @weekOfYear%2 AND [DayOfWeek] = @dayOfWeek AND IsBusinessDay = 1) ORDER BY [Date]))
                END
            ELSE IF @Frequency = 'D' --Daily
                BEGIN
                    SET @Date = ((SELECT TOP(1) [Date] FROM dbo.dim_Date WHERE ([DATE] > @currentDate AND IsBusinessDay = 1) ORDER BY [Date]))
                END
            ELSE IF @Frequency = 'E' -- Semi-Monthly (twice each month) (assume the 15th and last day of month)
                BEGIN
                    -- GET the next 15th or last day of month.
                    SET @Date = (SELECT TOP(1) [Date] FROM dim_Date WHERE [Date] > @currentDate AND (Day = '15' OR (DateAdd(day, -1, DateAdd( month, DateDiff(month , 0,[Date])+1 , 0)) = [Date])) ORDER BY [Date])
                    IF ((SELECT IsBusinessDay FROM dim_Date WHERE [Date] = @Date) = 0)
                    BEGIN
                        SET @Date = ((SELECT TOP(1) [Date] FROM dbo.dim_Date WHERE (([DATE] > @Date) AND IsBusinessDay = 1) ORDER BY [Date])
                    END --Parser ERROR: Incorrect syntax near the keyword 'END'.
                END
            ELSE IF @Frequency = 'I' --Bimonthly (Every other month)
                BEGIN
                    SET @Month = (SELECT Month FROM dbo.dim_Date WHERE [Date] = @BeginDate)
                    SET @Date = (SELECT TOP(1) [Date] FROM dbo.dim_Date WHERE ([Date] > @currentDate AND [Month]%2 = @Month%2 AND [Day] = DATEPART(day, @BeginDate)) ORDER BY [Date])
                    IF ((SELECT IsBusinessDay FROM dim_Date WHERE [Date] = @Date) = 0)
                    BEGIN
                        SET @Date = ((SELECT TOP(1) [Date] FROM dbo.dim_Date WHERE (([DATE] > @Date) AND IsBusinessDay = 1) ORDER BY [Date])
                    END --Parser ERROR: Incorrect syntax near the keyword 'END'.
                END
            ELSE IF @Frequency = 'L' --Last Business Day of the Month
                BEGIN
                    SET @Date = (SELECT TOP(1) [Date] FROM dim_Date WHERE [Date] > '2013-03-20 00:00:00.000' AND  (DateAdd(day, -1, DateAdd( month, DateDiff(month , 0,[Date])+1 , 0)) = [Date]) ORDER BY [Date])
                    IF ((SELECT IsBusinessDay FROM dim_Date WHERE [Date] = @Date) = 0)
                    BEGIN
                        SET @Date = ((SELECT TOP(1) [Date] FROM dbo.dim_Date WHERE (([DATE] < @Date) AND IsBusinessDay = 1) ORDER BY [Date] DESC))
                    END
                END     
            ELSE IF @Frequency = 'M'  -- Monthly
                BEGIN
                    SET @Date = (SELECT TOP(1) [Date] FROM dim_Date WHERE [Date] > @currentDate AND (Day > DATEPART(day, @BeginDate)) ORDER BY [Date])
                    IF ((SELECT IsBusinessDay FROM dim_Date WHERE [Date] = @Date) = 0)
                    BEGIN
                        SET @Date = ((SELECT TOP(1) [Date] FROM dbo.dim_Date WHERE (([DATE] > @Date) AND IsBusinessDay = 1) ORDER BY [Date])
                    END--Parser ERROR: Incorrect syntax near the keyword 'END'.
                END
            ELSE
            IF @Frequency = 'Q' --Quarterly
                BEGIN
                    SET @dayOfQuarter = (Select DayOfQuarter FROM dim_Date WHERE [DATE] = @BeginDate)
                    SET @currentDayOfQuarter = (Select DayOfQuarter FROM dim_Date WHERE [DATE] = @currentDate)
                    SET @quarterOfNextOccurance = (SELECT [Quarter] FROM dbo.dim_Date WHERE [Date] = @currentDate)
                    SET @yearOfNextOccurance = (SELECT [Year] FROM dbo.dim_Date WHERE [Date] = @currentDate)
                    --SELECT @dayOfQuarter DOQ, @currentDayOfQuarter curDOQ, @quarterOfNextOccurance QofNext, @yearOfNextOccurance YofNext
                    IF (@currentDayOfQuarter >= @dayOfQuarter)
                        SET @quarterOfNextOccurance = @quarterOfNextOccurance + 1
                    SET @Date = '1900-01-01'
                    WHILE (@Date < @currentDate) --Loop through this just in case we're close to the end of the Quarter, and there's no more business days left
                        BEGIN
                            IF (@quarterOfNextOccurance > 4)
                                BEGIN
                                    SET @quarterOfNextOccurance = @quarterOfNextOccurance%4
                                    SET @yearOfNextOccurance = @yearOfNextOccurance + 1         
                                END
                            --SELECT @quarterOfNextOccurance QofNext, @yearOfNextOccurance YofNext
                            --CREATE a temp table with all of the business dates from the target quarter in decending order.
                            SELECT * INTO ##temp FROM (SELECT TOP(92) [Date], MAX(DayOfQuarter) as DOQ FROM dim_Date WHERE (YEAR = @yearOfNextOccurance AND Quarter = @quarterOfNextOccurance AND IsBusinessDay = 1) Group By Date ORDER BY Date DESC) as foo
                            SET @Date = (SELECT TOP(1) DATE from ##temp WHERE DOQ <= @dayOfQuarter)
                            SET @quarterOfNextOccurance = @quarterOfNextOccurance + 1 -- Go ahead and incriment this just in case we start the WHILE over.
                            --SELECT * FROM ##temp
                            BEGIN TRY DROP TABLE ##temp END TRY BEGIN CATCH END CATCH
                        END
                END
            ELSE
            IF @Frequency = 'S' --Semi-annually
                BEGIN
                    SET @Date = @BeginDate
                    WHILE (@Date < @currentDate)
                    BEGIN
                        SET @Date = (SELECT [Date] FROM dbo.dim_Date WHERE [Date] = DATEADD(MONTH, 6, @Date))
                    END
                    IF ((SELECT IsBusinessDay FROM dim_Date WHERE [Date] = @Date) = 0)
                    BEGIN
                        SET @Date = ((SELECT TOP(1) [Date] FROM dbo.dim_Date WHERE (([DATE] > @Date) AND IsBusinessDay = 1) ORDER BY [Date])
                    END--Parser ERROR: Incorrect syntax near the keyword 'END'.
                END
            ELSE
            IF @Frequency = 'W' --Weekly
                BEGIN
                    SET @dayOfWeek = (SELECT [DayOfWeek] FROM dbo.dim_Date WHERE [Date] = @BeginDate)
                    SET @Date = (SELECT TOP(1) [Date] FROM Dbo.dim_Date WHERE DayOfWeek = @dayOfWeek AND [DATE] < @currentDate ORDER BY [DATE] DESC)
                    --SELECT @Date DATE, @BeginDate BeginDate
                    WHILE ((@Date < @currentDate) OR (@Date < @BeginDate))
                    BEGIN
                        SET @Date = (SELECT [Date] FROM dbo.dim_Date WHERE [Date] = DATEADD(Week, 1, @Date))
                        --SELECT @Date DateInWHile
                    END
                    IF ((SELECT IsBusinessDay FROM dim_Date WHERE [Date] = @Date) = 0)
                        SET @Date = (SELECT TOP(1) [Date] FROM dbo.dim_Date WHERE (([DATE] > @Date) AND IsBusinessDay = 1) ORDER BY [Date])
                    --SELECT @Date
                END
            ELSE
                SET @Date = NULL
        END
        RETURN @Date
END

GO
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:38:52+00:00Added an answer on June 16, 2026 at 5:38 am

    You’re missing a closing ):

    BEGIN
      SET @Date = ((
        SELECT TOP(1) [Date] 
        FROM dbo.dim_Date 
        WHERE (([DATE] > @Date) AND IsBusinessDay = 1) 
        ORDER BY [Date]) ) -- Paren added here...
    END --Parser ERROR: Incorrect syntax near the keyword 'END'.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good Morning, I've written the following stored procedure that searches for records that have
I have written the following stored procedure: ALTER PROCEDURE [dbo].[sp_Accounts_ValidateLogin] @EmailAddress varchar(255), @Password varchar(20)
I have written the following piece of code ( sql clr stored procedure )
I have written a stored procedure that returns my rows as XML with the
I have written following stored procedure. If I don’t use IF ELSE block Order
I have written the following procedure to create a series of 20 databases. But
I have written a PL/SQL (Ora10gR2) package which contains a stored procedure. The header
So here is my problem, I have written a stored procedure to do the
I have following Entity Framework Logic, and I want to translate to Stored Procedure,
I am using oracle 11g and have written a stored procedure which stores values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.