2012-08-23 4 views
0

2 열이있는 테이블이 있습니다. 첫 번째 열에는 반복되는 값이 있습니다. 한 번에 각 고유 값을 선택하고 싶습니다. , 나는 SQL에서 임시 테이블을 만들었지 만 oracle sql 개발자는 어떻게 코드를 작성합니까?임시 테이블을 만들거나 루프의 열에서 고유 한 값만 선택하는 방법

CREATE TABLE look_up_table 
(row_id INT NOT NULL, 
attribute VARCHAR(500), 
VARCHAR(700) 
) 
/* now manually populating this table */ 
INSERT INTO look_up_table 
VALUES 
(1, grmacolor_frame_access, black); 
(2, grmacolor_frame_access, blue); 
(3, grmacolor_frame_access, red); 
(4, grmamaterial_frame_access, acetate); 
(5, grmamaterial_frame_access, metal); 
(6, grmamaterial_frame_access, nylon); 
(7, grmamaterial_frame_access, plastic); 

DECLARE @temp_col_val NVARCHAR (700), @counter1 INT, 
SET @counter1 = 0; 
SET @column_count = (SELECT COUNT (DISTINCT attribute) FROM look_up_table); 

CREATE TABLE #temp1 AS 
SELECT DISTINCT attribute AS attrib, 
ROW_NUMBER() OVER (ORDER BY attribute) AS seqno1, 
FROM look_up_table; 

WHILE (@counter1 < @column_count) 
BEGIN; 

SET @temp_col_val = (SELECT attrib FROM #temp1 WHERE seqno1 = @counter1; 

답변

0

은 다음 코드는 각 속성을 통해 루프가 인쇄됩니다 도와주세요 :

declare 
    curField varchar2(100); 
    resultCnt number ; 
begin  
    select count(distinct attribute) into resultCnt from look_up_table; 
    for ind in 1..resultCnt loop 
     select attribute into curField from (
      select attribute, rownum rwn 
      from 
      (
       select distinct attribute 
       from look_up_table 
      ) 
     ) where rwn = ind; 

     dbms_output.put_line (curField); 
    end loop; 
end; 
/
관련 문제