-1

I'm trying to insert yearly weekend details such as date, dayName into a SQL Server table using the following stored procedure

alter procedure usp_AddOfficeHolidays
    @paramName NVARCHAR(max)
as
begin
    DECLARE @Year AS INT,
            @FirstDateOfYear DATETIME,
            @LastDateOfYear DATETIME

    -- You can change @year to any year you desire
    SELECT @year = 2016

    SELECT @FirstDateOfYear = DATEADD(yyyy, @Year - 1900, 0)
    SELECT @LastDateOfYear = DATEADD(yyyy, @Year - 1900 + 1, 0)

    -- Creating Query to Prepare Year Data
    --declare dayN varchar(max)
    if (select COUNT(*) from tblWeekSettings) < 1
    begin
        ;WITH cte AS 
        (
            SELECT 
                1 AS DayID,
                @FirstDateOfYear AS FromDate,
                DATENAME(dw, @FirstDateOfYear) AS Dayname
            UNION ALL
            SELECT 
                cte.DayID + 1 AS DayID,
                DATEADD(d, 1 ,cte.FromDate),
                DATENAME(dw, DATEADD(d, 1 ,cte.FromDate)) AS Dayname
            FROM cte
            WHERE DATEADD(d, 1, cte.FromDate) < @LastDateOfYear
        )
        SELECT FromDate AS Date, Dayname
        FROM CTE
        WHERE DayName IN (SELECT Param FROM dbo.fn_MVParam(@paramName,','))
        OPTION (MaxRecursion 370)
    end
    else
    begin
        Select 'Exists'
    end
end

and executing it using

exec usp_AddOfficeHolidays 'Saturday,Sunday'

which returns the following result

enter image description here

This works perfectly fine, BUT I have been unable to add/insert these details into the following table

enter image description here

I face the following error when I try to access the WEEKEND details by its alias CTE

The statement terminated. The maximum recursion 100 has been exhausted before statement completion

Although I've added the clause

 OPTION (MaxRecursion 370)

suggested by these links which I found on stack overflow

EDIT

Basically i face the specified error when i try something like this

alter procedure usp_AddOfficeHolidays
@paramName NVARCHAR(max)
as
begin
----------------------------------------------------------
DECLARE @Year AS INT,
@FirstDateOfYear DATETIME,
@LastDateOfYear DATETIME
-- You can change @year to any year you desire
SELECT @year = 2016
SELECT @FirstDateOfYear = DATEADD(yyyy, @Year - 1900, 0)
SELECT @LastDateOfYear = DATEADD(yyyy, @Year - 1900 + 1, 0)
-- Creating Query to Prepare Year Data
--declare dayN varchar(max)
if (select COUNT(*) from tblWeekSettings) < 1

    begin
        ;WITH cte AS (
        SELECT 1 AS DayID,
        @FirstDateOfYear AS FromDate,
        DATENAME(dw, @FirstDateOfYear) AS Dayname
        UNION ALL
        SELECT cte.DayID + 1 AS DayID,
        DATEADD(d, 1 ,cte.FromDate),
        DATENAME(dw, DATEADD(d, 1 ,cte.FromDate)) AS Dayname
        FROM cte
        WHERE DATEADD(d,1,cte.FromDate) < @LastDateOfYear
        )



        SELECT FromDate AS Date, Dayname
        FROM CTE
        WHERE DayName IN(SELECT Param FROM dbo.fn_MVParam(@paramName,','))

        insert into tblWeekSettings(DayNo,WeekDayName,Dates)
        values('',Dayname,Date)

        OPTION (MaxRecursion 370)
    end

else
    begin
        Select 'Exists'
    end


--select cte
-----------------------------------------------------------
end

Any sort of help here would really be appreciated! I just need to insert the data in my the specified table!

Thank you!

Community
  • 1
  • 1
