Regd:- Execute Immediate

HI,
WHEN I EXECUTE THE BELOW QUERY I get PLSQL /Numeric or value error. Suggestions are welcome.
DECLARE
CURSOR c1
IS
SELECT object_name, object_type, owner
FROM all_objects
WHERE object_type IN ('PACKAGE')
AND object_name = 'PKG_UPLOAD'
AND owner = 'TEST';
lv_v_header VARCHAR2 (2000) := 'SELECT DBMS_METADATA.GET_DDL( ';
lv_v_footer VARCHAR2 (200) := ' )FROM DUAL';
lv_v_object_type all_objects.object_type%TYPE;
lv_v_object_name all_objects.object_name%TYPE;
lv_v_owner all_objects.owner%TYPE;
lv_v_stmt VARCHAR2 (4000);
lv_v_quotes VARCHAR2 (100) := '''';
lv_v_comma VARCHAR2 (100) := ',';
lv_v_value VARCHAR2 (32767);
filehandler UTL_FILE.file_type;
lv_v_file_name VARCHAR2 (3000);
BEGIN
FOR i IN c1
LOOP
BEGIN
lv_v_file_name :=
i.owner
|| '_'
|| i.object_name
|| CASE
WHEN i.object_type = 'PACKAGE'
THEN '.PKB'
WHEN i.object_type = 'TRIGGER'
THEN '.TRG'
END;
lv_v_object_type := i.object_type;
lv_v_object_name := i.object_name;
lv_v_owner := i.owner;
lv_v_stmt :=
lv_v_header
|| lv_v_quotes
|| lv_v_object_type
|| lv_v_quotes
|| lv_v_comma
|| lv_v_quotes
|| lv_v_object_name
|| lv_v_quotes
|| lv_v_comma
|| lv_v_quotes
|| lv_v_owner
|| lv_v_quotes
|| lv_v_footer;
EXECUTE IMMEDIATE lv_v_stmt
INTO lv_v_value;
filehandler := UTL_FILE.fopen ('TEST_DIR', lv_v_file_name, 'w');
UTL_FILE.putf (filehandler, lv_v_value||'\n');
UTL_FILE.fclose (filehandler);
END;
END LOOP;
END;
/

In case you need help to use UTL_FILE, then here it is.
SQL> DECLARE
  2    myddl clob;
  3    fname VARCHAR2(200);
  4    extn VARCHAR2(200);
  5 
  6  /* Main function to get the DDls with DBMS_METADATA */ 
  7    FUNCTION get_metadata(pi_obj_name  dba_objects.object_name%TYPE,
  8                          pi_obj_type  IN dba_objects.object_type%TYPE,
  9                          pi_obj_owner IN dba_objects.owner%TYPE) RETURN clob IS
10      h   number;
11      th  number;
12      doc clob;
13    BEGIN
14      h := DBMS_METADATA.open(pi_obj_type);
15      DBMS_METADATA.set_filter(h, 'SCHEMA', pi_obj_owner);
16      DBMS_METADATA.set_filter(h, 'NAME', pi_obj_name);
17      th := DBMS_METADATA.add_transform(h, 'MODIFY');
18      th := DBMS_METADATA.add_transform(h, 'DDL');
19      --DBMS_METADATA.set_transform_param(th,'SEGMENT_ATTRIBUTES',false);
20      doc := DBMS_METADATA.fetch_clob(h);
21      DBMS_METADATA.CLOSE(h);
22      RETURN doc;
23    END get_metadata;
24 
25  ---Writing the CLOB using UTL_FILE
26  PROCEDURE write_clob(p_clob in clob, pi_fname VARCHAR2) as
27      l_offset number default 1;
28      fhandle UTL_FILE.file_type;
29      buffer VARCHAR2(4000);
30    BEGIN
31      fhandle:=UTL_FILE.fopen('SAUBHIK',pi_fname,'A');
32      loop
33        exit when l_offset > dbms_lob.getlength(p_clob);
34        buffer:=dbms_lob.substr(p_clob, 255, l_offset);
35        UTL_FILE.put_line(fhandle,buffer);
36        l_offset := l_offset + 255;
37        buffer:=NULL;
38      end loop;
39      UTL_FILE.fclose(fhandle);
40    END write_clob; 
41 
42  ---Main execution begins. 
43  BEGIN
44    FOR i in (SELECT object_name, object_type, owner
45                FROM dba_objects
46               WHERE owner IN ('SCOTT', 'HR')
47                 AND object_type IN ('PACKAGE', 'TRIGGER')) LOOP
48      --Calling the function.              
49      myddl := get_metadata(i.object_name,i.object_type,i.owner);
50      --Preparing the filename.
51      fname := i.owner||i.object_name || '.' ||
52               CASE WHEN i.object_type='PACKAGE' THEN 'pkb'
53               ELSE 'trg'
54               END;
55     --Writing the file using UTL_FILE.
56      write_clob(myddl,fname);
57    END LOOP;
58  END;
59  /
PL/SQL procedure successfully completed.
SQL> also, about your error, "EXECUTE IMMEDIATE lv_v_stmt INTO lv_v_value;" DBMS_METADATA.get_dll returns a CLOB.

Similar Messages

  • Error while insert data using execute immediate in dynamic table in oracle

    Error while insert data using execute immediate in dynamic table created in oracle 11g .
    first the dynamic nested table (op_sample) was created using the executed immediate...
    object is
    CREATE OR REPLACE TYPE ASI.sub_mark AS OBJECT (
    mark1 number,
    mark2 number
    t_sub_mark is a class of type sub_mark
    CREATE OR REPLACE TYPE ASI.t_sub_mark is table of sub_mark;
    create table sam1(id number,name varchar2(30));
    nested table is created below:
    begin
    EXECUTE IMMEDIATE ' create table '||op_sample||'
    (id number,name varchar2(30),subject_obj t_sub_mark) nested table subject_obj store as nest_tab return as value';
    end;
    now data from sam1 table and object (subject_obj) are inserted into the dynamic table
    declare
    subject_obj t_sub_mark;
    begin
    subject_obj:= t_sub_mark();
    EXECUTE IMMEDIATE 'insert into op_sample (select id,name,subject_obj from sam1) ';
    end;
    and got the below error:
    ORA-00904: "SUBJECT_OBJ": invalid identifier
    ORA-06512: at line 7
    then when we tried to insert the data into the dynam_table with the subject_marks object as null,we received the following error..
    execute immediate 'insert into '||dynam_table ||'
    (SELECT

    887684 wrote:
    ORA-00904: "SUBJECT_OBJ": invalid identifier
    ORA-06512: at line 7The problem is that your variable subject_obj is not in scope inside the dynamic SQL you are building. The SQL engine does not know your PL/SQL variable, so it tries to find a column named SUBJECT_OBJ in your SAM1 table.
    If you need to use dynamic SQL for this, then you must bind the variable. Something like this:
    EXECUTE IMMEDIATE 'insert into op_sample (select id,name,:bind_subject_obj from sam1) ' USING subject_obj;Alternatively you might figure out to use static SQL rather than dynamic SQL (if possible for your project.) In static SQL the PL/SQL engine binds the variables for you automatically.

  • Strange Case on Security Rights and Dynamic SQL (Execute Immediate)

    Hi friends, (forgive me if I write with wrong grammar and sentence, I not used English for daily)
    I got a weird trouble yesterday.
    I created a package (we can called it X, OK!?) which containing Execute Immediate Statement, that function to delete a table (we can called it Y).
    Several days ago, it's worked, but yesterday it wasn't. Last things happened before was recreate those table, and regrant to a role which including user account that execute package X.
    Error Msg shown is ORA-00942 : Table or view does not exist. After rechecked and rechecked, I found nothing that could trigger that error, I used DBMS_OUTPUT.PUT_LINE to debug and show what statement resulted and executed, I cut and paste, and it's worked. I created anonymous PL/SQL Block, and wrote it and executed it, and worked.
    Finally, today, We Grant explicitly those table to user account Y, not via Role, ... and it's work. Interesting thing I think :P
    And, I revoke, execute package and run. I think, there's something about Oracle he..he.. :D .
    Can somebody help me and explain me the reason of that strange symptomp? and right solution? I must know it, because several days again, it's launched / install.
    TIA

    Here is the procedure that get troubled into :)
    PROCEDURE DeleteOld_Job(
    p_Job_Code IN VARCHAR2,
    p_User_Id IN VARCHAR2,
    p_Parameter_Entry IN VARCHAR2,
    p_Status OUT NUMBER )
    IS
    StrSql VARCHAR2(1000);
    CURSOR CTable_Used_By_Report IS
    SELECT TABLE_NAME
    ,TABLE_OWNER
    FROM TABLE_USED_BY_JOB
    WHERE
    Job_Code = p_Job_Code
    BEGIN
    p_Status := 1;
    DBMS_OUTPUT.PUT_LINE('p_Job_Code '| |p_Job_Code );
    DBMS_OUTPUT.PUT_LINE('p_Parameter_Entry '| |p_Parameter_Entry );
    FOR Item IN CTable_Used_By_Report
    LOOP
    StrSql := 'DELETE '| |Item.TABLE_OWNER| |'.'| |Item.TABLE_NAME| |' T WHERE EXISTS ( SELECT 1 FROM USERBATCH.HISTORY_JOB H WHERE H.USER_ID = ' ;
    StrSql := StrSql| |''''| |p_User_Id| |''''| |' AND H.Job_Code = '| |''''| |p_Job_Code| |''''| |' AND H.PARAMETER_ENTRY = '| |'''' | |p_Parameter_Entry| |''''| |' AND T.SESSION_ID = H.TRANSACTION_ID)';
    DBMS_OUTPUT.PUT_LINE(StrSql);
    DBMS_OUTPUT.PUT_LINE(Item.TABLE_OWNER| |'.'| |Item.TABLE_NAME);
    EXECUTE IMMEDIATE StrSql;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('DELETE USERBATCH.HISTORY_JOB WHERE USER_ID ='''| | p_User_Id | |'''
    AND Job_Code ='''| | p_Job_Code | |''' AND PARAMETER_ENTRY = '''| | p_Parameter_Entry | |'''');
    EXECUTE IMMEDIATE 'DELETE USERBATCH.HISTORY_JOB WHERE USER_ID ='''| | p_User_Id | |'''
    AND Job_Code ='''| | p_Job_Code | |''' AND PARAMETER_ENTRY = '''| | p_Parameter_Entry | |'''';
    COMMIT;
    EXCEPTION
    WHEN OTHERS THEN
    ROLLBACK;
    p_Status := 0;
    DBMS_OUTPUT.PUT_LINE( SUBSTR(SQLERRM,1,255) );
    END DeleteOld_Job;
    TIA
    null

  • Execute immediate and dynamic sql

    Dear all;
    Just curious....Why do developers still use dynamic sql..and execute immediate, because I always thought dynamic sql were bads and the use of execute immediate as well...
    or am I missing something...

    There are no 'bad' things and 'good' things.
    There are 'correctly used' and 'incorrectly used' features.
    It depends what you want to do.
    One simple example: Oracle 11.2 - you write a package that fetches data from range interval partitioned table (a new partition is created automatically every day when new key values are inserted). If you use static SQL then whenever Oracle creates a new partition then your package gets invalidated and has to be compiled. If your package is heavily used (by many sessions running in parallel) then you may get this:
    ORA-04068: existing state of packages has been discarded
    ORA-04061: existing state of package body "PACKAGE.XXXXX" has been invalidated
    ORA-06508: PL/SQL: could not find program unit being called: "PACKAGE.XXXXX" Nice, isn't it?
    You can avoid this kind of problems by simply using dynamic SQL. You break dependency with the table and your package is not invalidated when new partition is created.

  • [Solved] Reference apex_application.g_fXX in "execute immediate" statement

    Hi!
    I created a dynamically generated tabular form - the number of columns is not known in advanced. Each cell of the form is a text item generated with apex_item.text().
    I want to write an after-submit process that saves the values from the form into a database table. In this process I already know how many columns there are in the report so I want to do the following:
    --for each row...
    for i in 1..apex_application.g_f01.count loop
      -- and for each column in that row (number of columns is in v_col_count)
      for j in 1..v_col_count loop
        -- get the value of text item
        v_query := 'select apex_application.g_f0' || j || '(' || i || ')' || ' from dual';
        execute immediate v_query into v_value;
        -- now do some DML with v_value
      end loop;
    end loop;The problem is that I get an error: ORA-06553: PLS-221: 'G_Fxx' is not a procedure or is undefined where xx is the number from the generated query.
    My question is - am I doing something wrong or is is just not possible to reference apex_application.g_fxx in "execute immediate"? Will I have to manually check for all 50 possibilites of apex_application.g_fxx? Is there another way?
    TIA,
    Jure

    Well now I know what was wrong and what you were trying to tell me - apex_application.g_fxx is not visible in "plain" SQL. And now I also have a solution to this problem. The point is to wrap the select statement with begin - end block so that the statement is rendered as pl/sql:
    --for each row...
    for i in 1..apex_application.g_f01.count loop
      -- and for each column in that row (number of columns is in v_col_count)
      for j in 1..v_col_count loop
        -- get the value of text item
        v_query := 'begin select apex_application.g_f0' || j || '(:i)' || ' into :x from dual; end;';
        execute immediate v_query using i, out v_value;
        -- now do some DML with v_value
      end loop;
    end loop;This works great :).
    Jure

  • How can i have '&' in EXECUTE IMMEDIATE statement?

    I have PL/SQL procedure, where i will be inserting data to a table. Data which i am inserting is having a '&' value in it. If i use the below statement for insert then sqlplus is prompting for the value '&' like a substitution value while compile this procedure.
    EXECUTE IMMEDIATE 'INSERT INTO TABLE_TEST VALUES (:1,:2)' USING 'root/direct/&text', SYSDATE;If i replace the '&' by assigning this value to VARCHAR field then it is working properly. But when i populate an xml from this table using DBMS.XMLGEN, then the value '&' is not entity escaped in the XML which is normally replaced with '&' in XML.
    Please help.

    Vel wrote:
    i can't go by both ways because i am using '&' in various files to get some values. Also in this package i am using the '&' to get another substitution variable. Is there any other workaround possible?I prefer not to mess with server-side SQL and PL/SQL code to fix, or work around, a problem on the client-side. Not a very sensible thing IMO.
    In such a case as you've described, I would set the substitution character to a character that I'm not using in server-side code. I have done this a couple of times in the past for db create and installation scripts. Oracle uses the same approach for their Apex installation scripts.
    I think Oracle uses the +#+ character for their Apex install scripts. E.g.
    set define '#'I think I used the tilde (char <i>~</i>) at a stage as substitution char as it is very seldom used in PL/SQL and SQL code.
    The bottom line is that you will need to change your code. Set define off and on where needed to ensure substitution only happens where needed. Set the substitution char to a different char and update your substitution variables. Or hack your server-side code (as suggested above) and use the char() function instead.

  • Error in execute immediate statement

    i am writing a stored procedure i am getting error
    v_Sql NVARCHAR2(1500);
    BEGIN
    v_Sql := 'Select * from vw_FillPatient Where 1 = 1 ';
    IF v_pVisitTypeID <> 0 THEN
    v_Sql := v_Sql || ' AND VisitTypeID = ' || CAST(v_pVisitTypeID AS VARCHAR2) || '';
    ELSE
    IF v_pVisitTypeID = 0 THEN
    v_Sql := v_Sql || ' AND VisitTypeID In (1,2,6)';
    END IF;
    END IF;
    v_Sql := v_Sql || ' AND VisitDate between ''' || TO_CHAR(v_pFromDate,'''DD_MON_YYYY''')
    || ''' and ''' || TO_CHAR(v_pToDate,'''DD-MON-YYYY''') || '''';
    v_Sql := v_Sql || ' ORDER BY VisitID ';
    EXECUTE IMMEDIATE v_Sql;-- error in this line as shown by SQL Developer
    end;
    error
    Error(64,22): PLS-00382: expression is of wrong type

    v_Sql NVARCHAR2(1500);
    BEGIN
      V_SQL := 'Select * from vw_FillPatient Where 1 = 1 ';
      IF V_PVISITTYPEID=0 THEN
        v_Sql := v_Sql || ' AND VisitTypeID = ''' || CAST(v_pVisitTypeID AS VARCHAR2) || '''';
      ELSE
        IF v_pVisitTypeID = 0 THEN
          v_Sql          := v_Sql || ' AND VisitTypeID In (1,2,6)';
        END IF;
      END IF;
      V_SQL :=
      V_SQL || ' AND VisitDate between  to_date(''' ||V_PFROMDATE||''',''DD-MON-YYYY'') and '
            ||  'to_date('''||v_pToDate||''',''DD-MON-YYYY'')';
      v_Sql := v_Sql || ' ORDER BY VisitID ';
      EXECUTE IMMEDIATE v_Sql;-- error in this line as shown by SQL Developer
    END;try it , please
    I think your V_PFROMDATE, and V_PTODATE is varchar
    elsif V_PFROMDATE and V_PTODATE id date then
    V_SQL || ' AND VisitDate between  to_date(''' ||to_char(V_PFROMDATE,'DD-MON-YYYY')||''',''DD-MON-YYYY'') and '
            ||  ' to_date('''||to_char(v_pToDate,'DD-MON-YYYY')||''',''DD-MON-YYYY'')';Edited by: Mahir M. Quluzade on Mar 1, 2011 11:15 AM
    Edited by: Mahir M. Quluzade on Mar 1, 2011 11:22 AM

  • Java Stored Procedure in EXECUTE IMMEDIATE

    Hi,
    I need advice for the following.
    I'm on Oracle 11g R2. I'm testing application in Oracle 11gR1 and R2 and Oracle Express.
    Purpose is to generate XML reports.
    I have PLSQL Stored Procedure which does that, but since there is bug in Oracle11gR2 related to XMLTRANSFORM I have and Java Stored Procedure which is workaround. They are both compiled, valid etc.
    Java class is :
    import java.io.PrintWriter;
    import java.io.Writer;
    import oracle.xml.parser.v2.DOMParser;
    import oracle.xml.parser.v2.XMLDocument;
    import oracle.xml.parser.v2.XSLProcessor;
    import oracle.xml.parser.v2.XSLStylesheet;
    * This class is used as Java stored procedure
    * There is a bug on Oracle11gR2, related to the limitation on the number of style sheet instructions
    * This stored procedure is workaround when PLSQL code can not be used.
    * File must not have package, otherwise is wrongly compiled in DB
    public class JavaXslt {
         public static void XMLTtransform(oracle.sql.CLOB xmlInput,oracle.sql.CLOB xslInput,oracle.sql.CLOB output) throws Exception{
              DOMParser parser;
              XMLDocument xml;
              XMLDocument xsldoc;
              try{
                   parser = new DOMParser();
                   parser.parse(xmlInput.getCharacterStream());
                   xml = parser.getDocument();
                   parser.parse(xslInput.getCharacterStream());
                   xsldoc = parser.getDocument();
                   XSLProcessor processor = new XSLProcessor();
                   XSLStylesheet xsl = processor.newXSLStylesheet(xsldoc);
                   Writer w = output.setCharacterStream(1L);
                   PrintWriter pw = new PrintWriter(w);
                   processor.processXSL(xsl, xml, pw);
              }catch (Exception ex){
                   throw ex;
    PROCEDURE Java_XmlTransform (xml CLOB, xslt CLOB, output CLOB) AS LANGUAGE JAVA
    NAME 'JavaXslt.XMLTtransform(oracle.sql.CLOB, oracle.sql.CLOB, oracle.sql.CLOB)';
    I'm calling Java stored procedure from PLSQL Stored procedure (if it is Oracle11gR2) like that :
    Java_Proc.Java_XmlTransform(inputXML, xslt, res);
    So till here everything works ok. XSLT as applied and output XML (res) is OK.
    But when Oracle Express is used Java is out of the question, so there is no Java stored procedure. Howewer PLSQL Stored procedure is still needed.
    So I had to put call to Java Stored procedure in EXECUTE IMMEDIATE statement in order to compile to PLSQL package.
    But when I do that :
    EXECUTE IMMEDIATE 'BEGIN Java_Proc.Java_XmlTransform (:1, :2, :3); END;' USING inputXML, xslt, res;
    result value CLOB (res) has zero length...
    What am I missing? Should i set return value to Java class?
    Hope my explanations are clear though.
    Thanks

    Hi odie_63,
    Thanks for quick response.
    I didn't clearly explained.
    When using Oracle 11gR1 and Oracle Express I'm using only PLSQL Procedure.
    When using Oracle 11gR2 i have to use Java Stored procedure because there is documented bug in R2.
    That's why i have to use EXECUTE IMMEDIATE. I don't know which version is the client DB and whether there is or no Java procedures.
    I did tried
    EXECUTE IMMEDIATE 'BEGIN Java_Proc.Java_XmlTransform (:1, :2, :3); END;' USING IN inputXML, IN xslt, OUT res; and the result was ORA-06537: OUT bind variable bound to an IN position
    When using IN OUT for last parameter i.e.
    EXECUTE IMMEDIATE 'BEGIN Java_Proc.Java_XmlTransform (:1, :2, :3); END;' USING IN inputXML, IN xslt, IN OUT res;
    there is no exception, but still DBMS_LOB.getlength(res) = 0
    Thanks

  • How to execute a statement in forms procedure like SQL EXECUTE IMMEDIATE

    Hi to all,
    In a form I have created this procedure:
    PROCEDURE insert_rows (
    tbName IN VARCHAR2,
    list_of_fields IN VARCHAR2,
    origin_table IN VARCHAR2,
    wCondition IN VARCHAR2 DEFAULT NULL)
    IS
    where_clause VARCHAR2 (2000) := ' WHERE ' || wCondition ;
    table_to_fill VARCHAR2 (30);
    BEGIN
    -- Exist the table ?
    SELECT OBJECT_NAME INTO table_to_fill FROM USER_OBJECTS
    WHERE OBJECT_NAME = UPPER(origin_table) AND OBJECT_TYPE = 'TABLE' ;
    IF wCondition IS NULL THEN
    where_clause := NULL;
    END IF ;
    EXECUTE IMMEDIATE 'INSERT INTO ' || table_to_fill || ' SELECT ' || list_of_fields || ' FROM ' || origin_table || where_clause ;
    EXCEPTION
    WHEN NO_DATA_FOUND THEN
    -- Here the Alert
    END;
    But, when I compile this error is displayed:
    Function not supported from client side application corresponding to SQL statement EXECUTE IMMEDIATE
    How can to correct this script for my form ?
    I hope in Your help.
    Best Regards
    Gaetano

    You have two options:
    1)To create this procedure in database
    2)Yo use the forms built-in FORMS_DDL instead of execute immediate , altering the one provided
    Sim

  • Problem wile EXECUTE IMMEDIATE DDL statement in procedure

    Hi ,
    This is my procedure and it's getting compiled but while executing procedure getting this error,
    can anyone please tell me how to fix this?
    create or replace procedure construct_Table (name_table IN VARCHAR2)
    IS
    v_tab_name varchar2(40):=NULL;
    v_sql_Stmt varchar2(32767) := NULL;
    finalquery varchar2(32767) :=NULL;
    cursor tp is
    select COLUMN_NAME,DATA_TYPE,DATA_PRECISION,CHAR_LENGTH  from all_tab_cols where table_name=name_table;
    BEGIN
    begin
    select TABLE_NAME into v_tab_name from user_tables where table_name=name_table;
    EXCEPTION
    WHEN no_Data_found
    THEN
    DBMS_OUTPUT.PUT_LINE('No such table exist');
    end;
    if(v_tab_name IS NOT NULL)then
    finalquery := 'CREATE TABLE '||v_tab_name||'_DUMMY (';
       FOR I IN tp LOOP
       if(I.data_type='VARCHAR2') then
       v_sql_stmt := finalquery ||I.column_name||' '||I.data_type||'('||I.char_length||') ';
       elsif(I.data_type='NUMBER') then
       v_sql_stmt := finalquery ||I.column_name||' '||I.data_type||'('||I.DATA_PRECISION ||') ';
       else
       v_sql_stmt := finalquery ||I.column_name||' '||I.data_type ;
       end if;
       finalquery := v_sql_stmt || ',';
       END LOOP;
       finalquery := SUBSTR(finalquery,1,LENGTH(finalquery) - 1)||')';
       dbms_output.put_line(finalquery);
       EXECUTE IMMEDIATE'grant create any table to cmsuser';
       EXECUTE IMMEDIATE finalquery;
    end if; 
    END;
    /This is the error I am getting
    Error starting at line 1 in command:
    begin
    construct_Table ('EMP');
    end;
    Error report:
    ORA-01031: insufficient privileges
    ORA-06512: at "CMSUSER.CONSTRUCT_TABLE", line 30
    ORA-06512: at line 2
    01031. 00000 -  "insufficient privileges"
    *Cause:    An attempt was made to change the current username or password
               without the appropriate privilege. This error also occurs if
               attempting to install a database without the necessary operating
               system privileges.
               When Trusted Oracle is configure in DBMS MAC, this error may occur
               if the user was granted the necessary privilege at a higher label
               than the current login.
    *Action:   Ask the database administrator to perform the operation or grant
               the required privileges.
               For Trusted Oracle users getting this error although granted the
               the appropriate privilege at a higher label, ask the database
               administrator to regrant the privilege at the appropriate label.Thanks ,
    Deekay.

    Deekay,
    If you grant create table privilege and create table in the same procedure, then how you will differentiate that which user you granted the privilege and in which schema, you are creating the table. Here, you are granting to "cmuser", but in the same schema, you are creating the table also. How can a user grant privilege to himself?
    Login as DBA, grant create any table privilege to "cmuser" from dba. Then, you can execute you procedure in "cmuser" schema.

  • Execute immediate -- udpate statement

    Hello,
    I am fairly new to scripting sql..pl/sql.. any help will be really appreciated!!
    I have a table where i am trying to update one of the column with table name i am passing dynamically.
    However i get the invalid identifier error. ORA-00904. The column AD.MIR_PARENT i am updating is of compatible data type.
    Any help please.. where i am doing a mistake..
    fyi -- the reason i am updating the column with table name is because i need to know which table i make a hit.
    DECLARE
    p_table varchar2(200);
    P_SCHEMA varchar2(200);
    p_acct NUMBER;
    v_acct NUMBER;
    P_column varchar2(200);
    cursoR c is
    select schema_name, table_name, column_name from addnl_sources where TABLE_NAME = 'ACCT';
        BEGIN
            FOR R IN C LOOP
                                    p_TABLE := R.TABLE_NAME;
                                    P_SCHEMA := R.SCHEMA_NAME;
                                    P_column := R.column_name;
                       execute immediate 'UPDATE ACCT_DELETE AD
                SET AD.MIR_PARENT = '||p_table || '
                WHERE EXISTS (select "'||p_table || '" from "' || P_SCHEMA||'"."'||p_table || '" where "' || P_column ||'" = ad.acct_id )
                AND AD.EXCLUDE IS NULL
                AND AD.ACCT_ID = 472039
                AND AD.MIR_PARENT IS NULL' ;
                            END LOOP;
        end;Edited by: 982806 on Jan 18, 2013 10:47 AM
    Edited by: 982806 on Jan 18, 2013 11:04 AM

    Welcome to the forum!
    Whenever you post provide your 4 digit Oracle version (result of SELECT * FROM V$VERSION)
    >
    I am fairly new to scripting sql..pl/sql.. any help will be really appreciated!!
    I have a table where i am trying to update one of the column with table name i am passing dynamically.
    However i get the invalid identifier error. ORA-00904. The column AD.MIR_PARENT i am updating is of compatible data type.
    Any help please.. where i am doing a mistake..
    >
    Just about everything you are doing is a mistake.
    1. You using PL/SQL instead of SQL to do this job? That is usually a red flag that something is wrong about what you are trying to do or how you are trying to do it.
    2. You are 'fairly new to scripting' and have chosen to use dynamic sql? That is one of the harder techniques to learn and to use properly and is another red flag. Experienced professionals often don't use dynamic sql properly and often use it when they shouldn't.
    3. You are using a loop to execute against multiple tables? It is extremely difficult to perform proper error handling and recovery when you do this. What if some of the tables get updated and others don't? How will you know which ones still need to be updated? How will you roll back the data if you need to.
    4. You have opened a GIGANTIC opportunity for sql injection where the wrong tables could accidentally be updated. That could corrupt the data and reek havoc on the system. Anyone could, accidentally or intentionally, put the wrong schema, table or column in your lookup table and cause enormous damage.
    5. You are using double quotes in PL/SQL? Where did you learn that?
                WHERE EXISTS (select "'||p_table || '" from "' || P_SCHEMA||'"."'||p_table || '" where "' || P_column ||'" = ad.acct_id ) Finally, it is standard practice when you do have a use case for dynamic sql to construct the query in a variable and then print the query or write it to a log file so it can be tested independently.
    If you do that you will likely see your error.
    query VARCHAR2(4000);
    query := 'UPDATE . . .';
    execute immediate query;SUMMARY: don't try to automate something until you can first do it manually. Write a query that works properly first. Then, and only then, convert it to dynamic sql.

  • Execute Immediate with SQL in Stored Procedure

    Dear Experts, Please help / guide on below statement which i want to execute in stored procedure,
    EXECUTE IMMEDIATE 'INSERT INTO TABLE
    SELECT COUNT(AGENTID) FROM TABLE_1 A,
    TABLE_2 B, TABLE_3 C
    WHERE A.COL = B.COL AND B.COL = C.COL AND
    DT BETWEEN TRUNC(SYSDATE-1) + 1/24 + 0/12/60/60 AND TRUNC(SYSDATE-1) + 25/24 - 0/12/60/60';
    Edited by: DBA on Dec 3, 2011 3:57 PM

    So what's the problem? The only thing I can see is:
    INSERT INTO TABLETABLE is reserved word, so name your table differently.
    Also, I am assuming the above statement is actually generated from some variables based on some conditions, otherwise why would you need dynamic SQL, just issue:
    INSERT INTO TABLE
    SELECT COUNT(AGENTID) FROM TABLE_1 A,
    TABLE_2 B, TABLE_3 C
    WHERE A.COL = B.COL AND B.COL = C.COL AND
    DT BETWEEN TRUNC(SYSDATE-1) + 1/24 + 0/12/60/60 AND TRUNC(SYSDATE-1) + 25/24 - 0/12/60/60;

  • EXECUTE IMMEDIATE dynamic statement and return procedure value

    Hello guys,
    How can i return values from procedure using EXECUTE IMMEDIATE statment?
    I made easy example:
    CREATE OR REPLACE PACKAGE pac_test
    IS
    PROCEDURE pro_calc_average( p_age_1 IN NUMBER,
    p_age_2 IN NUMBER,
    p_age_3 IN NUMBER,
    v_out OUT NUMBER);
    PROCEDURE pro_calc(p_age_1 IN NUMBER,
    p_age_2 IN NUMBER,
    p_age_3 IN NUMBER);
    END;
    CREATE OR REPLACE PACKAGE BODY pac_test
    IS
    PROCEDURE pro_calc_average( p_age_1 IN NUMBER,
    p_age_2 IN NUMBER,
    p_age_3 IN NUMBER,
    v_out OUT NUMBER)
    IS
    BEGIN
    v_out:=(p_age_1+p_age_2+p_age_3)/3;
    END pro_calc_average;
    PROCEDURE pro_calc(p_age_1 IN NUMBER,
    p_age_2 IN NUMBER,
    p_age_3 IN NUMBER)
    IS
    x number;
    v_sql varchar2(4000);
    BEGIN
    v_sql:='pac_test.pro_calc_average('||p_age_1||','||p_age_2||','||p_age_3||',x)';
    dbms_output.put_line(v_sql);
    EXECUTE IMMEDIATE v_sql;
    dbms_output.put_line(' ====> '||x);
    END pro_calc;
    END;
    -- Run procedures [Faild]
    EXEC pac_test.pro_calc(2,9,19);
    When i run:
    DECLARE
    x number;
    BEGIN
    pac_test.pro_calc_average(2,9,9,x);
    dbms_output.put_line(' ====> '||x);
    END;
    It's works, but this is not what i am looking for.
    Thank you guys,
    Sam.

    Hi Sam,
    Like this?
    CREATE OR REPLACE PACKAGE pac_test
    IS
    pac_var number;  /* added new*/
    PROCEDURE pro_calc_average( p_age_1 IN NUMBER,
    p_age_2 IN NUMBER,
    p_age_3 IN NUMBER,
    v_out OUT NUMBER);
    PROCEDURE pro_calc(p_age_1 IN NUMBER,
    p_age_2 IN NUMBER,
    p_age_3 IN NUMBER);
    END;
    CREATE OR REPLACE PACKAGE BODY pac_test
    IS
    PROCEDURE pro_calc_average( p_age_1 IN NUMBER,
    p_age_2 IN NUMBER,
    p_age_3 IN NUMBER,
    v_out OUT NUMBER)
    IS
    BEGIN
    v_out:=(p_age_1+p_age_2+p_age_3)/3;
    END pro_calc_average;
    PROCEDURE pro_calc(p_age_1 IN NUMBER,
    p_age_2 IN NUMBER,
    p_age_3 IN NUMBER)
    IS
    pack_local_var number;
    v_sql varchar2(4000);
    BEGIN
    --v_sql:='pac_test.pro_calc_average('||p_age_1||','||p_age_2||','||p_age_3||',x)';
    v_sql:=' declare x number; begin pac_test.pro_calc_average('||p_age_1||','||p_age_2||','||p_age_3||',x);
    dbms_output.put_line(x);
    pac_test.pac_var:=x; /* added new*/
    end;';
    dbms_output.put_line(v_sql);
    EXECUTE IMMEDIATE v_sql;
    pack_local_var:=pac_var; /* added new*/
    dbms_output.put_line(pack_local_var); /* added new*/
    END pro_calc;
    END;Declared a package variable.
    But be aware this variable is accessible to everyone and they can read or change its value if they have the right privileges.
    REgards,
    Bhushan

  • How can i pass multiple values by a single variable to EXECUTE IMMEDIATE

    Hi All,
    I want to pass multiple values for where condition for execute immediate. Something Like this:-
    bold
    Declare
    v_cond varchar(1000);
    Begin
    v_cond := '''INR','USD'''; --(OPTION 1)
    v_cond := 'INR,USD'; --(OPTION 2)
    EXECUTE IMMEDIATE 'Delete from table where colm in (:v_cond)' using v_cond;
    END;
    bold
    I am using this into a procedure
    Now option 1 gives an error ie a syntax error (; expected or something like that)(I am sorry, i can't tell the exact error here as i am not in the office right now)
    and option 2 makes the procedure execute but obviously doesn't delete the records, as it takes the whole as one.
    Please Help
    Regards
    Neeraj Bansal

    See the links containing examples under
    *7. List of values in an IN clause?*
    SQL and PL/SQL FAQ
    from the SQL and PL/SQL FAQ.

  • Execute immediate in pl/sql block not functioning

    Hello,
    I've made a short plsql block which create's a bind variable on an dynamic select. when i try this in sqplus with a dbms_output.put_line(v_temp) the correct value is displayed but within the execute immediate commando the bind variable is not filled. Here's the code:
    DECLARE
    v_count number(5) := 1;
    v_text varchar2(200);
    v_temp varchar2(200);
    BEGIN
    select count(*) into v_count from v_survey_questions where page_id=2;
    for c1 in 1..v_count
    loop
    v_temp:='select text into :P2_Q'||c1||' from v_survey_questions where page_id=2 and ord='||c1||'';
    execute immediate v_temp;
    end loop;
    END;
    Any help would be appreciated.
    Thanks Leo.

    Hi Vikas,
    Thanks but i've tried that. The thing is that the bind variable :P2_Q1 is dynamic generated like this:
    DECLARE
    v_count number(5) := 1;
    v_temp varchar2(2000);
    v_temp2 varchar2(200);
    BEGIN
    select count(*) into v_count from v_survey_questions where page_id=2;
    for c1 in 1..v_count
    loop
    v_temp:='select text from v_survey_questions where page_id=2 and ord=1';
    v_temp2:=':P2_Q'||c1;
    execute immediate v_temp into v_temp2;
    end loop;
    END;
    Hope you can help me out.
    Greetings,
    Leo.

Maybe you are looking for

  • Itunes is saying it cannot connect to this iphone but I have a touch.

    I am a dance teacher and trying to sync up my ipod touch to my PC to get my new songs on it and make some changes to my itunes. Itunes will not allow the sync because of error 0XE8000065 which states itunes could not connect to this iphone.  I have b

  • Desc doesn't show datetime columns from SQl Server

    Hi Everyone, I have working db link to sql server using hsodbc: Oracle 10.2 on AIX using freetds odbc driver version 0.82. I can access tables on sql server but oracle sees only varchars and numerics columns in a table. Is there a way to select datet

  • Installed Leopard - Installed Windows XP - Restart Problems

    I decided to upgrade to Leopard last night and I'm beginning to think, for the first time ever for Apple, that this was a mistake. I got the blue screen issue probably due to Application Enhancer, so I Archive and Install. Then decided after I couldn

  • How do I undo to short in my memo?

    Hi I went to the concert of Ed Sheeran yesterday and I recorded the whole thing on my Voice recorder. I was listening to it today and I wanted to send it to my e-mail but the file was too long so I had to short it in. So wha happened is that it short

  • Authorizing on replacement PC

    My computer crashed and I'm setting up a new one. ADE won't let me authorize my new PC because my ID has been previously used (on the computer that crashed.) I don't want to establish a new ID ... prefer to keep using my primary ID. How do I get ADE