DYNAMIC PL/SQL BLOCK

Hello,
My requirement is to create procedures on the fly and call the created procedures. The pl/sql code for the procedures are stored in the tables. I was able to create the procedure on the fly. But it ended being an invalid object. I got this error
ORA-24344: success with compilation error
Following is my code snippet
DECLARE
proc_str VARCHAR2(20000) := NULL;
v_err_code VARCHAR2 (256) := NULL;
v_err_msg VARCHAR2 (256) := NULL;
BEGIN
FOR cur_pi_proc IN (SELECT sql_query, procedure_name
FROM mas_pi_rules
WHERE ID = 7)
LOOP
proc_str := cur_pi_proc.sql_query ;
EXECUTE IMMEDIATE ''|| proc_str || '';
EXECUTE IMMEDIATE 'ALTER PROCEDURE '|| cur_pi_proc.procedure_name ||' COMPILE';
EXECUTE IMMEDIATE 'CALL "'
|| cur_pi_proc.procedure_name
|| '" ( :p_mode)'
USING 'BATCH';
COMMIT;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
v_err_msg := SUBSTR (SQLCODE || ':-' || SQLERRM, 1, 200);
INSERT INTO stored_proc_err
(error_origin, error_msg, error_date
VALUES ('test_dynproc', v_err_msg, SYSDATE
COMMIT;
END;
Any help in creating procedures dynamically is greatly appreciated.
regards,
Ravi
Message was edited by:
user611440

-- EXECUTE IMMEDIATE '' || proc_str || ''; -- Comment ThisUmm, you've missed the whole and entire point of this thread, which is that the procedure to be run is created dynamically from the source in the table.
The OP's problem (well, their specific problem rather than the more philosophical one) is the way they were running the create statement. Fix that and everything else falls into place...
SQL> create table MAS_PI_RULES ( SQL_QUERY varchar2(4000), PROCEDURE_NAME varchar2(30), ID number)
  2  /
Table created.
SQL>
SQL> insert into MAS_PI_RULES
  2  values ('CREATE OR REPLACE PROCEDURE REVERSE_STRING (I_STRING IN OUT VARCHAR2) IS L_STRING VARC
HAR2 (400); BEGIN SELECT REVERSE (I_STRING) INTO I_STRING FROM DUAL; END;', 'REVERSE_STRING', 7)
  3  /
1 row created.
SQL>
SQL> DECLARE
  2   proc_str VARCHAR2 (20000) := NULL;
  3   v_err_code VARCHAR2 (256) := NULL;
  4   v_err_msg VARCHAR2 (256) := NULL;
  5   v_ouput VARCHAR2 (256) := 'ZLLIKS DAM SAH CPA';
  6  BEGIN
  7   FOR cur_pi_proc IN (SELECT sql_query
  8     ,procedure_name
  9    FROM mas_pi_rules
10   WHERE ID = 7) LOOP
11  proc_str := cur_pi_proc.sql_query;
12 
13  EXECUTE IMMEDIATE  proc_str ;
14  EXECUTE IMMEDIATE 'ALTER PROCEDURE ' || cur_pi_proc.procedure_name || ' COMPILE';
15 
16  EXECUTE IMMEDIATE 'begin ' || cur_pi_proc.procedure_name || '(:p_mode); end;'
17  USING IN OUT v_ouput;
18 
19  COMMIT;
20  END LOOP;
21 
22  DBMS_OUTPUT.put_line ('The output is ' || v_ouput);
23  EXCEPTION
24  WHEN OTHERS THEN
25  v_err_msg := SUBSTR (SQLCODE || ':-' || SQLERRM, 1, 200);
26  DBMS_OUTPUT.put_line (v_err_msg);
27  END;
28  /
The output is APC HAS MAD SKILLZ
PL/SQL procedure successfully completed.
SQL> Cheers, APC
Blog : http://radiofreetooting.blogspot.com/

Similar Messages

  • Dynamic PL/SQL block vs dynamic SQL SELECT

    Hi there,
    I have a question regarding the optimal way to code a dynamic SELECT INTO statement. Below are the 2 posiibilities I know of:
    _1. Dynamically executing the SELECT statement and making use of the INTO clause of the EXECUTE IMMEDIATE statement_
    CREATE OR REPLACE FUNCTION get_num_of_employees (p_loc VARCHAR2, p_job VARCHAR2)
    RETURN NUMBER
    IS
    v_query_str VARCHAR2(1000);
    v_num_of_employees NUMBER;
    BEGIN
    v_query_str := 'SELECT COUNT(*) FROM emp_'
    || p_loc
    || ' WHERE job = :bind_job';
    EXECUTE IMMEDIATE v_query_str
    INTO v_num_of_employees
    USING p_job;
    RETURN v_num_of_employees;
    END;
    _2. Encapsulating the SELECT INTO statement in a block and dynamically exectuting the block_
    CREATE OR REPLACE FUNCTION get_num_of_employees (p_loc VARCHAR2, p_job VARCHAR2)
    RETURN NUMBER
    IS
    v_query_str VARCHAR2(1000);
    v_num_of_employees NUMBER;
    BEGIN
    v_query_str := 'begin
    SELECT COUNT(*) INTO :into_bind FROM emp_'
    || p_loc
    || ' WHERE job = :bind_job;
    end;';
    EXECUTE IMMEDIATE v_query_str
    USING out v_num_of_employees, p_job;
    RETURN v_num_of_employees;
    END;
    I was just wondering which way would be preferred? I know the second method uses a bind variable for the INTO clause, but does the first one also use bind varialbes (no semi-colon)? Any differences in terms of efficiency or speed?
    Thanks alot
    Edited by: BYS2 on Oct 19, 2011 1:23 AM

    sybrand_b wrote:
    No difference in terms of performance or speed
    Both variants will wreck the primary purpose of PL/SQL: to avoid parsing.
    When I would see a 'developer' do this, I would fire him on the spot.
    Why abuse PL/SQL in such a fashion? Both statements don't require parsing, as there is nothing dynamic in them and indicate a complete lack of understanding of Oracle, or a desire to deliver completely unscalable applications, resulting in end-users desiring to lynch you, and rightly so.
    Remove the dynamic SQL or find another job.
    Sybrand Bakker
    Senior Oracle DBANot dynamic? What if p_loc and p_job were generated dynamically based on user-input? or what if there were potentially thousands of tables that p_loc could refer to? Should I make a CASE statement with a thousand cases?
    In addition, the first example was actually taken directly from the official Oracle Database Application Developer's Guide (version 10.2). http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_dynamic_sql.htm#i1006429 - look under 'Sample Single-Row Query Using Native Dynamic SQL' heading. Therefore, if you have any issues with this alleged 'improper' usage of dynamic SQL, perhaps you should go talk to Oracle directly.
    While I appreciate your response, I don't think it has occurred that you that not everyone is a 'developer'. In fact, I have only very recently (several days ago) taught myself how to use Oracle SQL, PL/SQL and XMLDB by reading several of the official Oracle language and developer's guides. It is more a passing interest to me as I am working on some medical research which may require the use of a database. I am actually in medical school at the moment but have an undergraduate degree in Electrical and Computer engineering so I am generally well-versed in programming.
    Perhaps the next time, you post your rubbish, rude and unhelpful comments, you should stop and think that people come to this forum because they need help and not because they want to be told to 'find another job'. In fact, I am quite certain that I could make you look absolutely stupid in any topic of electrical engineering or medicine.
    Please do us all a favour and stop polluting this forum with your vapid posts. While I understand that your behavior is likely a compensatory mechanism to cope with your inferiority complex, know that help IS available if you need it.
    Edited by: BYS2 on Oct 19, 2011 2:13 AM

  • Dynamic pl/sql in pro*c/c++

    I have a stored procedure to query and will receive a result by it....
    I used the dbms_sql package to give name of the table at run-time
    then it runs in sql*plus well...
    but If i called it in pro*c/c++, it would not return result...
    This is my examples..
    (1) stored procedure
    CREATE OR REPLACE PACKAGE XDMS_STORE_PKG
    IS
    PROCEDURE isExistInstance(table_name IN VARCHAR2, url IN VARCHAR2, num OUT N
    UMBER);
    END XDMS_STORE_PKG;
    CREATE OR REPLACE PACKAGE BODY XDMS_STORE_PKG IS
    PROCEDURE isExistInstance (table_name IN VARCHAR2, url IN VARCHAR2, num OUT NUMBER)
    IS
    cursor1 integer;
    rows_processed integer;
    tmp NUMBER;
    tmp_char VARCHAR2(10);
    BEGIN
    cursor1 := dbms_sql.open_cursor;
    dbms_sql.parse (cursor1, 'SELECT count(*) FROM ' | | table_name | | ' WHERE DocURL = ' | | ':x', dbms_sql.v7);
    dbms_sql.bind_variable(cursor1, 'x', url);
    dbms_sql.define_column (cursor1, 1, tmp);
    rows_processed := dbms_sql.execute (cursor1);
    loop
    if dbms_sql.fetch_rows (cursor1) > 0 then
    dbms_sql.column_value (cursor1, 1, tmp);
    num := tmp;
    else
    exit;
    end if;
    end loop;
    dbms_sql.close_cursor (cursor1);
    EXCEPTION
    WHEN OTHERS THEN
    dbms_output.put_line(sqlerrm);
    if dbms_sql.is_open (cursor1) then
    dbms_sql.close_cursor (cursor1);
    end if;
    END isExistInstance;
    END XDMS_STORE_PKG;
    (2) In the sqlplus
    set serveroutput on;
    DECLARE
    cnt NUMBER;
    dtdid VARCHAR2(10) := 'DT_AAAAC';
    url VARCHAR2(200) := 'http://www.ce.cnu.ac.kr/~xdms/data/personnel.xml';
    BEGIN
    XDMS_STORE_PKG.isExistInstance(dtdid, url, cnt);
    dbms_output.put_line(to_char(cnt));
    END;
    (3) In the pro*c/c++
    bool StorageManager::isExistInstance(char *this_url)
    EXEC SQL BEGIN DECLARE SECTION;
    int count = 0;
    char dtdid[6];
    char url[200];
    EXEC SQL END DECLARE SECTION;
    if(isNewDtd) return false;
    sprintf(dtdid, "DT_%s", this_dtdid);
    strcpy(url, this_url);
    EXEC SQL EXECUTE
    BEGIN
    XDMS_STORE_PKG.isExistInstance(:dtdid, :url, :count);
    END;
    END-EXEC;
    if(count < 1) return false;
    else return true;
    thanks...
    null

    This is tough it seems. However here are a few thoughts if they can help:
    - Generate(build) a dynamic pl/sql block like :
    Begin
    Your Procedure Name
    End;
    keep building this piece of code for each one of your procedures in C or C++. And then execute it as any PL/sql code in the form of a 'constructed' string. Pro*C also has DEscribe Using BInd, but i do not know if that can help.

  • Bind Variable in SELECT statement and get the value  in PL/SQL block

    Hi All,
    I would like  pass bind variable in SELECT statement and get the value of the column in Dynamic SQL
    Please seee below
    I want to get the below value
    Expected result:
    select  distinct empno ,pr.dept   from emp pr, dept ps where   ps.dept like '%IT'  and pr.empno =100
    100, HR
    select  distinct ename ,pr.dept   from emp pr, dept ps where   ps.dept like '%IT'  and pr.empno =100
    TEST, HR
    select  distinct loc ,pr.dept   from emp pr, dept ps where   ps.dept like '%IT'  and pr.empno =100
    NYC, HR
    Using the below block I am getting column names only not the value of the column. I need to pass that value(TEST,NYC..) into l_col_val variable
    Please suggest
    ----- TABLE LIST
    CREATE TABLE EMP(
    EMPNO NUMBER,
    ENAME VARCHAR2(255),
    DEPT VARCHAR2(255),
    LOC    VARCHAR2(255)
    INSERT INTO EMP (EMPNO,ENAME,DEPT,LOC) VALUES (100,'TEST','HR','NYC');
    INSERT INTO EMP (EMPNO,ENAME,DEPT,LOC) VALUES (200,'TEST1','IT','NYC');
    INSERT INTO EMP (EMPNO,ENAME,DEPT,LOC) VALUES (300,'TEST2','MR','NYC');
    INSERT INTO EMP (EMPNO,ENAME,DEPT,LOC) VALUES (400,'TEST3','HR','DTR');
    INSERT INTO EMP (EMPNO,ENAME,DEPT,LOC) VALUES (500,'TEST4','HR','DAL');
    INSERT INTO EMP (EMPNO,ENAME,DEPT,LOC) VALUES (600,'TEST5','IT','ATL');
    INSERT INTO EMP (EMPNO,ENAME,DEPT,LOC) VALUES (700,'TEST6','IT','BOS');
    INSERT INTO EMP (EMPNO,ENAME,DEPT,LOC) VALUES (800,'TEST7','HR','NYC');
    COMMIT;
    CREATE TABLE COLUMNAMES(
    COLUMNAME VARCHAR2(255)
    INSERT INTO COLUMNAMES(COLUMNAME) VALUES ('EMPNO');
    INSERT INTO COLUMNAMES(COLUMNAME) VALUES ('ENAME');
    INSERT INTO COLUMNAMES(COLUMNAME) VALUES ('DEPT');
    INSERT INTO COLUMNAMES(COLUMNAME) VALUES ('LOC');
    COMMIT;
    CREATE TABLE DEPT(
    DEPT VARCHAR2(255),
    DNAME VARCHAR2(255)
    INSERT INTO DEPT(DEPT,DNAME) VALUES ('IT','INFORMATION TECH');
    INSERT INTO DEPT(DEPT,DNAME) VALUES ('HR','HUMAN RESOURCE');
    INSERT INTO DEPT(DEPT,DNAME) VALUES ('MR','MARKETING');
    INSERT INTO DEPT(DEPT,DNAME) VALUES ('IT','INFORMATION TECH');
    COMMIT;
    PL/SQL BLOCK
    DECLARE
      TYPE EMPCurTyp  IS REF CURSOR;
      v_EMP_cursor    EMPCurTyp;
      l_col_val           EMP.ENAME%type;
      l_ENAME_val       EMP.ENAME%type;
    l_col_ddl varchar2(4000);
    l_col_name varchar2(60);
    l_tab_name varchar2(60);
    l_empno number ;
    b_l_col_name VARCHAR2(255);
    b_l_empno NUMBER;
    begin
    for rec00 in (
    select EMPNO aa from  EMP
    loop
    l_empno := rec00.aa;
    for rec in (select COLUMNAME as column_name  from  columnames
    loop
    l_col_name := rec.column_name;
    begin
      l_col_val :=null;
       l_col_ddl := 'select  distinct :b_l_col_name ,pr.dept ' ||'  from emp pr, dept ps where   ps.dept like ''%IT'' '||' and pr.empno =:b_l_empno';
       dbms_output.put_line('DDL ...'||l_col_ddl);
       OPEN v_EMP_cursor FOR l_col_ddl USING l_col_name, l_empno;
    LOOP
        l_col_val :=null;
        FETCH v_EMP_cursor INTO l_col_val,l_ename_val;
        EXIT WHEN v_EMP_cursor%NOTFOUND;
          dbms_output.put_line('l_col_name='||l_col_name ||'  empno ='||l_empno);
       END LOOP;
    CLOSE v_EMP_cursor;
    END;
    END LOOP;
    END LOOP;
    END;

    user1758353 wrote:
    Thanks Billy, Would you be able to suggest any other faster method to load the data into table. Thanks,
    As Mark responded - it all depends on the actual data to load, structure and source/origin. On my busiest database, I am loading on average 30,000 rows every second from data in external files.
    However, the data structures are just that - structured. Logical.
    Having a data structure with 100's of fields (columns in a SQL table), raise all kinds of questions about how sane that structure is, and what impact it will have on a physical data model implementation.
    There is a gross misunderstanding by many when it comes to performance and scalability. The prime factor that determines performance is not how well you code, what tools/language you use, the h/w your c ode runs on, or anything like that. The prime factor that determines perform is the design of the data model - as it determines the complexity/ease to use the data model, and the amount of I/O (the slowest of all db operations) needed to effectively use the data model.

  • Can't create a sequence within a pl/sql block with execute immediate.

    Hi All. I created a user and granted it the 'create sequence' privilege though a role. In a pl/sql block I try to create a sequence using 'execute immediate' but get a 1031-insufficient privileges error. If I grant create sequence directly to the user, the pl/sql block completes successfully. Can anyone explain this behavior? We're running 11.2 Enterprise Editon.
    Thanks,
    Mark

    In a definer's rights stored procedure (the default), you only have access to privileges that have been granted directly, not via a role.
    There are two basic reasons for that. First, roles can be enabled or disabled, default and non-default, password-protected, etc. so the set of roles a particular user actually has is session-specific. Oracle needs to know at compile time what privileges the owner of the procedure has. The only way to do that (without deferring the privilege check) is to ignore privileges granted through roles.
    Second, since 99% of privilege management DBAs do involves granting and revoking roles, it's helpful that changing role privileges will never cause objects to be marked invalid and recompiled which can have side-effects on applications. DBAs only need to worry about causing problems on those rare cases where they are granting or revoking direct privileges to users.
    You can create an invoker's rights stored procedure by adding the clause (AUTHID CURRENT_USER). That defer's the security check to run-time but allows the procedure to see privileges granted through roles in the current session. But that means that the caller of the procedure would need to have the CREATE SEQUENCE privilege through the role, not the owner of the procedure.
    And just to make the point, dynamic object creation in PL/SQL is almost always a red flag that there is something problematic in your design. If you are creating sequences dynamically, that means that you'd have to refer to them dynamically throughout your code which means that your inserts would need to use dynamic SQL. That's not a particularly easy or safe way to develop code.
    Justin

  • PL/SQL block returning error

    Hi All,
    I have used PL/SQL block in my custom report to search table heading. Basically I have two select list (LOV), user select the parameter from the list and click on go, it displays the report fine when record exists.
    I am using following PL/SQL block -
    >
    DECLARE
    v_sql VARCHAR2(4000);
    BEGIN
    IF :P126_PARK_NAME IS NOT NULL THEN
    v_sql := 'select P.OLIC_KEY1, PA.NAME, C.TRADING_NAME, L.LIC_TYPE, P.'|| :P126_PARK_NAME ||', C.COP_ID, L.OLIC_ID Olic_id '
    ||' from RTT_OP_PARKS_TEMP P, RTT_COMMERCIAL_OPERATORS C , RTT_PARTIES PA, RTT_OP_LICENCES L'
    ||' where '|| :P126_PARK_NAME || ' IS NOT NULL '
    ||' AND PA.PAR_ID =C.PAR_ID '
    ||' AND P.COP_ID =C.COP_ID '
    ||' AND L.OLIC_KEY1=P.OLIC_KEY1 '
    ||' AND :P126_QUERY = ''Y'''
    ||' ORDER BY PA.NAME ';
    END IF;
    /* debugging */
    IF :DEBUG = 'YES' THEN
    htp.preopen;
    htp.p(v_sql);
    htp.preclose;
    END IF;
    RETURN (v_sql);
    END;
    >
    Now, I want to use like condition to display all the records by checking the parameters in text field. I have created a new text field and modified the query as follows but it does not work -
    >
    DECLARE
    v_sql VARCHAR2(4000);
    BEGIN
    IF :P126_PARK_NAME IS NOT NULL THEN
    v_sql := 'select P.OLIC_KEY1, PA.NAME, C.TRADING_NAME, L.LIC_TYPE, P.'|| :P126_PARK_NAME ||', C.COP_ID, L.OLIC_ID Olic_id '
    ||' from RTT_OP_PARKS_TEMP P, RTT_COMMERCIAL_OPERATORS C , RTT_PARTIES PA, RTT_OP_LICENCES L'
    ||' where '|| :P126_PARK_NAME || ' IS NOT NULL '
    ||' AND PA.PAR_ID =C.PAR_ID '
    ||' AND P.COP_ID =C.COP_ID '
    ||' AND L.OLIC_KEY1=P.OLIC_KEY1 '
    ||' AND :P126_QUERY = ''Y'''
    ||' ORDER BY PA.NAME ';
    END IF;
    IF :P126_MARINE_PARK != -1 THEN
    v_sql := 'select distinct L.OLIC_KEY1, PA.NAME, C.TRADING_NAME, L.LIC_TYPE, M.'|| :P126_MARINE_PARK ||', C.COP_ID '
    ||' from RTT_OP_MARINE_PARKS M, RTT_COMMERCIAL_OPERATORS C , RTT_PARTIES PA, RTT_OP_LICENCES L'
    ||' where '|| :P126_MARINE_PARK || ' IS NOT NULL '
    ||' M.'||:P126_MARINE_PARK||' IS LIKE :P126_MARINE_PARK||'%' '
    ||' AND PA.PAR_ID =C.PAR_ID '
    ||' AND M.COP_ID =C.COP_ID '
    ||' AND L.OLIC_KEY1=M.OLIC_KEY1 '
    ||' AND :P126_QUERY = ''Y'''
    ||' ORDER BY PA.NAME ';
    END IF;
    /* debugging */
    IF :DEBUG = 'YES' THEN
    htp.preopen;
    htp.p(v_sql);
    htp.preclose;
    END IF;
    RETURN (v_sql);
    END;
    >
    Can anybody tell me what I am doing wrong here?
    Thanks in advance
    Regards,
    M Tajuddin

    Hi Andy,
    Thanks for pointing the quotation however it does not work either. Now I am getting -
    >
    failed to parse SQL query:
    ORA-00933: SQL command not properly ended
    >
    Now my query is like below -
    >
    DECLARE
    v_sql VARCHAR2(4000);
    BEGIN
    IF :P126_ZONE_NAME IS NOT NULL THEN
    v_sql := 'select distinct L.OLIC_KEY1, PA.NAME, C.TRADING_NAME, L.LIC_TYPE, M.'|| :P126_ZONE_NAME ||', C.COP_ID '
    ||' from RTT_OP_MARINE_PARKS M, RTT_COMMERCIAL_OPERATORS C , RTT_PARTIES PA, RTT_OP_LICENCES L'
    ||' where '|| :P126_ZONE_NAME || ' IS NOT NULL '
    ||' AND PA.PAR_ID =C.PAR_ID '
    ||' AND M.COP_ID =C.COP_ID '
    ||' AND L.OLIC_KEY1=M.OLIC_KEY1 '
    ||' AND :P126_QUERY = ''Y'''
    ||' ORDER BY PA.NAME ';
    END IF;
    IF :P126_MARINE_PARK IS NOT NULL THEN
    v_sql := 'select distinct L.OLIC_KEY1, PA.NAME, C.TRADING_NAME, L.LIC_TYPE, M.'|| :P126_MARINE_PARK ||', C.COP_ID '
    ||' from RTT_OP_MARINE_PARKS M, RTT_COMMERCIAL_OPERATORS C , RTT_PARTIES PA, RTT_OP_LICENCES L'
    ||' where ' || :P126_MARINE_PARK || ' IS NOT NULL '
    *||' M.' || :P126_MARINE_PARK || ' IS LIKE ''' || :P126_MARINE_PARK || '%'' '*
    ||' AND PA.PAR_ID =C.PAR_ID '
    ||' AND M.COP_ID =C.COP_ID '
    ||' AND L.OLIC_KEY1=M.OLIC_KEY1 '
    ||' AND :P126_QUERY = ''Y'''
    ||' ORDER BY PA.NAME ';
    END IF;
    /* debugging */
    IF :DEBUG = 'YES' THEN
    htp.preopen;
    htp.p(v_sql);
    htp.preclose;
    END IF;
    RETURN (v_sql);
    END;
    >
    Can you tell me how can I display dynamic multiple column in my query? Say in the LIKE condition if user type M in the parameter field and click on go, it must return more than one column starting column name M but in my current query it will only display one column, am I right?
    Thanks again for your suggestion.
    Kind regards,
    M Tajuddin
    http://tajuddin.whitepagesbd.com

  • Pl/sql block on block trigger

    I have a pl/sql block in when-new-block-instance trigger on forms 10g.
    Basically, these trigger fires after user selection of the search criteria block.
    Exist one condition which is totaly important:
    One of the search criteria is a list of document type. So the user, can check one, all or none of the doc types.
    See this pictures:
    doc type options: TRRLS, ADS, SIQD
    If the user check only the TRRLS, the next block will display only the record match the doc type selected. If the user check none doc type, all the record will be displayed.
    For those purpose, in the trigger I talk before (when new block instance) in the results block, I use two cursors, one for all the record (if the user check none doc type). And the other cursor for the records with specific doc type.
    The first one works just fine, but the second one, do nothing. Part of the where in the cursor is the following:
    where ........... and
    dim.doc:typ IN (:global.doc_typ_where_clasue) and
    The value of the :global.doc_typ_where:clause is the list of all the doc type selected by the user. In example, if the user check TRRLS and ADS, the global variable will have 'TRRLS', 'ADS'.
    But it doesn't work..... I don't know why....
    Any idea to pass a list of doc to a cursor and use in a where clause dynamically.
    thanks a lot,
    abdel.

    If you search for "dynamic in list" on http://asktom.oracle.com/ you'll find a solution to exactly the same problem you're facing. The solution is:
    1. Strip the string into an array
    2. Use the array in the where clause.
    But maybe you can also create your own where clause and use that one by setting set_block_property( <block>, onetime_where, <where clause>)
    HTH
    Roel

  • Exception in declarative section to propagating in PL/SQL Block

    Hi All, I have a requirement to send emails to some receipient whenever there is an error in a process. The is working untill the put a wrong database link in the parameter, the cursor is in declarative statement, hence when other exception does not work, create and enclosing block around the initial block so that the exception can propagate to the enclosing block, but this does not work either, please advice. Below is the a brief pseudo code. The bold is the initial block. Please advice.
    DECLARE
    Invalid_table EXCEPTION;
    PRAGMA EXCEPTION_INIT(Invalid_table, -00942);
    BEGIN
    declare
    c_test number;
    cursor c1 is select 1 from tt@sro4link1 --- wrong databaselink sent as a parameter
    where 1 =1;
    */*Because the error is in the cursor select, no email is been sent, that was the reason I put an enclosing block, but the exception is not propagate either */*
    BEGIN
    OPEN C1;
    FETCH C1 into c_test;
    CLOSE C1;
    EXCEPTION
    WHEN OTHERS THEN
    --- Send email
    END; EXCEPTION
    WHEN Invalid_table THEN
    --- send email
    END;
    /

    Ade2 wrote:
    it is not a dynamic sql. Your description is not very clear about what is code and what is pseudo code, but if you are using substitution variables in sqlplus, then that is dynamic SQL.
    sqlplus scans for substitution variables, prompts when needed, replaces the variable (substitutes) with the text input and passes the entire result to the database for validation.
    If the substitution text input results in an invalid PL/SQL block then a compilation error is returned from the database. The PL/SQL cannot be compiled, it never runs, no run time exceptions are possible.
    So you cannot use exceptions to detect errors in values input to substitution variables.
    SQL> declare
      2    l_dummy number;
      3  begin
      4    select 1 into l_dummy from dual;
      5  end;
      6  /
    PL/SQL procedure successfully completed.
    SQL> declare
      2    l_dummy number;
      3  begin
      4    &5
      5  end;
      6  /
    Enter value for 5: select 1 into l_dummy from dual;
    old   4:   &5
    new   4:   select 1 into l_dummy from dual;
    PL/SQL procedure successfully completed.
    SQL> /
    Enter value for 5: this will not compile
    old   4:   &5
    new   4:   this will not compile
      this will not compile
    ERROR at line 4:
    ORA-06550: line 4, column 8:
    PLS-00103: Encountered the symbol "WILL" when expecting one of the following:
    := . ( @ % ;

  • Export data into Excel from PL/SQL Block

    Hi,
    Please tell me how to export data into excel comming out from PL/SQL code in APEX.
    We can simply export data into excel if we have Report region, but my query is dynamic for which i had to use PL/SQL block. Now i want to export that data into excel.
    Thanks & Regards,
    Smith

    Hi,
    Take a look here http://spendolini.blogspot.com/2006/04/custom-export-to-csv.html
    Regards
    Paul

  • Alter database statement in anonymous pl/sql block

    Is it possible to include an alter database statement in an anonymous pl/sql block?
    When I execute this code to query user_tables for all table names, disable their constraints and drop the table, I got the following error:
    ***MY CODE
    -- DECLARE VARIABLE(S)
    DECLARE
         v_TABLE_NAME TABLE_NAME.USER_TABLE%TYPE;
    -- DECLARE AND DEFINE CURSOR
    CURSOR c_GETTABLES is
         SELECT TABLE_NAME from USER_TABLES;
    BEGIN
    OPEN c_GETTABLES;
    LOOP
    FETCH c_GETTABLES into v_TABLE_NAME;
    EXIT when c_GETTABLES%notfound;     
    ALTER TABLE v_TABLE_NAME DISABLE PRIMARY KEY CASCADE;
    DROP TABLE v_TABLE_NAME;
    END LOOP;
    CLOSE c_GETTABLES;
    END;
    ***RESPONSE FROM SERVER
    ALTER TABLE v_TABLE_NAME DISABLE PRIMARY KEY CASCADE;
    ERROR at line 15:
    ORA-06550: line 15, column 1:
    PLS-00103: Encountered the symbol "ALTER" when expecting one of the following:
    begin case declare exit for goto if loop mod null pragma
    raise return select update while with <an identifier>
    <a double-quoted delimited-identifier> <a bind variable> <<
    close current delete fetch lock insert open rollback
    savepoint set sql execute commit forall merge
    <a single-quoted SQL string> pipe
    Thanks

    When you want to perform ddl statements in a (anonymous) PL/SQL block, you have to use dynamic SQL because ddl is not possible in pl/sql.
    Dynamic sql means that you sort of execute ddl statements in a sql manner. To use dynamic sql, two options exist:
    - dbms_sql package : for oracle before 8i. To use this package is not always easy. Read about it carefully first before using.
    - Native Dynamic SQL : implemented in 8i and very easy to use. An example would be :
    declare
    lv_statement varchar2(32676);
    begin
    lv_statement := 'ALTER TABLE MY_TABLE DISABLE CONSTRAINT MY_TABLE_CK1';
    execute immediate lv_statement;
    lv_statement := 'ALTER TABLE MY_TABLE ENABLE CONSTRAINT MY_TABLE_CK1';
    execute immediate lv_statement;
    end;
    Good luck.
    Edwin van Hattem

  • Cursor query retrieves records in anon pl/sql block but no data in store pr

    Hello,
    Can any one please help me,
    I am using the below query, to get the table name and constraint name
    select table_name,constraint_name
    from all_constraints
    where constraint_type in ('R','P')
    and status = 'ENABLED'
    and owner='IRIS_DATA'
    order by constraint_type desc;
    The below query retrieves data in anonymous pl/sql block and retrieve no data when i use the same cursor with the same query in procedure inside the package.
    CREATE USER CLONEDEV
    IDENTIFIED BY CLONE123
    DEFAULT TABLESPACE IRIS
    TEMPORARY TABLESPACE TEMP;
    GRANT DBA TO CLONEDEV;
    the user from which i am executing this query is granted dba role.
    My oracle version is 10.2.0.4
    Please update if you any other information.
    Please advice.
    Thanks

    >
    Roles cannot be used by pl/sql.
    >
    NOT TRUE!
    That is an oft quoted myth. There are many posts in the forum that mis-state this.
    Roles can be, and are used by PL/SQL. In fact, OP stated and demonstrated that in their question: their anonymous block worked.
    The Oracle docs provide a very clear explanation - the core restriction is for NAMED PL/SQL blocks that use DEFINER's rights (the default):
    >
    Roles Used in Named Blocks with Definer's Rights
    All roles are disabled in any named PL/SQL block (stored procedure, function, or trigger) that executes with definer's rights. Roles are not used for privilege checking and you cannot set roles within a definer's rights procedure.
    >
    The Database Security Guide has the information in the section 'How Roles Work in PL/SQL Blocks (which, of course, wouldln't be needed if they didn't work. ;) )
    http://docs.oracle.com/cd/B28359_01/network.111/b28531/authorization.htm#i1007304
    >
    The use of roles in a PL/SQL block depends on whether it is an anonymous block or a named block (stored procedure, function, or trigger), and whether it executes with definer's rights or invoker's rights.
    Roles Used in Named Blocks with Definer's Rights
    All roles are disabled in any named PL/SQL block (stored procedure, function, or trigger) that executes with definer's rights. Roles are not used for privilege checking and you cannot set roles within a definer's rights procedure.
    The SESSION_ROLES view shows all roles that are currently enabled. If a named PL/SQL block that executes with definer's rights queries SESSION_ROLES, then the query does not return any rows.
    See Also:
    Oracle Database Reference
    Roles Used in Named Blocks with Invoker's Rights and Anonymous PL/SQL Blocks
    Named PL/SQL blocks that execute with invoker's rights and anonymous PL/SQL blocks are executed based on privileges granted through enabled roles. Current roles are used for privilege checking within an invoker's rights PL/SQL block. You can use dynamic SQL to set a role in the session.

  • JDBC: send batch of SQL commands as anonymous PL/SQL block

    Hi All,
    I did a little measurement to see if I can improve jdbc
    applications by batching dissimilar SQL commands into one
    anonymous PL/SQL block and execute it once. To my surprise, for
    a batch of 5 SQL commands, it take 60% more time than execute
    each of the 5 SQL commands separately.
    The same JDBC code, using similar SQL text batching, running
    against Sybase or MSSQL shows 60%-300% improvement.
    Is there any other way to batch dynamic dissimilar SQL commands
    in Oracle other than using anonymous PL/SQL block? Here is an
    example of how the "text-batching" PL/SQL block looks like:
    "begin insert into testtab1(col1, col2) values(1, 'row1');
    insert into testtab2(col1, col2) values(100, 1);....; end;"
    Thanks,
    Nam Nguyen
    null

    If you do:
    declare
    l_sql varchar2(32767);
    l_value varchar2(32767);
    begin
    select query_sql into l_sql from lov where lov_id = 100;
    dbms_output.put_line(l_sql);
    end;
    You'll see something like that:
    SELECT
    l.DESCRIPTION || decode(l2.DESCRIPTION,null,'',l2.description, '-' || l2.description) || decode(a.DT,'Y',' - Distributed Training','N',null,null) as value1
    FROM ACTIVITY a
    ,MOUNTAINEERING m
    ,LOV l
    ,LOV l2
    WHERE a.JSATFA_ID = 82
    AND a.SPECIFIC_ACTIVITY_LOV_ID = l.LOV_ID
    AND m.ACTIVITY_ID(+) = a.ACTIVITY_ID
    AND m.CLASSIFICATION_LOV_ID = l2.LOV_ID(+);
    you need to duplicate the '
    you can do many things like:
    CTH@> select * from sqls;
    C
    select first_name || ' ' || last_name as value1 from employees where rownum=1
    1 fila seleccionada.
    CTH@>
    CTH@> ;
    1 declare
    2 l_sql varchar2(32767);
    3 l_value varchar2(32767);
    4 type generic_cursor is ref cursor;
    5
    6 c generic_cursor;
    7
    8 begin
    9 select replace(c, ''', ''''') into l_sql from sqls;
    10
    11 execute immediate l_sql into l_value;
    12 dbms_output.put_line(l_value);
    13* end;
    CTH@> /
    Ellen Abel
    Procedimiento PL/SQL terminado correctamente.
    CTH@>

  • What is wrong with this PL/SQL block

    declare
    v_count integer := 0;
    begin
    -- execute immediate 'select count(1) into v_count from ' || 'abc' || '.table1';
    execute immediate 'select count(1) into v_count from abc.table1';
    dbms_output.put_line('v_count: ' || v_count);
    end;
    When i execute the above pl/sql block, I get the following error:
    ORA-00905: missing keyword
    ORA-06512: at line 6
    What am I doing wrong?
    When I use SELECT COUNT(1) INTO v_count FROM abc.table1; ( instead of the dynamic sql ), it works.
    I realize I don't need to use execute immediate for SELECT, but I need to do the dynamic SQL because the table1 will be under multiple schemas.
    Would appreciate an explanation for the above error, and/or alternate solution(s) for dynamic sql.
    Thanks

    DECLARE
      v_count INTEGER := 0;
    BEGIN
      -- execute immediate 'select count(1) into v_count from ' || 'abc' || '.table1';
      EXECUTE IMMEDIATE 'select count(1)  from abc.table1' into v_count;
      dbms_output.put_line('v_count: ' || v_count);
    END;TEST
    DECLARE
      v_count INTEGER := 0;
    BEGIN
      -- execute immediate 'select count(1) into v_count from ' || 'abc' || '.table1';
      EXECUTE IMMEDIATE 'select count(1) from dual ' into v_count;
      dbms_output.put_line('v_count: ' || v_count);
    END;
    v_count: 1
    PL/SQL procedure successfully completed.HTH
    SS

  • DDL in pl/sql block???

    Hi,
    begin
    create table sample_test ( x number);
    end;
    gives an error, but
    begin
    execute immediate 'create table sample_test(x number)';
    end
    very much works, is there any specific reason to why the first way of doing a DDL statement is restricted or avoided???
    cheere

    Hi,
    begin
    create table sample_test ( x number);
    end;
    you can not issue DDL like above in a PL/SQL Block, hence error
    gives an error, but
    begin
    execute immediate 'create table sample_test(x
    (x number)';
    end
    You Issued DDL in PL/SQL Block using Native Dynamic SQL, hence worked !!
    Message was edited by:
    biswabijay

  • Execute a string containing a PL/SQL block

    Hi,
    I would like to build a string containing a PL/SQL block and execute it dynamically. Is there way to do this.
    Note - The reason I want to this is because, based on certain table data dictionary views the declaration section of the PL/SQL block that I am building might vary
    I tried to use EXECUTE IMMEDIATE, it didn't work, pls let me know if I am missing something.
    DECLARE
    v_str VARCHAR2(1000);
    BEGIN
    v_str := 'BEGIN NULL; END';
    EXECUTE IMMEDIATE v_str;
    END;
    /

    Hi,
    Just happened to find it. EXECUTE IMMEDIATE can be used, the bug with my code was I didn't have a ; after the END statement. Corrected code is below, thanks for your time
    DECLARE
    v_str VARCHAR2(1000);
    BEGIN
    v_str := 'BEGIN NULL; END;';
    EXECUTE IMMEDIATE v_str;
    END;
    /

Maybe you are looking for

  • Can not open camera raw from 70d to adobe photoshop cc

    can not open camera raw from 70d to adobe photoshop cc

  • Device won't show up on my desktop

    Hi, everyone. I tried connecting my phone and my camera (individually at different times) to my Macbook Pro, but for some reason, neither device shows up on my desktop. I looked at my Finder preferences and "external disks" is checked off to show up

  • Using Mac OS 10.6 Server as Desktop

    Are there any advantages? disadvantages? I want MySQL and I want it the +Apple way.+

  • Issue with the home buton... HELP

    So I just purchased my iPhone4 yesterday and here I am less than 24hrs later and there's an issue with the home button. Either the button won't work at all and I have to restart the phone, or its magically being held down and the voice control is act

  • Help: JSP Error

    Hi ! Can anyone help to describe the problem: I have .jsp page that connects to Oracle DB and selects some data from it. (DB NLS_LANG = win1251, client charset=win1251). Some times (I dont find any dependences) I receive the following: Exception: jav