Aimal Khan
  • 1,009
  • 1
  • 12
  • 25
  • `if (select COUNT(*) from tblWeekSettings) < 1` -> `if not exists(select 1 from tbl...)` – Ivan Starostin Jul 28 '16 at 08:21
  • Show full error message with sp name, line number and so on. – Ivan Starostin Jul 28 '16 at 08:22
  • @ivanStarostin This stored Procedure WORKS fine i just need to insert the data into my table which i've mentioned in my question! – Aimal Khan Jul 28 '16 at 08:35
  • Is this an exercise? If not, why are you using recursion and are not exploiting the fact that a week has 7 days? You could just loop 52-53 times starting from the first weekend of the year and adding 7 days every loop. – FDavidov Jul 28 '16 at 08:36
  • @AimalKhan _"I face the following error when I try to access the WEEKEND details by its alias CTE"_ - show full error message with sp name, line number and so on. You are getting an exception, don't you? Show that exception without any editing. – Ivan Starostin Jul 28 '16 at 08:41
  • @FDavidov not an exercise, more like a task! i mean i found this code on the internet i'm just doing my job :D If you recommend a better solution i.e. getting weekend details with dayname, date on yearly basis and inserting it in the specified table as well, i'll be happy to follow your way! – Aimal Khan Jul 28 '16 at 08:41
  • Actually that happens when i put an INSERT statement in between the code! something like this.. . UNION ALL SELECT cte.DayID + 1 AS DayID, DATEADD(d, 1 ,cte.FromDate), DATENAME(dw, DATEADD(d, 1 ,cte.FromDate)) AS Dayname FROM cte WHERE DATEADD(d,1,cte.FromDate) < @LastDateOfYear ) SELECT FromDate AS Date, Dayname FROM CTE WHERE DayName IN(SELECT Param FROM dbo.fn_MVParam(@paramName,',')) insert into tblWeekSettings(DayNo,WeekDayName,Dates) values('','','') OPTION (MaxRecursion 370) – Aimal Khan Jul 28 '16 at 08:43
  • @AimalKhan, I'll provide an answer with a logical paradigm and leave for you the details (syntax, etc.). – FDavidov Jul 28 '16 at 08:44
  • how about selecting into a #temp table, then inserting into your table via a select of your #temp table? I can't understand exactly why this isn't working though, I'm wondering if your INSERT version has the maxrecursion flag on it – Cato Jul 28 '16 at 08:45
  • @Fdavidov i look forward to it. Thank you! – Aimal Khan Jul 28 '16 at 08:45
  • @andrewDeighton i can't figure that out either. – Aimal Khan Jul 28 '16 at 08:46
  • @ivanStarostin did you read my comment? i couldn't tag you into that comment cause stackoverflow didn't let me. – Aimal Khan Jul 28 '16 at 08:47
  • @AimalKhan are you getting exception _"The statement terminated. The maximum recursion 100 has been exhausted before statement completion"_ or not? – Ivan Starostin Jul 28 '16 at 08:50
  • @ivanStarOstin yes! Exactly. – Aimal Khan Jul 28 '16 at 08:51
  • Can you post full error message? with every technical information it contains. Not only user-readable part. – Ivan Starostin Jul 28 '16 at 08:52
  • @IvanStarOstin There you go. Msg 530, Level 16, State 1, Procedure usp_AddOfficeHolidays, Line 18 The statement terminated. The maximum recursion 100 has been exhausted before statement completion. BUT This only happens when i try to put an INSERT STATEMENT before this line OPTION (MaxRecursion 370) – Aimal Khan Jul 28 '16 at 08:53
  • Can you edit code in your question and add `insert` command as you add it to the version of sp which fails? – Ivan Starostin Jul 28 '16 at 08:56
  • @ivanStarOstin i've edited my question! – Aimal Khan Jul 28 '16 at 08:59
  • the sql you showed us at the bottom to do the INSERT no longer seems to have maxrecursion, although it is recursive ...try....FROM CTE WHERE DayName IN(SELECT Param FROM dbo.fn_MVParam(@paramName,',')) insert into tblWeekSettings(DayNo,WeekDayName,Dates) OPTION (MaxRecursion 30000) – Cato Jul 28 '16 at 09:12

3 Answers3

1

That's the error:

    SELECT FromDate AS Date, Dayname
    FROM CTE
    WHERE DayName IN(SELECT Param FROM dbo.fn_MVParam(@paramName,','))

I split this code to make you understand what code actually is working in this case:

    insert into tblWeekSettings(DayNo,WeekDayName,Dates)
    values('',Dayname,Date)
    OPTION (MaxRecursion 370)

