2013-07-12 2 views
0

간단한 while 루프를 작성하려고합니다.SQL while 루프가 무한 루프에 걸렸습니다.

declare @colname as varchar ='' 

while @colname is not null 
begin 
    Select @colname = col1 
    FROM Table1 
    WHERE col1 in ('test1','test2','test3') 

    if(@colname is not null) 
    begin 
    exec sp('@colname') 
    end 

end 

마지막으로 찾은 행의 값이 점점 커지고있는 것 같습니다. 이 문제를 해결하는 방법에 대한 제안.

업데이트 : select 문에서 반환되는 각 값에 대해 저장 프로 시저를 호출하고 있습니다. 대신 논리는 커서를 사용하여 작성되었습니다. 따라서 커서를 while 루프로 변환하려고합니다. 감사

+1

I를 루프는 그냥 선택 문 앞에 null로 설정 @colname 필요 영원히

계속 그것이 결코 null이 될 것이라고 생각하지 마십시오. @colname은 SELECT의 마지막 값을 포함합니다. –

+2

SQL의 루프는 일반적으로 필요하지 않습니다. 루프를 작성해야합니까? 그리고 다른 사람들이 지적했듯이 SELECT 문은 항상 'test1', 'test2'또는 'test3'값이없는 한 값을 반환합니다. –

+0

이 while 루프를 사용하여 수행하려는 작업은 무엇입니까? 그것은 우리가 당신의 논리로 무엇을 수정해야하는지에 대한 조언을 제공하는 데 도움이 될 수 있습니다. –

답변

0

내가 스크립트를 알아 보았하지 않습니다,하지만 어쩌면이 유용 할 것입니다 :

declare @colname as varchar ='' 

while NULLIF(@colname,'') is not null 
begin 
    Select @colname = col1 
    FROM Table1 
    WHERE col1 in ('test1','test2','test3') 
end 

귀하의 문제는 ''<> NULL은 "동안의 조건"에 있습니다. 어쩌면 당신이 역시 할 수있다 :

while isnull(@colname,'') <> '' 

또는

while coalesce(@colname,'') <> '' 

어쨌든 난 당신의 쿼리가이 동안이 방법을 사용하기에 좀 더 복잡한 것으로 가정합니다.

0

하지만 당신이 정말로 당신이 루프 나갈 것인지, 이런 종류의 물건을 어떻게해야하는 경우,이 시도 :

declare @colname as varchar ='' 

while @colname is not null 
begin 
Select @colname = col1 
FROM Table1 
WHERE col1 in ('test1','test2','test3') 
Select @colname = null 
end 

편집 :

@rs이 거의 있었다.

이 시도 :

declare @t table (colname varchar(10)) 
insert into @t 
select distinct col1 
FROM Table1 
WHERE col1 in ('test1','test2','test3') 

declare @colname as varchar(10) ='' 
while @colname is not null 
begin 
    -- get one row from table variable 
    Select top 1 @colname = colname 
    FROM @t 

    --execute stored procedure 
    exec sp('@colname') 

    --delete row from table variable so that you don't read it again 
    delete from @t where colname = @colname 
    --set @colname to null if there is no more value to process 
if ((select count(*) from @t) = 0) 
begin 
select @colname = null 
end 
end 
+0

@rs라고 말하는 것이 아니라 단지 한 번만 부를 것이라고 생각합니다. select colname = null은 colname 값을 null로 설정합니다. 그러면 루프가 중지됩니다. –

+0

예 thats는 true이지만 찾은 모든 값에 대해 SP를 실행하지는 않지만 오직 하나만 실행합니다. –

+0

@rs 알아요 ... SP가 처음 대답했을 때 거기에 없었습니다 ... 그리고 OP가 자신의 질문을 편집했을 때 똑같은 말을했습니다. –

0

declare @t table (colname varchar(10)) 
insert into @t 
select distinct col1 
FROM Table1 
WHERE col1 in ('test1','test2','test3') 

declare @colname as varchar ='' 
declare @cnt int = 0; 

--set count used in loop condition 
select @cnt = count(*) from @t 

while @cnt > 0 
begin 
    -- get one row from table variable 
    Select top 1 @colname = colname 
    FROM @t 

    --execute stored procedure 
    if(@colname is not null) 
    begin 
    exec sp('@colname') 
    end 

    --delete row from table variable so that you don't read it again 
    delete from @t where colname = @colname 

    select @cnt = count(*) from @t 
end 
+0

거의 이것입니다 ...하지만 마지막 이름과 루프가 여전히 붙어 있습니다. 무한 –

+0

@FabienTheSolution, 사실입니다. 마지막 값을 처리하기 위해 코드를 업데이트했습니다. –

0

내 생각 엔 당신이 저장 프로 시저의 시리즈를 실행하려고하는 것입니다보십시오. 이 절차는 table1.col1에 저장됩니다. 내가 좋아하는 뭔가를 할 것이다 다음

DECLARE @ColName VARCHAR(MAX) 
SET @ColName = '' 

SELECT @ColName = col1 
FROM Table1 
WHERE Col1 > @ColName 
ORDER BY Col1 

While @Colname IS NOT NULL 
BEGIN 
EXEC SP(@colname) 

SELECT @ColName = col1 
FROM Table1 
WHERE col1 > @ColName 
AND col1 in ('test1', 'test2', 'test3') 
ORDER BY col1 

END 
+0

첫 번째 선택에서 상위 1 개가 필요하다고 생각합니다. 그렇지 않으면 마지막 값으로 설정됩니다. test9라고 말하면 while 루프는 반환하지 않습니다. 모든 행 –

1

SELECT 문이 실행되지 않습니다 (@ COLNAME = COLNAME) 다음 변수의 할당을 행이 반환하지 않습니다 - 그리고 @colname의 값이 그대로 유지 - 이전에서 null이 아닌 값 반복 - 행이 실제로 발견 된 경우 또는 체크 @@ 확인 SELECT 문 후 행 개수 권리를 - - 그리고하지 않을 경우 - 종료 루프

관련 문제