将周末插入SQL Server表

时间:2016-07-28 08:12:05

标签: sql sql-server stored-procedures recursion

我正在尝试使用以下存储过程将年度周末详细信息(例如date,dayName)插入到SQL Server表中

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

并使用

执行它
exec usp_AddOfficeHolidays 'Saturday,Sunday'

返回以下结果

enter image description here

这完全正常,但是我无法将这些细节添加/插入下表

enter image description here

当我尝试通过别名 CTE

访问周末详细信息时,我遇到以下错误
  

声明终止。最大递归100已经用尽   在陈述完成之前

虽然我已添加条款

 OPTION (MaxRecursion 370)
这些链接建议我在堆栈溢出中找到

  

修改

当我尝试这样的事情时,我基本上面对指定的错误

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

真的很感激这里的任何帮助!我只需要在指定的表中插入数据!

谢谢!

3 个答案:

答案 0 :(得分:1)

这就是错误:

    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)

OPTION (MAX RECURSION)现在属于单个插入语句。这是独立的,与CTE完全无关。

我想你确实需要这个:

    ;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)

但目标表中有三列,而您的选择只有两列。因此,您必须更新您的选择。

有关INSERT-SELECT的一些提示:
http://www.w3schools.com/sql/sql_insert_into_select.asp

此代码:

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

没有任何插入来源。这不是有效的代码 - 这里没有任何Dayname,Date变量 - 它们甚至不会被@作为变量引用。这完全不是有效的代码。

答案 1 :(得分:0)

对于任何面临以下问题的人

  • 获取每周详细信息,即DayName,Date
  • 插入表格

这个存储过程可以解决问题。

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

另外,这篇文章真的很有帮助。

答案 2 :(得分:0)

试试这个,我希望这对你有用。

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