OPTION (MAX RECURSION) now belongs to single insert statement. Which is standalone, totally not related to CTE.

You actually need this, I suppose:

    ;with CTE (...)
    insert into tblWeekSettings(DayNo,WeekDayName,Dates)
    SELECT FromDate AS Date, Dayname
    FROM CTE
    WHERE DayName IN(SELECT Param FROM dbo.fn_MVParam(@paramName,','))
    OPTION (MaxRecursion 370)

but there are three columns in target table whilst your select has only two columns. So you'll have to update your select.

Some tips about INSERT-SELECT:
http://www.w3schools.com/sql/sql_insert_into_select.asp

this code:

    insert into tblWeekSettings(DayNo,WeekDayName,Dates)
    values('',Dayname,Date)

does not have any source for inserting. This is not valid code - you don't have here any Dayname,Date variables - they are not even referenced with @ as variables. It's totally not valid code.

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
0

For anyone who face the following issues

  • Getting Weeked details i.e. DayName, Date
  • Inserting into a Table

This stored procedure would do the trick.

alter procedure usp_AddOfficeHolidays
@paramName NVARCHAR(max)
as
begin
----------------------------------------------------------
DECLARE @Year AS INT,
@FirstDateOfYear DATETIME,
@LastDateOfYear DATETIME
-- You can change @year to any year you desire
SELECT @year = 2016
SELECT @FirstDateOfYear = DATEADD(yyyy, @Year - 1900, 0)
SELECT @LastDateOfYear = DATEADD(yyyy, @Year - 1900 + 1, 0)
-- Creating Query to Prepare Year Data
--declare dayN varchar(max)
if (select COUNT(*) from tblWeekSettings) < 1

    begin
        ;WITH cte AS (
        SELECT 1 AS DayID,
        @FirstDateOfYear AS FromDate,
        DATENAME(dw, @FirstDateOfYear) AS Dayname
        UNION ALL
        SELECT cte.DayID + 1 AS DayID,
        DATEADD(d, 1 ,cte.FromDate),
        DATENAME(dw, DATEADD(d, 1 ,cte.FromDate)) AS Dayname
        FROM cte
        WHERE DATEADD(d,1,cte.FromDate) < @LastDateOfYear
        )


        insert into tblWeekSettings(DayNo,Dates,WeekDayName) 
        SELECT '',FromDate AS Date, Dayname
        FROM CTE WHERE DayName IN(SELECT Param FROM dbo.fn_MVParam(@paramName,',')) 

        OPTION (MaxRecursion 30000) 




    end

else
    begin
        Select 'Exists'
    end


--select cte
-----------------------------------------------------------
end

Plus this article could really be helpful.

Aimal Khan
  • 1,009
  • 1
  • 12
  • 25
0

Try this, I hope this is useful for you.

alter procedure usp_AddOfficeHolidays
@paramName NVARCHAR(max)
as
begin
----------------------------------------------------------
DECLARE @Year AS INT,@DayNo as int=1,
@FirstDateOfYear DATETIME,
@LastDateOfYear DATETIME
-- You can change @year to any year you desire
SELECT @year = 2016
SELECT @FirstDateOfYear = DATEADD(yyyy, @Year - 1900, 0)
SELECT @LastDateOfYear = DATEADD(yyyy, @Year - 1900 + 1, 0)
Select getdate() DateOfYear into #tbl where 1=0
-- Creating Query to Prepare Year Data
--declare dayN varchar(max)
if (select COUNT(*) from tblWeekSettings) < 1

    begin
            while (@FirstDateOfYear< @LastDateOfYear)
            begin
                Insert Into #tbl (DayNo,DateOfYear) values (@DayNo,@FirstDateOfYear)
                set @FirstDateOfYear+=1
                set @DayNo+=1;
            End

            Insert Into tblWeekSettings (DayNo,WeekDayName,Dates)
            SELECT DayNo,DATENAME(dw, DateOfYear) Name,DateOfYear AS Date
            FROM #tbl
            WHERE DATENAME(dw, DateOfYear) IN(SELECT Param FROM dbo.fn_MVParam(@paramName,','))

    end

else
    begin
        Select 'Exists'
    end


--select cte
-----------------------------------------------------------
end