declare @i int declare @cnt int declare @ABC varchar(50) set @ABC ='' select @cnt = count(A) from B where companyCode='3036' and carNum='1004238' and isUse='Y' set @i = 0 while(@i < @cnt) begin select @ABC =carcode from B where companyCode='3036' and carNum='1004238' and isUse='Y' set @i = @i + 1 print(@ABC) end
이렇게 쓰고있는데
select @cnt = count(A) from B where companyCode='3036' and carNum='1004238' and isUse='Y'
이 쿼리값은 3개가 나옵니다
하지만
select @ABC =carcode from B where companyCode='3036' and carNum='1004238' and isUse='Y' 이값은 각각
1000002
1000003
1000005
가 있습니다. 하지만 저 루프문을 태우면 1000005만 3번 나오는상황이라 이럴땐 어떤식으로 처리해야할지 알려주시면 감사하겠습니다!
DECLARE @cnt INT SET @cnt = 0 DECLARE @abc VARCHAR(50) SET @abc = '' DECLARE cur CURSOR FOR SELECT carcode FROM b WHERE companyCode = '3036' AND carNum = '1004238' AND isUse = 'Y' OPEN cur FETCH NEXT FROM cur INTO @abc WHILE @@fetch_status = 0 BEGIN @cnt = @cnt + 1 print(@abc) FETCH NEXT FROM cur INTO @abc END CLOSE cur DEALLOCATE cur