Inserting weekends into SQL Server table -


i'm trying insert yearly weekend details such date, dayname sql server table using following stored procedure

alter procedure usp_addofficeholidays     @paramname nvarchar(max) begin     declare @year int,             @firstdateofyear datetime,             @lastdateofyear datetime      -- can change @year year desire     select @year = 2016      select @firstdateofyear = dateadd(yyyy, @year - 1900, 0)     select @lastdateofyear = dateadd(yyyy, @year - 1900 + 1, 0)      -- creating query prepare year data     --declare dayn varchar(max)     if (select count(*) tblweeksettings) < 1     begin         ;with cte          (             select                  1 dayid,                 @firstdateofyear fromdate,                 datename(dw, @firstdateofyear) dayname             union             select                  cte.dayid + 1 dayid,                 dateadd(d, 1 ,cte.fromdate),                 datename(dw, dateadd(d, 1 ,cte.fromdate)) dayname             cte             dateadd(d, 1, cte.fromdate) < @lastdateofyear         )         select fromdate date, dayname         cte         dayname in (select param dbo.fn_mvparam(@paramname,','))         option (maxrecursion 370)     end     else     begin         select 'exists'     end end 

and executing using

exec usp_addofficeholidays 'saturday,sunday' 

which returns following result

enter image description here

this works fine, but have been unable add/insert these details following table

enter image description here

i face following error when try access weekend details alias cte

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

although i've added clause

 option (maxrecursion 370) 

suggested these links found on stack overflow

edit

basically face specified error when try this

alter procedure usp_addofficeholidays @paramname nvarchar(max) begin ---------------------------------------------------------- declare @year int, @firstdateofyear datetime, @lastdateofyear datetime -- can change @year year desire select @year = 2016 select @firstdateofyear = dateadd(yyyy, @year - 1900, 0) select @lastdateofyear = dateadd(yyyy, @year - 1900 + 1, 0) -- creating query prepare year data --declare dayn varchar(max) if (select count(*) tblweeksettings) < 1      begin         ;with cte (         select 1 dayid,         @firstdateofyear fromdate,         datename(dw, @firstdateofyear) dayname         union         select cte.dayid + 1 dayid,         dateadd(d, 1 ,cte.fromdate),         datename(dw, dateadd(d, 1 ,cte.fromdate)) dayname         cte         dateadd(d,1,cte.fromdate) < @lastdateofyear         )            select fromdate date, dayname         cte         dayname in(select param dbo.fn_mvparam(@paramname,','))          insert tblweeksettings(dayno,weekdayname,dates)         values('',dayname,date)          option (maxrecursion 370)     end  else     begin         select 'exists'     end   --select cte ----------------------------------------------------------- end 

any sort of here appreciated! need insert data in specified table!

thank you!

that's error:

    select fromdate date, dayname     cte     dayname in(select param dbo.fn_mvparam(@paramname,',')) 

i split code make understand code working in case:

    insert tblweeksettings(dayno,weekdayname,dates)     values('',dayname,date)     option (maxrecursion 370) 

option (max recursion) belongs single insert statement. standalone, totally not related cte.

you need this, suppose:

    ;with cte (...)     insert tblweeksettings(dayno,weekdayname,dates)     select fromdate date, dayname     cte     dayname in(select param dbo.fn_mvparam(@paramname,','))     option (maxrecursion 370) 

but there 3 columns in target table whilst select has 2 columns. you'll have update select.

some tips insert-select:
http://www.w3schools.com/sql/sql_insert_into_select.asp

this code:

    insert tblweeksettings(dayno,weekdayname,dates)     values('',dayname,date) 

does not have source inserting. not valid code - don't have here dayname,date variables - not referenced @ variables. it's totally not valid code.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -