Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 环中环_Sql_Sql Server_Loops_Common Table Expression - Fatal编程技术网

Sql 环中环

Sql 环中环,sql,sql-server,loops,common-table-expression,Sql,Sql Server,Loops,Common Table Expression,情境:我有两张桌子。其中有两个字段StartDate和EndDate。和一个只有一个字段日期的表。所以当你在开始日期和结束日期之间有3天的时间。他必须在新表中插入3行 我有下一个代码,它在表中插入了我的行 with View_Solidnet_Training as ( select cast('2013-04-09' as datetime) DateValue union all select DateValue + 1 from View_Solidnet_Training where

情境:我有两张桌子。其中有两个字段StartDate和EndDate。和一个只有一个字段日期的表。所以当你在开始日期和结束日期之间有3天的时间。他必须在新表中插入3行

我有下一个代码,它在表中插入了我的行

with View_Solidnet_Training as
(
select  cast('2013-04-09' as datetime) DateValue
union all
select DateValue + 1
from View_Solidnet_Training
where DateValue + 1 <= cast('2013-04-11' as datetime)
)
insert into OBJ_Availability  
select 34, DateValue, 'AM', 2, 'Test' from View_Solidnet_Training;
但现在,他在新表中插入行之后,就停止了。但是在循环之后,他必须在视图中下一行的新值中再次更改开始和结束日期:view\u Solidnet\u Training


所以,有没有可能的解决方案,或者我应该做一个新的循环,检查视图的ID是否为零?

根据我对您的问题的理解,我认为您应该从将日期输入列表开始,然后插入

样本:

create table Dates
(
startdate datetime,
endDate datetime
)

insert dates
SELECT '2013-04-06','2013-04-08'

SELECT * from Dates

Declare @date int
Declare  @tbl table 
(
date_ datetime
)

SELECT @date = datediff(day,startDate-1,EndDate) from Dates
SELECT @date

while(@date != 0 )
Begin 

insert into @tbl
SELECT dateadd(day,@date,StartDate-1) from dates

set @date = @date -1

END

/*
--TO-DO
--Update StartDate and EndDate values in table Dates
-- insert YourTable 
-- select date_ from @tbl
*/
SELECT * from @tbl
order by date_