2016-07-28 113 views
-1

我试图插入每年周末的详细信息,如日期,DAYNAME到使用插入到周末SQL Server表

exec usp_AddOfficeHolidays 'Saturday,Sunday' 
使用以下存储过程

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 

并执行一个SQL Server表

其返回以下结果

enter image description here

这工作完全正常,我一直无法添加/插入这些细节如下表

enter image description here

我面对下面的错误到,当我试图通过它的别名CTE访问WEEKEND细节

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

虽然我已经添加了条款

OPTION (MaxRecursion 370) 

建议通过这些链接,我的堆栈溢出发现

EDIT

基本上我所面临的特定错误,当我尝试这样的事情

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 

An y在这里的帮助真的很感谢!我只需要将数据插入到指定的表中!

谢谢!

+0

'如果(SELECT COUNT(*)从tblWeekSettings)< 1' ->'如果不存在(选择1从TBL ...)用菌名,行号等等' –

+0

显示完整的错误消息。 –

+0

@ivanStarostin这个存储过程工作正常,我只需要将数据插入到我的问题中提到的我的表中! –

回答

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变量 - 它们甚至不被@作为变量引用。这完全不是有效的代码。

+0

您如何期望VALUES进入tblWeekSettings?只是好奇! –

+0

这就是'INSERT ... SELECT'语句所做的。 –

+0

@AimalKhan你应该接受这个答案,如果它是正确的。 –

0

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

  • 获取Weeked细节即DAYNAME,日期
  • 插入到表

此存储过程会做的伎俩。

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 

加上这篇文章可以真正有帮助。

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 
+0

感谢您的评论@ m.jamshaidAlam但我得到了答案! –