programing

미리 정의된 값을 반복합니다.

elseif 2023. 4. 29. 08:47

미리 정의된 값을 반복합니다.

오라클에서 "각각"을 수행할 수 있는 방법이 있습니까?

begin
  for VAR in {1,2,5}
  loop
    dbms_output.put_line('The value: '||VAR);
  end loop;
end;

다음과 같은 작업을 수행할 수 있습니다.

begin
  for VAR in 1..5
  loop
    if VAR in(1,3,5) then
      dbms_output.put_line('The value: '||VAR);
    end if;
  end loop;
end;

하지만 더 좋은 방법이 없을까요?일련의 가치를 정의하고 이를 통해 반복합니까?

감사해요.

원하는 만큼 매끄럽지는 않지만 다음과 같이 할 수 있습니다.

declare
  type nt_type is table of number;
  nt nt_type := nt_type (1, 3, 5);
begin
  for i in 1..nt.count loop
    dbms_output.put_line(nt(i));
  end loop;
end;

데이터베이스에 유형을 작성하는 경우:

create type number_table is table of number;

그러면 다음을 수행할 수 있습니다.

begin
  for r in (select column_value as var from table (number_table (1, 3, 5))) loop
    dbms_output.put_line(r.var);
  end loop;
end;

그리고 A.B.로서.Cade는 아래에서 다음과 같이 Oracle과 함께 제공되는 데이터베이스 유형을 언급했습니다.sys.dbms_debug_vc2coll:

begin
  for r in (select column_value as var from table (dbms_debug_vc2coll (1, 3, 5))) loop
    dbms_output.put_line(r.var);
  end loop;
end;

이것은 A.B.에서 온 것입니다.현재 받아들여지고 있는 답변에 대한 케이드의 언급이지만, 저는 그것이 훨씬 더 깨끗하고 더 주의를 기울일 가치가 있다고 생각합니다.

BEGIN
  FOR i IN (SELECT column_value FROM table(sys.dbms_debug_vc2coll(1, 3, 5))) LOOP
    dbms_output.put_line(i.column_value);
  END LOOP;
END;

언급URL : https://stackoverflow.com/questions/10798161/loop-through-pre-defined-values