2013-04-09 3 views
0

SQL에서 반복 문제가 있습니다. 종료 날짜에 도달 할 때까지 시작 날짜를 반복하고 싶습니다. 커서를 사용하지 않고 SQL에서 삽입 문을 반복합니다.

그들은 커서를 사용하지 않는 나에게 말했다, 그래서 나는이 같은 예를 발견

with mycte as 
(
select cast('2007-01-01' as datetime) DateValue 
union all 
select DateValue + 1 
from mycte 
where DateValue + 1 < '2030-12-31' 
) 
select * from mcte 

이 작품을, 그래서 난 내 상황 변수를 변경 :

with View_Solidnet_Training as 
(
select StartingDate as DateValue 
union all 
insert into OBJ_Availability values(34, DateValue + 1, 'AM', 2, 'Test') 
select DateValue + 1 
from View_Solidnet_Training 
where DateValue + 1 < EndingDate 
) 
select * from View_Solidnet_Training 

하지만

Msg 156, Level 15, State 1, Line 5 Incorrect syntax near the keyword 'insert'. Msg 128, Level 15, State 1, Line 5 The name "DateValue" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted. Msg 102, Level 15, State 1, Line 9 Incorrect syntax near ')

+0

그리고 오류는 무엇입니까? –

+0

메시지 156, 수준 15, 상태 1, 줄 5 'insert'키워드 근처의 구문이 잘못되었습니다. 메시지 128, 수준 15, 상태 1, 줄 5 "DateValue"라는 이름은이 컨텍스트에서 허용되지 않습니다. 유효한 표현식은 상수, 상수 표현식 및 (일부 컨텍스트에서) 변수입니다. 열 이름은 허용되지 않습니다. 메시지 102, 수준 15, 상태 1, 줄 9 ')'근처에 구문이 잘못되었습니다. – user2206834

+1

중요한 정보를 의견으로 게시하지 마십시오. 질문 편집 –

답변

0

시도하십시오 :

with View_Solidnet_Training as 
(
    select @StartingDate as DateValue 

    union all 

    select DateValue + 1 
    from View_Solidnet_Training 
    where DateValue + 1 < @EndingDate 
) 
insert into OBJ_Availability 
select 34, DateValue + 1, 'AM', 2, 'Test' from View_Solidnet_Training 

제공 @StartingDate@EndingDate 두 개의 날짜 시간 변수 ADN 테이블 OBJ_Availability는 CTE의 선택 순서에 5 열을 포함해야합니다.

+0

좋지만 오류 : 메시지 156, 수준 15, 상태 1, 줄 11 'select'키워드 근처의 구문이 잘못되었습니다. – user2206834

+0

'StartingDate'는'@ StartingDate'와 같은 변수 여야하며 이에 대한 최소 날짜 값을 설정해야합니다. – TechDo

+0

내 질문에 답변 할 수 없습니다. 링크 : http://stackoverflow.com/questions/15896138/looping-sql-statement-insert – user2206834

0

이 (테스트되지 않은)을 시도해보십시오 : 다음과 같은 오류가

with mycte as 
(
    select 34, cast('2007-01-01' as datetime) DateValue, 'AM', 2, 'Test' 
    union all 
    select 34, DateValue + 1, 'AM', 2, 'Test' 
    from mycte 
    where DateValue + 1 < '2030-12-31' 
) 
insert into OBJ_Availability (select * from mcte) 
관련 문제