Problem in executing Dynamic SQL in SYSTEM Schema

Hi,
I am trying to execute the below given dynamic sql from SYSTEM account.
'SELECT cnt, SUBSTR(SYS_CONNECT_BY_PATH(column_name,'',''),2) AS COLS FROM
( SELECT b.column_name,b.position,COUNT(1) over (ORDER BY a.constraint_name) AS cnt
FROM SYS.dba_Constraints a JOIN SYS.dba_Cons_Columns b ON (a.constraint_name = b.constraint_name)
WHERE a.table_name = '''||UPPER(v_table)||''' AND a.owner = '''||UPPER(v_owner)||''' AND a.constraint_type = ''P'' ORDER BY b.position
) WHERE position = cnt START WITH position = 1 CONNECT BY PRIOR position = position -1';
I am getting the error ORA-00942:table or view does not exist.
If I execute the created SQL from SQLPlus it is getting executed without any errors. If I try the same from SYS account it is working fine without any issues.
Could some one share some light on it.
Thanks in Advance
Sree.

Hi Guys,
I got the solution, for referening objects in DYNAMIC sqls need explicit grant on those objects (not through roles). That is the reason this SQL is working in SYS schema, since SYS owns these tables, but system grot the access to these tables through roles.
Regards,
Sree.

Similar Messages

  • What is the problem with native dynamic sql when counting rows in all table

    what is the problem with native dynamic sql when counting rows in all table?Giving an error "table or view does not exist". Thanks.
    DECLARE
    v_sql_string varchar2(1000);
    v_no_of_rows number;
    BEGIN
    for i in ( select table_name from all_tables )
    loop
    v_sql_string := ' select count(1) from ' || i.table_name;
    dbms_output.put_line( v_sql_string );
    --execute immediate v_sql_string into v_no_of_rows;
    end loop;
    END;

    Usually your problem can be described with 'Who cares'. I mean, for what reason do you do this? I doubt that there's a business need to get 100 % accurate answers for this. Normally such things are used to get a picture about the growth of data.
    Personally I would prefer to have up-to-date statistics for all tables and just query the number of rows from there. Sufficient for me in < 99 % of all cases.
    Just my $ .02...

  • Error in executing dynamic SQL

    i am getting error in executing dynamic SQL
    declare
    vr_RenewService NUMBER(10,0);
    vr_sql VARCHAR2(50);
    begin
    vr_sql:='Select Case
    when 5 <= 365 Then 1
    When 1= 0 Then 1
    else 0 end into' || TO_CHAR(vr_RenewService) || 'from dual;';
    execute immediate vr_sql;
    --dbms_output.put_line(vr_RenewService);
    end;
    ERROR
    ORA-06502: PL/SQL: numeric or value error: character string buffer too small
    ORA-06512: at line 6
    06502. 00000 - "PL/SQL: numeric or value error%s"
    *Cause:   
    *Action:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    i too have tried this before you posted this solution but getting error message
    declare
    vr_RenewService NUMBER(10,0);
    vr_sql VARCHAR2(100);
    begin
    vr_sql:='Select Case
    when 5 <= 365 Then 1
    When 1= 0 Then 1
    else 0 end from dual;';
    execute immediate vr_sql into vr_RenewService;
    --dbms_output.put_line(vr_RenewService);
    end;
    ORA-06512: at line 11
    00911. 00000 - "invalid character"
    *Cause:    identifiers may not start with any ASCII character other than
    letters and numbers. $#_ are also allowed after the first
    character. Identifiers enclosed by doublequotes may contain
    any character other than a doublequote. Alternative quotes
    (q'#...#') cannot use spaces, tabs, or carriage returns as
    delimiters. For all other contexts, consult the SQL Language
    Reference Manual.
    *Action:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • How can I execute Dynamic SQL statement in Forms?

    Hi All,
    I have to execute dynamic SQL statement from Forms
    Below statement I have to execute
    "EXECUTE IMMEDIATE v_stmt INTO v_return;".
    Googled for the same got results saying, Better use Database function or procedures to execute these Dynamic Statements but We want to execute in forms only.
    Can any one help me..
    Thanks,
    Madhu

    So in short you are trading code obfuscation for maintainability and the ability to share code between tools? If from somewhere else you need a procedure already implemented in database PL/SQL (and now ported to forms) this would mean you'd need to implement it in every other tool. In times where you might want to integrate your forms with $other_technology and putting stuff on the database is the first step to share functionality you just go the opposite way? And all that because someone is afraid that somebody might steal your source code? I am sorry to be blunt, but this is just plain stupid.
    Leaving aside that some things like Analytic Functions, Bulk processing or execute immediate are not even available in forms your software consists of how many LOC? How long does it take to bring a new developer up to speed with your source code? Imagine how long that would take for a developer who doesn't have coleagues who know their way around.
    And just so you know: I work for a ISV selling a closed-source product as well. We have 200+ customers all over the planet. We are well aware that wrapped packages can be reverse engineered. The premise is: stored procedures can be reused in every tool we have, if it makes sense to put stuff on the database by all means do it. If someone would want to reverse engineer our software I'd wish him good luck as some parts are implemented in such a hilarious complicated way I have troubles understanding them (and quite frankly I refuse to understand certain parts, but that's another story). I do work for almost 10 years for that ISV.
    In any case the possible solutions have already been mentioned: you have exec_sql, create_group_from_query and forms_ddl to execute dynamic SQL in forms whereas forms_ddl is a one way street and most certainly not the thing you need or want. Take a look at the documentation for the other 2 things.
    cheers

  • Execute Dynamic SQL statement using procedure builder

    i want to execute a dynamic SQL statment using procedure builder not using forms
    because my statement depending on a variable table name
    i know that i can do that from forms using
    FORMS_DDL('SQL_STAT');
    but i wanna to use the procedure builder
    which function i should use and how?
    please explain in example if you don't mind.
    thanks

    Hi,
    You can very well use DBMS_SQL Package supplied by Oracle for doing this.
    Search for DBMS_SQL in OTN. You will get all info regarding this.
    Regards.
    <BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by itslul:
    i want to execute a dynamic SQL statment using procedure builder not using forms
    because my statement depending on a variable table name
    i know that i can do that from forms using
    FORMS_DDL('SQL_STAT');
    but i wanna to use the procedure builder
    which function i should use and how?
    please explain in example if you don't mind.
    thanks<HR></BLOCKQUOTE>
    null

  • Avoid Hard Parsing for executing dynamic SQL using DUAL table Oracle

    I want to know if dynamic sql statements involving DUAL table can be modified to remove HARD PARSING.
    We have several SQL statements are stored in configuration table, here is sample example
    -- query 1 before replacing index values as stored in config table ---
    select count(*) from dual where  'REPLACE_VALUE_OF_INDEX_3' IN ('K')
    AND (('REPLACE_VALUE_OF_INDEX_13' IN ('1053','1095','1199') ) OR ('REPLACE_VALUE_OF_INDEX_13' IN ('1200') ))
    AND 'REPLACE_VALUE_OF_INDEX_2' IN ('6')
    AND 'REPLACE_VALUE_OF_INDEX_15' IN ('870001305')
    -- query 1 after replacing index values--
    select count(*) from dual where  'REPLACE_VALUE_OF_INDEX_10' IN ('K')
    AND (('1030' IN ('1053','1095','1199') ) OR ('1030' IN ('1200') ))
    AND '2' IN ('6')
    AND 'X' IN ('870001305')
    -- query 2 before replacing index values as stored in config table --
    select count(*) from dual where  'REPLACE_VALUE_OF_INDEX_5' IN ('361A','362A')
    AND 'REPLACE_VALUE_OF_INDEX_22' BETWEEN '200707' AND '200806'
    -- query 2 after replacing index values--
    select count(*) from dual where  '3MAA' IN ('361A','362A') AND '201304' BETWEEN '200707' AND '200806'

    If I got it right you have some (maybe lots of) conditions stored in a table (be patient - it's my interpretation)
    create table eb_conditions as
    select 1 rid,q'{:5 IN ('361A','362A') AND :3 BETWEEN '200707' AND '200806'}' cndtn from dual union all
    select 2,q'{:2 IN ('361A','362A') AND :3 BETWEEN '200707' AND '200806'}' from dual union all
    select 3,q'{:1 IN ('K') AND ((:2 IN ('1053','1095','1199') ) OR (:4 IN ('1200') )) AND :3 IN ('6') AND :5 IN ('870001305')}' from dual
    RID
    CNDTN
    1
    :5 IN ('361A','362A') AND :3 BETWEEN '200707' AND '200806'
    2
    :2 IN ('361A','362A') AND :3 BETWEEN '200707' AND '200806'
    3
    :1 IN ('K') AND ((:2 IN ('1053','1095','1199') ) OR (:4 IN ('1200') )) AND :3 IN ('6') AND :5 IN ('870001305')
    and you have to check the conditions using values stored in an array
    I used a table instead: the vl at rid = 1 representing the value of bind variable :1 in eb_conditions table and so on ...
    create table eb_array as
    select 1 rid,'K' vl from dual union all
    select 2,'1199' from dual union all
    select 3,'200803' from dual union all
    select 4,'1000' from dual union all
    select 5,'870001305' from dual
    RID
    VL
    1
    K
    2
    1199
    3
    200803
    4
    1000
    5
    870001305
    You want to check the conditions using select count(*) from dual where <condition with binds substituted fron the array>
    Judging from the title Hard Parsing represents the major problem and you cannot avoid it since every condition to be verified is different from every other condition.
    I think your best bet is not to evaluate conditions row by row - context shift cannot be avoided and there might be more than one for each iteration.
    So try to do it in a single step:
    declare
    w_cndtn varchar2(4000);
    w_clob  clob;
    w_cursor sys_refcursor;
    one number;
    two number;
    begin
      dbms_lob.createtemporary(w_clob,false);
      for rw in (select rid,
                        max(cndtn) cndtn,
                        listagg(val,',') within group (order by rn)||',' usng
                   from (select c.rid,c.cndtn,c.rn,c.bind,
                                replace(rtrim(c.bind),':'||to_char(v.rid),''''||v.vl||'''') val
                           from (select rid,
                                        cndtn,
                                        regexp_substr(cndtn,':\d+ ',1,level) bind,
                                        level rn
                                   from eb_conditions
                                 connect by level <= regexp_count(cndtn,':')
                                        and prior rid = rid
                                        and prior sys_guid() is not null
                                ) c,
                                eb_array v
                          where instr(c.bind,':'||v.rid||' ') > 0
                  group by rid
      loop
        w_cndtn := rw.cndtn;
        while instr(w_cndtn,':') > 0
        loop
          w_cndtn := replace(w_cndtn,trim(regexp_substr(w_cndtn,':\d+ ',1,1)),substr(rw.usng,1,instr(rw.usng,',') - 1));
          rw.usng := substr(rw.usng,instr(rw.usng,',') + 1);
        end loop;
        w_cndtn := 'select '||to_char(rw.rid)||' cndtn_id,count(*) from dual where '||w_cndtn||' union all ';
        w_clob := w_clob ||' '||w_cndtn;
      end loop;
      w_clob := substr(w_clob,1,instr(w_clob,'union all',-1,1) - 1);
      open w_cursor for w_clob;
      loop
        fetch w_cursor into one,two;
        exit when w_cursor%notfound;
        dbms_output.put_line(to_char(one)||':'||to_char(two));
      end loop;
      dbms_lob.freetemporary(w_clob);
    end;
    1:0
    2:0
    3:0
    Statement processed.
    Regards
    Etbin

  • Executing dynamic SQL in trigger

    Hi !
    I just wanted to ask the community if it is ok using dynamic SQL
    with execute immediate in triggers, like:
    if (deleting or updating)
    and nvl(:old.col_val, 0) != 0
    then
    /* Update */
    sSql :=
    'update TMP_STAT_TEST ' ||
    'set col_val_' || to_char(:old.col_id) || ' = col_val_' ||
    to_char(:old.col_id) || ' - :1 ';
    execute immediate sSql using :old.col_val;
    end if;
    All dyn SQL inside the trigger does not reference the triggered
    table, so I will not get a mutating trigger.
    Thanks for your feedback.
    Stefan.

    Hi Andrew !
    I have a "master" table with date and amount and some more
    columns. I have a statistic table which must get updated
    accordingly each time the master table is changed (ins, upd,
    del). In this stats table there a cols like "sum_2000, sum_2001"
    etc. The dynamic statement depends on date to figure out the
    column name for the sum_yyyy cols, and amount will be
    added/subtracted. Of course there is a check that the trigger
    cannot handle when the year of the date is smaller than 2000 or
    greater than 2009.
    That's why I want to use dyn SQL.
    Stefan.

  • Query Build problem in Native Dynamic SQL

    I am writing a procedure to calculate the summary from the monthly salary table by dynamically building the query using ref cursor,open cursor using the bind variables and bulding the query statement.But the query is not returning any rows
    vSQL := 'select a.ad_code,a.acc_code,m.jobtype,m.estbtype,'||'sum(m.'||vabsrec.vadsn||') from gencopayroll.pay_allowaccounthead a,'||
    'gencopayroll.pay_monthly_paybill m where bill_type = :ptype and loc_id = :plocid '||
    'and processing_period = :pprocmon and a.ad_code = :padcode group by a.acc_code,'||
    'm.jobtype,m.estbtype,a.ad_code';

    I am writing a procedure to calculate the summary
    from the monthly salary table by
    1) dynamically building the query using ref cursor
    2) open cursor using the bind variables
    3) bulding the query statement.Can you show us the code that performs these steps.
    Once a cursor is opened you need to fetch the rows from it in order to get the data.
    Also, have you checked that the query runs as plain non-dynamic SQL within SQL*Plus with the same parameters, to see that it returns data?
    What is you purpose for using Dynamic SQL? If you clarify the requirement we may be able to show you how to do the same using non-dynamic SQL.

  • ORA-06550 error while executing Dynamic Sql

    EXECUTE IMMEDIATE 'BEGIN :out := ' || lv_formula || '; END;' using out RESULT ;
    here lv_formula:= ((capcost * rate / 100) + (ob + (ob * interestrate / 100)))
    lv_formula is varchar2(360);
    and capcost,rate,ob & interestrate are my column names . Here I m declaring variables same as these columns and datatype is number.
    I am not getting any compilation error. syntax is correct
    while executing this sql i m getting ora-06550.
    can any one help me for resolving this error.
    Thanks In Advance
    hoping for more respose

    Thanks Billy for your response,
    but i still didn't get my desired solution so i am pasting my whole plsql code here. so that u can get better picture.
    /* Formatted on 2010/11/22 15:23 (Formatter Plus v4.8.5) */
    PROCEDURE xb (
    l_refno IN VARCHAR2,
    l_billtyp IN VARCHAR2,
    infor IN VARCHAR2,
    aresult OUT NUMBER
    IS
    --A NUMBER:=10; B NUMBER :=5;
    lv_formula VARCHAR2 (270); --:=(A+B)*2+5;
    l_agtmnt VARCHAR2 (18);
    l_type VARCHAR2 (2);
    RESULT NUMBER;
    refno afxbrmst.refno%TYPE;
    billtyp afxbrmst.billtyp%TYPE;
    ob afxbrmst.ob%TYPE;-- := 45283;
    status afxbrmst.status%TYPE -- := 0;
    dueamt afxbrmst.dueamt%TYPE -- := 0;
    duedate afxbrmst.duedate%TYPE;
    frequency afxbrmst.frequency%TYPE;
    lastrevrate afxbrmst.lastrevrate%TYPE -- := 0;
    penaltyamt afxbrmst.penaltyamt%TYPE -- := 0;
    accounted afxbrmst.accounted%TYPE;
    lastbillraised afxbrmst.lastbillraised%TYPE;
    noofstaff afxbrmst.noofstaff%TYPE;
    items afxbrmst.items%TYPE;
    area afxbrmst.area%TYPE;
    LENGTH afxbrmst.LENGTH%TYPE -- := 0;
    interest afxbrmst.interest%TYPE -- := 0;
    capcost NUMBER; afxbrmst.capcost%TYPE; :=1;
    -- := 1509432;
    periodfrom afxbrrate.periodfrom%TYPE;
    periodto afxbrrate.preiodto%TYPE;
    rcsrate afxbrrate.rcsrate%TYPE -- := 0;
    length_area afxbrrate.length_area%TYPE;
    rate afxbrrate.rate%TYPE --:= 0;
    -- :=3;
    erate afxbrrate.erate%TYPE -- := 0;
    feamount afxbrrate.feamount%TYPE -- := 0;
    interestrate afxbrrate.interestrate%TYPE -- := 0;
    -- :=12;
    fiamount afxbrrate.fiamount%TYPE --:= 0;
    penaltyrate afxbrrate.penaltyrate%TYPE -- := 0;
    l_ob afxbrmst_test.ob%TYPE -- := 0;
    l_capcost afxbrmst.capcost%TYPE -- := 0;
    raise_application_trigger EXCEPTION;
    inti INTEGER;
    BEGIN
    --break;
    lv_formula :=
    TO_NUMBER ((capcost * rate / 100) + (ob + (ob * interestrate / 100)));
    --infor;
    l_agtmnt := l_refno;
    l_type := l_billtyp;
    BEGIN
    SELECT a.refno, a.billtyp, a.ob, a.status, a.dueamt, a.duedate,
    a.frequency, a.lastrevrate, a.penaltyamt, a.accounted,
    a.lastbillraised, a.noofstaff, a.items, a.area, a.LENGTH,
    a.interest, a.capcost
    INTO refno, billtyp, ob, status, dueamt, duedate,
    frequency, lastrevrate, penaltyamt, accounted,
    lastbillraised, noofstaff, items, area, LENGTH,
    interest, capcost
    FROM afxbrmst_test a
    WHERE billtyp = l_type AND refno = l_agtmnt;
    EXCEPTION
    WHEN NO_DATA_FOUND
    THEN
    NULL;
    END;
    BEGIN
    SELECT b.periodfrom, b.preiodto, b.rcsrate, b.length_area, b.rate,
    b.erate, b.feamount, b.interestrate, b.fiamount, b.penaltyrate
    INTO periodfrom, periodto, rcsrate, length_area, rate,
    erate, feamount, interestrate, fiamount, penaltyrate
    FROM afxbrrate b
    WHERE billtype = l_type;
    EXCEPTION
    WHEN NO_DATA_FOUND
    THEN
    NULL;
    END;
    EXECUTE IMMEDIATE 'BEGIN :out := ' || lv_formula || '; END;'
    USING OUT RESULT; --USING OUT RESULT;
    aresult := RESULT;
    END;
    -- Procedure
    here i m passing the parameter through my form builder and calling this procedure.
    can you tell me where i am making wrong
    Thanks again

  • Strange problem with executing PL/SQL procedure from sqlplus.

    Hello,
    basicly, I'm strugling with executing a procedure written in PL/SQL from sqlplus.
    It's all because I convert a data in procedure by other function.
    SELECT partition_name
    INTO strpartition
    FROM user_tab_partitions
    WHERE table_name = tablename_in
    and substr(partition_name, -8, length(partition_name)) = (SELECT F_CONVERT_DATE(tablename_in,p_date) from dual);
    /tablename_in and p_date are input parameters/
    Function F_CONVERT_DATE looks more less like :
    select
    TO_CHAR(TRUNC(NEXT_DAY(ADD_MONTHS(substr(partition_name, -8, length(partition_name)), -3), 'MONDAY')-7), 'YYYYMMDD')
    END
    AS p_date
    INTO v_okr
    FROM user_tab_partitions
    where substr(partition_name, -8, length(partition_name)) = p_date;
    Well, the thing is that procedure is executable from TOAD without any errors !! But when I try to execute it straight from sqlplus it returns:
    ORA-01861: literal does not match format string
    ORA-06512: at "F_CONVERT_DATE", line 13
    ORA-06512: at "NAME_OF_PROCEDURE", line 17
    Tip: When I don't use functions ADD_MONTHS, NEXT_DAY and TO_CHAR in function F_CONVERT_DATE sqlplus can execute it. But only when I use them it returns an error.
    Anybody has a clue how to solve it?
    Regards !

    Hi, Nodex,
    Avoid implicit conversions.
    For example:
    TO_CHAR(TRUNC(NEXT_DAY(ADD_MONTHS(substr(partition_name, -8, length(partition_name)), -3), 'MONDAY')-7), 'YYYYMMDD')ADD_MONTHS expects a DATE as its first argument.
    SUBSTR returns a VARCHAR2, so you're calling ADD_MONTHS with a VARCHAR2 where it expects a DATE.
    For good or ill, the system tries to avoid raising an error in this case by implicitly converting the VARCHAR2 to a DATE. Exactly how it does that depends on
    (a) the tool you are using (SQL*Plus or Toad, for example),
    (b) the version (Oracle 10 behaved quite different form Oracle 9),
    (c) environmental settings (such as NLS_DATE_FORMAT), which in turn may depend on initialization parameneters, and
    (d) who knows what else.
    When you have to convert, do so explicitly.
    You can convert a VARCHAR2 to a DATE using TO_DATE, like this:
    TO_CHAR ( TRUNC ( NEXT_DAY ( ADD_MONTHS ( TO_DATE ( SUBSTR ( partition_name
                                                       , -8
                                          , LENGTH (partition_name)
                                     , 'YYYYMMDD'     -- or whatever
                             , -3
                      , 'MONDAY'
              - 7
         , 'YYYYMMDD'
         )

  • Execute dynamic sql  statement

    Hi all
    CREATE TABLE  XX_OFFICE_USER_IMP
        ID              NUMBER,
        OFFICE          VARCHAR2(10 BYTE),
        USER_NAME       VARCHAR2(10 BYTE),
        BANK_ACCOUNT_ID NUMBER,
        TRANSFERED      NUMBER
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (421,'0000','F0000',10029,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (422,'0000','F0000',10031,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (423,'0000','F0000',10033,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (424,'0000','F0000',10036,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (425,'0000','F0000',10037,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (426,'0000','F0000',10039,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (427,'0000','F0000',10041,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (428,'0000','F0000',10046,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (429,'0000','F0000',10048,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (430,'0000','F0000',10067,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (431,'0000','F0000',10072,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (432,'0000','F0000',10087,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (433,'0000','F0000',10092,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (434,'0000','F0000',10008,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (435,'0000','F0000',10012,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (436,'0000','F0000',10013,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (437,'0000','F0000',10014,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (438,'0000','F0000',10017,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (439,'0000','F0000',10019,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (440,'0000','F0000',10024,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (441,'0000','F0000',10025,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (442,'0000','F0000',10001,null);
    Insert into xx_office_user_imp (ID,OFFICE,USER_NAME,BANK_ACCOUNT_ID,TRANSFERED) values (443,'0000','F0000',10002,null);
    CREATE TABLE XXBG_CASIER_CASH
        CASHIER         VARCHAR2(32 BYTE),
        BANK_ACCOUNT_ID NUMBER(38,0)
    declare
    v_exe_grant varchar2(32767 char);
    begin
    for i in (select * from xx_office_user_imp where office = '0000') loop
      insert into XXBG_CASIER_CASH values (i.user_name, i.bank_account_id);
      v_exe_grant :=
                     'create user '  || i.user_name || ' identified by ' || i.user_name || ';'
                  || 'GRANT create session to ' || i.user_name || ';'
                  || 'GRANT select on apps.XXBG_CE_STATEMENT_HEADERS_CASH to ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.FND_USER TO ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.dFND_DESCR_FLEX_COL_USAGE_TL TO ' || i.user_name || ';'
                  || 'GRANT select on apps.fnd_descr_flex_column_usages to ' || i.user_name || ';'
                  || 'GRANT select on apps.fnd_descriptive_flexs to ' || i.user_name || ';'
                  || 'GRANT select on apps.fnd_descriptive_flexs_tl to ' || i.user_name || ';'
                  || 'GRANT select on ce.ce_statement_headers to ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.fnd_doc_sequence_assignments TO ' || i.user_name || ';'
                  || 'GRANT SELECT ON CE.CE_STATEMENT_HEADERS_S TO ' || i.user_name || ';'
                  || 'GRANT EXECUTE ON APPS.XXBG_GET_NEXTVAL TO ' || i.user_name || ';'
                  || 'GRANT SELECT ON CE.CE_STATEMENT_LINES TO ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.XXBG_CE_STATEMENT_LINES TO ' || i.user_name || ';'
                  || 'GRANT select on apps.CE_BANK_ACCOUNTS to ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.CE_BANK_BRANCHES_V TO ' || i.user_name || ';'
                  || 'GRANT SELECT ON CE.XXBG_CASIER_CASH TO ' || i.user_name || ';'
                  || 'GRANT EXECUTE ON APPS.XXBG_ST TO ' || i.user_name || ';'
                  || 'GRANT select on ce.xxbg_ce_statement_lines_detail to ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.ce_transaction_codes TO ' || i.user_name || ';'
                  || 'GRANT select on ce.ce_statement_lines_s to ' || i.user_name || ';'
                  || 'GRANT SELECT ON CE.XXBG_CE_STATEMENT_LINES_DET_SQ TO ' || i.user_name || ';'
                  || 'GRANT select on apps.xx_pko_lines to ' || i.user_name || ';'
                  || 'GRANT SELECT ON apps.xx_rko_lines TO ' || i.user_name || ';'
                  || 'GRANT select on apps.XX_INVOICE_RELATIONS_CASH to ' || i.user_name || ';'
                  || 'GRANT select on APPS.PO_VENDOR_SITES_ALL to ' || i.user_name || ';'
                  || 'GRANT select on ap.AP_INVOICE_LINES_INTERFACE_S to ' || i.user_name || ';'
                  || 'GRANT select on ap.AP_INVOICE_LINES_INTERFACE to ' || i.user_name || ';'
                  || 'GRANT select on APPS.ap_distribution_set_lines_all to ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.AP_INVOICES_INTERFACE_S TO ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.AP_INVOICES_INTERFACE TO ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.AP_DISTRIBUTION_SETS_ALL  TO ' || i.user_name || ';'
                  || 'GRANT select on apps.ce_lookups  to ' || i.user_name || ';'
                  || 'GRANT select on ar.HZ_CUST_SITE_USES_ALL to ' || i.user_name || ';'
                  || 'GRANT select on ar.HZ_LOCATIONS to ' || i.user_name || ';'
                  || 'GRANT select on ar.HZ_PARTIES to ' || i.user_name || ';'
                  || 'GRANT select on ar.HZ_PARTY_SITES to ' || i.user_name || ';'
                  || 'GRANT SELECT ON AR.HZ_CUST_ACCT_SITES_ALL TO ' || i.user_name || ';'
                  || 'GRANT SELECT ON AR.HZ_CUST_ACCOUNTS TO ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.XXBG_CLAIMS_V TO ' || i.user_name || ';'
                  || 'GRANT select on apps.xxbg_insis_agents_v to ' || i.user_name || ';'
                  || 'GRANT select on ce.xxbg_cash_doc_types to ' || i.user_name || ';'
                  || 'GRANT select on AP.AP_BANK_ACCOUNTS_ALL to ' || i.user_name || ';'
                  || 'GRANT SELECT ON AP.AP_BANK_BRANCHES TO ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.FND_DESCR_FLEX_CONTEXTS TO ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.FND_DESCR_FLEX_CONTEXTS_TL TO ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.AP_SUPPLIERS to ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.per_employees_x TO ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.fnd_doc_seq_categories_ap_v TO ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.AP_LC_INVOICE_TYPES_V to ' || i.user_name || ';'
                  || 'GRANT SELECT ON ce.xxbg_ce_statement_lines_sq to ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.XXBG_STATEMENT_HEADERS_CASH to ' || i.user_name || ';'
                  || 'GRANT SELECT ON APPS.XXBG_INSIS_POLICY_V TO ' || i.user_name || ';'
                  || 'GRANT insert  ON ce.ce_statement_lines to ' || i.user_name || ';'
                  || 'GRANT INSERT  ON CE.XXBG_CE_STATEMENT_LINES_DETAIL TO ' || i.user_name || ';'
                  || 'GRANT INSERT ON APPS.AP_INVOICES_INTERFACE TO ' || i.user_name || ';'
                  || 'GRANT INSERT ON APPS.AP_INVOICE_LINES_INTERFACE TO ' || i.user_name || ';'
                  || 'GRANT INSERT ON APPS.XX_RKO_LINES TO ' || i.user_name || ';'
                  || 'GRANT INSERT ON APPS.XX_PKO_LINES TO ' || i.user_name || ';'
                  || 'GRANT delete on ce.xxbg_ce_statement_lines_detail to ' || i.user_name || ';'
                  || 'GRANT update on ce.XXBG_CE_STATEMENT_LINES_DETAIL to ' || i.user_name || ';'
                  || 'GRANT DELETE ON CE.CE_STATEMENT_LINES TO ' || i.user_name || ';'
                  || 'GRANT INSERT ON CE.CE_STATEMENT_HEADERS TO ' || i.user_name || ';'
                  || 'GRANT update on CE.CE_STATEMENT_HEADERS to ' || i.user_name || ';'
                  || 'GRANT update on ce.CE_STATEMENT_LINES to ' || i.user_name || ';'
                  || 'GRANT select on apps.XX_AGENTS_NO_V to ' || i.user_name || ';' ;
      execute immediate v_exe_grant;
    update xx_office_user_imp
      set transfered = 1
      where id = i.id
    v_exe_grant := '';
    end loop;
    end;
    /After execute the PL/SQL block i receive the error:
    Error report:
    ORA-00911: invalid character
    ORA-06512: at line 79
    00911. 00000 -  "invalid character"
    *Cause:    identifiers may not start with any ASCII character other than
               letters and numbers.  $#_ are also allowed after the first
               character.  Identifiers enclosed by doublequotes may contain
               any character other than a doublequote.  Alternative quotes
               (q'#...#') cannot use spaces, tabs, or carriage returns as
               delimiters.  For all other contexts, consult the SQL Language
               Reference Manual.
    *Action:Any ideas? I think i call execute immediate correctly.
    DB Version: 11g
    Unfortunately i cannot provide you with the sql of the other tables to create them.... Maybe you should try without all the grants... :)
    Thanks in advance,
    Bahchevanov.
    Edited by: bahchevanov on Oct 11, 2012 6:14 AM

    bahchevanov wrote:
    Any ideas? Sure. EXECUTE IMMEDIATE executes a single statment while you are trying to execute whole bunch. So use:
    execute immediate 'create user '  || i.user_name || ' identified by ' || i.user_name;
    execute immediate 'GRANT create session to ' || i.user_name;
    execute immediate 'GRANT select on apps.XXBG_CE_STATEMENT_HEADERS_CASH to ' || i.user_name;
    .SY.

  • How to execute dynamic sql in forms 6i?

    Cursor_Handle     Integer     := DBMS_SQL.OPEN_CURSOR;
         Out_Put          Integer;
    BEGIN
         DBMS_SQL.PARSE(Cursor_Handle, Sql_Stmt , dbms_sql.v7);
         Out_Put := DBMS_SQL.EXECUTE(Cursor_Handle);
         DBMS_SQL.CLOSE_CURSOR(Cursor_Handle);
    return true;
    END;
    this is the procedure i have used. it works fine in server side but not in client side. pls help me to solve this problem.
    Advance tanx

    The reason your process won't compile is that you are using a package variable, dbms_sql.v7. (Why aren't you using dbms_sql.native?) If you change that variable to a value of 1 it will probably work.
    However, Francois is correct -- In Forms, you should be using the Exec_SQL package. It is built for Forms; Oracle does not support using DBMS_SQL from Forms. In fact, if you use Forms Builder 10g connected to Oracle 9i and try to compile, the compiler fails with internal errors. They say it works again if you connect to Oracle 10.
    Exec_SQL is identical to DBMS_SQL, except for exception handling. You have to make an additional call to catch the error if an exception occurs: Exec_SQL.Last_Error_Code

  • How to reduce Parse time in dynamic SQL

    I'm using for a part of my code dynamic SQL with DBMS_SQL Package, this dynamic SQL code is located in a loop with say 1000 repeatations, if we trace the code we see that this sql statement is parsed 1000 times and this causes
    a serios performance issue. If i convert this part of code into static PLSQL code , the statement is parsed only one time as expected. i would like
    to know how i can resolve this problem in the dynamic SQL code.
    Why in case of static SQL, despite it is inside the loop, it is parsed
    only one time by ORACLE and in case of dynamic SQL as many as the upper limit of counter.
    Why the ORACLE has different behaviour to parse them? Is there any way or trick
    to force ORACLE to parse it only one time like static SQL?

    despite the open cursor is also inside the loop but oracle parse it only one time.That is because PL/SQL is caching your cursor and resuing it. With DBMS_SQL you are opening a new cursor area in the loop for each iteration (DBMS_SQL.OPEN_CURSOR).
    what you need to do is open/parse once and bind the value each time through the loop:
    DECLARE
        expr      VARCHAR2(1000);
        check_cur PLS_INTEGER;
        nDummy    PLS_INTEGER;
        nFetched  PLS_INTEGER;
    BEGIN
        -- Open Cursor
        check_cur := dbms_sql.open_cursor;
        expr      := 'select 1 from dual where ' || ':bindvar1 ' || '=' ||
                     ' ''bindvar1'' ';
        -- Parse Cursor
        dbms_sql.parse(check_cur,
                       expr,
                       1);
        FOR counter IN 1 .. 1000
        LOOP
            -- Define Column
            DBMS_SQL.define_column(check_cur,
                                   1,
                                   1);
            -- Do Binding
            dbms_sql.bind_variable(check_cur,
                                   ':bindvar1',
                                   'bindname1');
            -- Execute Cursor
            nDummy := DBMS_SQL.EXECUTE(check_cur);
            -- Fetch Rows
            nFetched := DBMS_SQL.fetch_rows(check_cur);
        END LOOP;
        -- Close Cursor
        dbms_sql.close_cursor(check_cur);
    END;
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.21       0.23          0          0          0           0
    Execute   1000      0.11       0.10          0          0          0           0
    Fetch     1000      0.03       0.01          0          0          0           0
    total     2001      0.35       0.35          0          0          0           0

  • Callprocedure which contain dynamic sql that retrieve multiple records from VB

    hi dear i have problem with using dynamic sql to retrieve multipule records
    and access them from visual basic
    first i use Oracle8 Enterprise Edition Release 8.0.4.0.0
    and odbc microsoft odbc for oracle version 2.573.4403.00
    these are the whole processes to do that
    please check the steps and if u find any error say to me....
    1-table description
    Name Null? Type
    PORT_KEY NOT NULL VARCHAR2(4)
    CITY_KEY NOT NULL VARCHAR2(4)
    and port_key is primary key
    2- package declaration
    create or replace package ww1
    as
    procedure bound_type
    (v_origin in varchar2,v_flag in varchar2
    ,city_key out dbms_sql.varchar2_table
    end ;
    3- package body
    create or replace package body ww1
    as
    procedure bound_type
    (v_origin in varchar2,v_flag in varchar2
    ,city_key out dbms_sql.varchar2_table
    is
    str varchar2(1000);
    cur_hdl integer :=dbms_sql.open_cursor;
    rows_processed integer;
    indx integer :=1;
    begin
    str:='select ltrim(rtrim(city_key)) from special_airport ';
    if ltrim(rtrim(upper(v_flag))) = upper('c')
    then
    str:=str &#0124; &#0124; ' where city_key=ltrim(rtrim(upper('&#0124; &#0124;''''&#0124; &#0124;v_origin&#0124; &#0124;''''&#0124; &#0124;')))';
    elsif ltrim(rtrim(upper(v_flag))) = upper('a') then
    str:= str &#0124; &#0124;' where port_key=ltrim(rtrim(upper('&#0124; &#0124;''''&#0124; &#0124;v_origin&#0124; &#0124;''''&#0124; &#0124;')))';
    end if;
    dbms_sql.parse(cur_hdl,str, dbms_sql.native );
    dbms_sql.define_array(cur_hdl,1,city_key,10,indx);
    rows_processed:=dbms_sql.execute(cur_hdl);
    loop
    rows_processed:=dbms_sql.fetch_rows(cur_hdl);
    dbms_sql.column_value(cur_hdl,1,city_key);
    exit when rows_processed != 10;
    end loop;
    dbms_sql.close_cursor(cur_hdl);
    end bound_type;
    end ww1;
    4- code for calling stored procedure from visual basic
    dim c As New ADODB.Connection
    Dim cmd As New ADODB.Command
    Dim rs As New ADODB.Recordset
    c.Open "dsn=dsnName;uid=uidValue;pwd=pwdValue"
    With cmd
    Set .ActiveConnection = c
    .CommandType = adCmdText
    '---------(call bound_type stored procedure)--------------
    .Parameters.Append .CreateParameter(, adBSTR, adParamInput, , "bwi")
    .Parameters.Append .CreateParameter(, adBSTR, adParamInput, , "a")
    .CommandText = "{call fares_procedures.bound_type (?,?,{resultset 10, city_key }) }"
    End With
    Set rs.Source = cmd
    rs.Open
    While Not rs.EOF
    z = rs.Fields("city_key")
    rs.MoveNext
    Wend
    Set cmd = Nothing
    rs.Close
    Set rs = Nothing
    c.Close
    Set c = Nothing
    ***********result get error*************
    please check the code and steps and if u find any error contact me
    thanks any way
    null

    hi every one i got a simple solusion and its faster than dbms_sql package by 1.5 to 3 times...but it works only for oracle 8i or higher......
    after concatenat the string of dynamic sql
    but it in a ref cursor and loop on it
    TYPE VAR_TABLE IS TABLE OF VARCHAR2(40)
    INDEX BY BINARY_INTEGER;
    procedure bound_type
    (v_origin in varchar2,v_flag in varchar2
    ,city_key out var_table )
    is
    str varchar2(1000);
    type curtype is ref cursor;
    sa_cur curtype;
    i integer default 1;
    begin
    str:='select ltrim(rtrim(city_key)) from special_airport ';
    if ltrim(rtrim(upper(v_flag))) = upper('c')
    then
    str:=str &#0124; &#0124; ' where city_key=ltrim(rtrim(upper('&#0124; &#0124;''''&#0124; &#0124;v_origin&#0124; &#0124;''''&#0124; &#0124;')))';
    elsif ltrim(rtrim(upper(v_flag))) = upper('a') then
    str:= str &#0124; &#0124;' where port_key=ltrim(rtrim(upper('&#0124; &#0124;''''&#0124; &#0124;v_origin&#0124; &#0124;''''&#0124; &#0124;')))';
    end if;
    open sa_cur for str;
    loop
    fetch sa_cur into city_key(i);
    exit when sa_cur%notfound;
    i:=i+1;
    end loop;
    close sa_cur;
    end bound_type;
    null

  • DBMS_SQL.TO_REFCURSOR and dynamic sql

    Hello,
    We've stored procedures that execute dynamic sql statements, an example template given below:
    create procedure sp_procedure1 (
    P_pselect_txt IN NVARCHAR2 DEFAULT NULL
    P_CV1 IN OUT PK_COM_DEFS.CV_TYP ) -- cv_typ is a loosely typed ref cursor.
    As
    begin
    open p_cv1 for p_select_txt;
    end;
    Now the issue is, open cursor do not support NVARCHAR2 data type. So, trying to use a new feature in 11g, DBMS_SQL.TO_REFCURSOR.
    Changed the above procedure as below:
    create procedure sp_procedure1 (
    P_pselect_txt IN NVARCHAR2 DEFAULT NULL
    P_CV1 IN OUT PK_COM_DEFS.CV_TYP ) -- cv_typ is a loosely typed ref cursor.
    As
    begin
    c number;
    r number;
    c:= dbms_sql.open_cursor;
    dbms_sql.parse (c, P_pselect_txt, dbms_sql.native);
    r := dbms_sql.execute(c);
    p_cv1 := dbms_sql.to_refcursor(c);
    dbms_sql.close_cursor (c);
    end;
    but getting "statement handle not executed" & access denied on sql.dbms_sql error messages.
    Could someone suggest how to change the above procedure to use dbms_sql.to_refcursor?
    thanks
    K.

    SQL> CREATE TABLE ntest (
      2  mycol NVARCHAR2(30));
    Table created.
    SQL> DECLARE
      2    rec_array SYS_REFCURSOR;
      3  BEGIN
      4    OPEN rec_array FOR
      5    'SELECT mycol FROM ntest';
      6  END;
      7  /
    PL/SQL procedure successfully completed.If you can not do this in your version then please post the results of this query:
    SELECT * FROM v$version;

Maybe you are looking for