DECLARE @planid INT,@prno VARCHAR(20)
DECLARE @TempTable TABLE(id INT identity,planid INT,prno VARCHAR(20),logdate DATETIME)
DECLARE pr_cursor CURSOR FOR
SELECT DISTINCT planid,prno FROM ETS_PR_PLAN_DETAIL
OPEN pr_cursor
FETCH NEXT FROM pr_cursor INTO @planid,@prno
WHILE @@FETCH_STATUS =0
BEGIN
INSERT INTO @TempTable(planid,prno,logdate)
SELECT TOP 2 planid,prno,logdate FROM ETS_PR_PLAN_DETAIL WHERE planid=@planid and prno=@prno ORDER BY planid ASC,prno ASC, logdate DESC
FETCH NEXT FROM pr_cursor INTO @planid,@prno
END