Dynamic 프로시저 도움 요청합니다.. 0 3 3,477

by ks9275 [PL/SQL] procedure 프로시져 dynamic dynamic procedure 동적 프로시져 다이나믹 프로시져 [2013.02.28 17:01:11]


CREATE OR REPLACE procedure SHKWON.load_procedure
(
    v_datetime IN aaa.DATETIME%TYPE,
    v_user_id IN aaa.USER_ID%TYPE,
    v_action IN aaa.ACTION%TYPE,
    v_user_ip IN aaa.USER_IP%TYPE,
    v_pageCount IN NUMBER,
    sel_cur IN OUT sys_refcursor
)
IS    
    
    vsQuery varchar(2000);
    
BEGIN
    vsQuery := 'select * from (
                    select
                        user_id, log_index, action,user_ip,
                        to_char(datetime, ''YYYY/MM/DD HH24:MI:SS'') as datetime,
                        message
                    from aaa';
                    
        vsQuery := 'where 1=1';
        if( v_datetime is not null ) then
            vsQuery := 'and datetime =  || v_datetime ||';
        end if;
        if( v_user_id is not null ) then
            vsQuery := 'and datetime =  || v_user_id ||';
        end if;
        if( v_action is not null ) then
            vsQuery := 'and action = || v_action ||';
        end if;
        if( v_user_ip is not null ) then
            vsQuery := 'and user_ip = || v_user_ip ||';
        end if;
    vsQuery := ' chr(13)) ';
    vsQuery := 'where
                log_index >= (select count(*) from aaa)-';
    vsQuery := v_pageCount;
    vsQuery := 'order by log_index desc;';
    dbms_output.put_line(vsQuery);
    open sel_cur for vsQuery;
    
COMMIT;
END load_procedure;
/


프로시저를 실행하면 

1행에 오류:
ORA-00900: SQL 문이 부적합합니다
ORA-06512: "SHKWON.LOG_LOAD_PROCEDURE", 줄 45에서
ORA-06512: 줄 1에서

라는 오류가 뜨는데 도저히 못잡겠네요ㅠ_ㅠ
by 마농 [2013.02.28 17:14:57]

우선 Select 이므로 Commit 은 빼시구요.
Open 하는 부분 주석처리하고
dbms_output.put_line(vsQuery); 여기까지만 실행해 보세요.
이때 출력되는 쿼리(vsQuery) 보시면 엉망이란걸 아실 거에요.

vsQuery 를 계속 엎어치는데 엎어치면 안되고 추가해야죠.
vsQuery := vsQuery || '추가조건';

조건절 주실때 문자열인 경우엔 앞뒤로 따옴표 챙겨서 붙여주시구요.
날짜 컬럼에 대한 조건은 신경 좀 스셔야 할것 같네요...


by 손님 [2013.02.28 17:53:32]

CREATE OR REPLACE procedure SHKWON.load_procedure
(
    v_datetime  IN varchar2, -- 매개변수에는 크기를 정하지않음
    v_user_id   IN varchar2,
    v_action    IN varchar2,
    v_user_ip   IN varchar2,
    v_pageCount IN NUMBER,
    sel_cur IN OUT sys_refcursor
)
IS   
    vsQuery varchar(2000);
    lf VARCHAR2(1) := chr(10);

BEGIN
    vsQuery := 'select * from ('
  || lf || '    select'
  || lf || '     user_id, log_index, action,user_ip,'
  || lf || '     to_char(datetime, ''YYYY/MM/DD HH24:MI:SS'') as datetime,'
  || lf || '     message'
  || lf || '    from aaa'
  || lf || '    where 1=1';
    if( v_datetime is not null ) then
    vsQuery := vsQuery || lf || '    and datetime = '
|| 'to_date(''' || v_datetime || ''',''YYYY/MM/DD HH24:MI:SS'')';
    end if;
    if( v_user_id is not null ) then
    vsQuery := vsQuery || lf || '    and user_id = ''' || v_user_id || '''';
    end if;
    if( v_action is not null ) then
    vsQuery := vsQuery || lf || '    and action = ''' || v_action || '''';
    end if;
    if( v_user_ip is not null ) then
    vsQuery := vsQuery || lf || '    and user_ip = ''' || v_user_ip ||'''';
    end if;
    vsQuery := vsQuery || lf || '    )';
    vsQuery := vsQuery || lf || 'where log_index >= (select count(*) from aaa)-'
||  v_pageCount;
    vsQuery := vsQuery || lf || 'order by log_index desc;';

    dbms_output.put_line(vsQuery);

    open sel_cur for vsQuery;
   
END load_procedure;
/


 


by 손님 [2013.02.28 18:11:38]
    vsQuery := vsQuery || lf || 'order by log_index desc;'; 에서 ';'빼세요
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입