2013-11-25 6 views
0

하나의 행이 아닌 테이블에 대해 저장 프로 시저를 실행하려고합니다. 레코드를 테이블에 삽입하고 업데이트를해야하므로 저장 프로 시저를 선택합니다. 이 메서드는 걸려있는 것 같습니다. 설명 계획을 살펴 봤지만 내 논리가 잘못되어 한 레코드를 처리하고 중단합니다.테이블의 저장 프로 시저 실행

이것은 내가 지금까지 가지고있는 코드입니다. 내 저장 프로 시저가 짐승처럼 실행되고 나는 그것이 문제라고 생각하지 않는다.

---- Create a driver table for the stored procedure to run off . 
drop table #Driver_Table 

select col1, col2 
IDENTITY(int) AS idcol 
INTO #Driver_Table 
FROM CUSTOMER 
--- Contains about 37 k records 

--- Index added for performance 
CREATE CLUSTERED INDEX IDX_C_Driver_Table_UserID ON #Driver_Table(idcol) 

-- Define the last customer ID to be handled 
DECLARE @LastCustomerID INT 
SET @LastCustomerID = 0 

--- Other parameter the queries will use 
DECLARE @idcol INT 
DECLARE @col1 datetime 
DECLARE @col2 varchar(200) 

SET @idcol= 0 

-- Iterate over all customers 
BEGIN 

-- Get next customerId 
SELECT TOP 1 @idcol = idcol FROM #Driver_Table 
WHERE idcol > @LastCustomerID 

select TOP 1 @col1=col1 
FROM #Driver_Table 
WHERE idcol > @LastCustomerID 

select TOP 1 @col2=col2 
FROM #Driver_Table 
WHERE idcol > @LastCustomerID 

---- To get the process to end when last customer is processed. 
WHILE @col2 NOT NULL 

-- call your sproc 
EXEC SP @col1,@Col2 

-- set the last customer handled to the one we just handled 
SET @LastCustomerID = @idcol 
SET @col2 = NULL 

-- select the next customer to handle 
SELECT TOP 1 @col2 = col2 
FROM #Driver_Table 
WHERE idcol > @LastCustomerID 

END 

내가 거기에 볼 수 있습니다 제공된 정보를 사용하여 SQL Server 2005의

GO

+0

당신 수 없습니다 "테이블에 대한 저장 프로 시저를 실행"-하지만 당신은 저장 프로 시저를 변경 (또는 새로 추가) 모든 반대로 세트를 처리하는 수 이 임시 테이블/커서 바쁜 작업. SP의 정의를 보여 준다면 누군가가 당신을 도울 수 있습니다. –

답변

1

루프 구문은 당신이 END 블록을 BEGIN while 루프에서 작동 동봉 havent 한 잘못 ... 1 인 반면, 2nd while 루프가 실행될 때마다 temp 테이블의 레코드 수를 줄이지 않기 때문에 계속 실행되는 무한 while 루프가 있습니다. ... 이런 식으로 뭔가를 시도

WHILE (EXISTS (SELECT * FROM #Driver_Table)) 

BEGIN 

    SELECT TOP 1 @idcol = idcol, @col1=col1, @col2=col2 
    FROM #Driver_Table 


    EXEC SP @col1,@Col2 

    DELETE FROM #Driver_Table 
    WHERE idcol = @idcol; 


END 
+0

내 저장 프로 시저가 약간의 성능 문제를 보이고 있지만이 작동합니다. 나는 그것에 대해 별도의 질문을 제기 할 수 있습니다. 고맙습니다. –

+0

도움이 되니 기쁩니다. 나는 EXEC SP가 무엇인지 알지 못한다. 코드는 단순화 될 수있다. 그들은 항상 그들이하는 일을하는 더 좋은 방법이있다. :) –