2017-11-03 6 views
-1

데이터베이스에 두 개의 테이블이 있습니다. Table1의 조건을 확인하고 싶습니다. 그것이 사실이된다면 Table2에 두 개의 레코드를 생성해야합니다. OracleSQLDeveloper를 사용하고 있으며이를 위해 PL/SQL 스크립트를 작성하고 싶습니다. 내가 이것을 성취 할 수있는 가장 좋은 방법은 무엇입니까? 당신이 자습서를 가지고 PL/SQL의 기초를 배우려고 한PL/SQL 스크립트로 새 레코드 생성

답변

1

, 다음은 다음과 비슷한 모습이 될 것

declare 
    v_indicator varchar2(100) := ''; 
begin 
    -- read indicator from Table1 
    select some_colmn into v_indicator 
    from Table1 
    where some_criterium='some_value' 
    ; 
    -- check indicator value against some of your condition 
    if v_indicator = 'condition_matched' then 
    -- log information to server output 
    dbms_output.put_line('condition_matched'); 
    -- generate records: 
    insert into Table2 (col_t2) values ('r1'); 
    insert into Table2 (col_t2) values ('r2'); 
    else 
    -- here it means condition was not matched 
    dbms_output.put_line('condition NOT matched'); 
    end if; 
end; 

희망이로, PL/SQL에 그 일을

+0

안녕하세요! 당신의 도움을 주셔서 감사합니다. 하지만 table1 열의 값을 table2 열 중 일부에 삽입해야합니다. 나는 그것을 하드 코딩하고 싶지 않다. insert 문에서이를 수행하는 방법? –

1

하는 데 도움이 J. Chomel에 의해 제안 된, 읽기 쉽고 좋다. 그 어떤 이유로 필요한 경우하지만 당신은 너무 순수 SQL에서 작업을 수행 할 수 있습니다

insert into table2(col_t2) 
    select val from (select 'r1' val from 
        dual union all 
        select 'r2' from dual) 
    where exists (select 'x' from table1 
        where some_criterium='some_value') 

물론, 그런 일에는 매우 읽을 수 없습니다.