DDL Statements in a Stored Procedure

Is there some technique which can be used to execute DDL statements in a stored procedure? I want to drop and recreate a table and a number of indexes. Is there a way for me to do this within a stored procedure?
Thanks in advance for your assistance!!!

BEGIN
   EXECUTE IMMEDIATE 'CREATE TABLE bonus (id NUMBER, amt NUMBER)';
END;http://download-west.oracle.com/docs/cd/B10501_01/appdev.920/a96624/13_elems18.htm#33889

Similar Messages

  • DDL statement/s in stored procedures (FUNCTION)

    Hello all,
    I was trying to create a small function that tests keywords on the basis of successful/failed table creation. If a table can be created with a keyword as identifier, it is a non-reserved keyword, if table creation fails, it is a reserved keyword:
    CREATE OR REPLACE FUNCTION createTableForKeyword (keyword VARCHAR) RETURN BOOLEAN IS
    BEGIN
    CREATE TABLE keyword (x NUMBER);
    -- if no exception occurred, table creation succeeded
    DROP TABLE keyword; drop it
    RETURN FALSE; -- FALSE means non-reserved
    EXCEPTION
    WHEN OTHERS THEN
    -- if exception occurred, table creation failed
    RETURN TRUE; -- TRUE means reserved
    END createTableForKeyword;
    This would have been my first PL/SQL program, but I get the following error:
    PLS-00103: Found symbol "CREATE" when expecting one of:
    begin case declare exit ................
    .............. merge pipe
    I had to translate the error message from German, but it should suffice. Obviously DDL statements in functions are not allowed. How do I solve my problem then, given a table that has one column containing the keyword?:
    CREATE TABLE Keywords (keyword VARCHAR(30));
    I was looking too call this function from a loop, but how would I do this without a function? How do I capture table creation fails without exceptions in functions?
    TIA
    Karsten

    To run DDL inside stored procedure you must use Dynamic SQL. Use EXECUTE IMMEDIATE
    To see if a word is a reserved word just query V$RESERVED_WORDS
    Thanks,
    Karthick.

  • JDBC/select/async statement to JDBC/stored procedure/sync call

    Hi
    We have JDBC/select/async statement to JDBC/stored procedure/sync call i.e sender and receiver are JDBC.
    PI has to pick all the the records of single internal order number at a time from sender system and upload to receiver JDBc,
    gets the response and routes to sender/insert statement.
    This should run only once per day.
    We will have multiple Internal orders daily, each order consisting of 10 to 20 records but only one IO related records has
    to upload to Receiver/JDBC
    What are the options available ?
    We have thought of following options
    1. SQL query is already to pick, but we have to pick records at one time daily. example: morning,evening or midnight.
       At that time it can pick multiple times but it should not pick through out day
    2. Is there any option in BPM so that we can group IO's at a time and upload ? If so what are the steps need to use
       Any additonal receive step need to be used to pick the records from the table.
    Thanks

    hi
    as i can understando you, you will receive mani IO and you must execute one IO in the receiver SP? if so, you can solve this usssing a ccBPM where you will have to create a mapping(0.N) where the source and the target structure will be the same, the diferrence will be in the occurrance of the target structure which will have to be 0.N (Tab signature in Message Mapping). then back to the ccBPM define a block with the property ForEach. this will  loop any times accord with the number of IO that you receive from the sender. as a result you will execute one SP for each IO.
    so, you ccBPM will be
    RS>TS>BLOCK(Multiline container and single container of source structure)>TS->SS
    RS:Receive Step
    TS:Trans. Step
    SS:Send Step
    Also the container will be:
    source--> type Abs
    source_multiline --> type Abs
    target -->type Abs
    Thanks
    Rodrigo P.
    Edited by: Rodrigo Alejandro Pertierra on Jun 24, 2010 4:54 PM

  • Writing mulitple sql statements in 1 stored procedure

    Hi all, can i know how to create mulitple sql statements in 1 stored procedure??
    Eg the first sql statement will generate few results and my second sql statement will based on the first statement result to execute its second results and my third sql statements will on the second results to generate the final results which will be passed back to jsp pages as a resultset??
    For the time being, i only know how to create a single sql statement in one stored procedure..i had surf through the oracle website but cant find any solution. Can anyone help me?? Samples or links to any website will do.. Thanks alot...

    Hi Irene,
    If I understand your question correctly, then I have already written
    a similar (PL/SQL) stored procedure without any problems.
    However, I do think your question is more suited to the following
    forum:
    http://forums.oracle.com/forums/forum.jsp?id=478021
    I also think it will help others to answer your question if you
    include the following information:
    1. Version of Oracle you are using.
    2. The error message you are getting.
    3. The part of your code that is causing the problem.
    Also, have you looked at the following web sites?
    http://asktom.oracle.com
    http://metalink.oracle.com
    Good Luck,
    Avi.

  • Truncate statement in a stored procedure

    How can I execute a DDL statement, TRUNCATE TABLE table_name, from a stored procedure. In fact, I have tried the following:
    execute immediate ' truncate table table_name ';
    but it doesn't work. Do you have any ideas.
    The Oracle version is 7.3.
    Thanks in advance.

    The syntax for using dbms_sql is
    DECLARE
    v_cur INTEGER;
    v_ret INTEGER;
    BEGIN
    v_cur := DBMS_SQL.OPEN_CURSOR;
    DBMS_SQL.PARSE(v_cur, 'TRUNCATE TABLE your_tab', dbms_sql.v7);
    v_ret := DBMS_SQL.EXECUTE(v_cur);
    DBMS_SQL.CLOSE_CURSOR(v_cur);
    END;
    It's usually worth puting this in a stored procedure and passing the DDL statement in as a parameter, saves you having to type all this out everytime.
    If you don't have the dbms_sql package installed, the scripts are usually found under rdbms/admin

  • Rollback of JDBC Statement on error - Stored Procedure

    Hi All,
    We are using the JBDC receiver adapter for inserting the record and for that we are using the Strored Procedure and we have N number of the records for insertion in a single mapping so, we have given the occurence of the Statement 1 : unbounded as, multiple statement will be executed so, my query is if one of statement fails because of some error(Mapping error or data error) what will happen to the statement which are already executed, do they rollback? or it is not possible in case of stored procedure?
    Regards,
    Gaurav

    Hi,
    This is one of the solution but i guess there is one more issue, if we have 10 statements and third statement shows error and Stored Exception exception branch is able to catch then it will rollback the previous statement but can we able to restrict another 7 pending statement for execution or not?
    Regards,
    Gaurav

  • Callable statement with oracle stored procedure error

    i'm calling a stored procedure in java with the following code. However i constantly recieve this error
    so what is going on please HELP
    Parameter Type Conflict: sqlType=2006
    my call statement would be this:
    call Statement = {call getUserByLogin(?,?,?,?)}
    if(storedProcedureName=="getUserByLogin"){
    strCStmt = ("{call " + storedProcedureName +"(?,?,?,?) }");
    cStmt.setObject(1, "system");
    cStmt.setObject(2, "username");
    cStmt.setObject(3,"password");
    cStmt.registerOutParameter(4, java.sql.Types.REF);
    rs = cStmt.executeQuery();
    i've also tried it with a setString as the IN parameter:
    here's the stored procedure:
    CREATE OR REPLACE PROCEDURE getUserByLogin (
    arg_subscriptionName IN varchar,
    arg_loginName IN varchar,
    arg_password IN varchar,
    arg_rec_userinfo_valLanguage OUT types.rec_userinfo_valLanguage
    ) AS
    var_userNum int;
    BEGIN
    select
    u.userNum into var_userNum
    from
    userInfo u,
    subscription s
    where
    s.subscriptionName = arg_subscriptionName AND
    s.subscriptionNum = u.subscriptionNum AND
    u.loginName = arg_loginName AND
    u.password = arg_password;
    if (var_userNum is null) then
    var_userNum := 0;
    end if;
    getUser(var_userNum, arg_rec_userinfo_valLanguage);
    END;

    i'm calling a stored procedure in java with the
    following code. However i constantly recieve this
    error
    so what is going on please HELP
    Parameter Type Conflict: sqlType=2006
    my call statement would be this:
    call Statement = {call getUserByLogin(?,?,?,?)}
    if(storedProcedureName=="getUserByLogin"){
    strCStmt = ("{call " + storedProcedureName +"(?,?,?,?)
    cStmt.setObject(1, "system");
    cStmt.setObject(2, "username");
    cStmt.setObject(3,"password");
    cStmt.registerOutParameter(4, java.sql.Types.REF);
    rs = cStmt.executeQuery();
    i've also tried it with a setString as the IN
    parameter:
    here's the stored procedure:
    CREATE OR REPLACE PROCEDURE getUserByLogin (
    arg_subscriptionName IN varchar,
    arg_loginName IN varchar,
    arg_password IN varchar,
    arg_rec_userinfo_valLanguage OUT
    types.rec_userinfo_valLanguage
    ) AS
    var_userNum int;
    BEGIN
    select
    u.userNum into var_userNum
    from
    userInfo u,
    subscription s
    where
    s.subscriptionName = arg_subscriptionName AND
    s.subscriptionNum = u.subscriptionNum AND
    u.loginName = arg_loginName AND
    u.password = arg_password;
    if (var_userNum is null) then
    var_userNum := 0;
    end if;
    getUser(var_userNum, arg_rec_userinfo_valLanguage);
    END;
    /Hai,
    Try with this if u are using Oracle.
    import oracle.sql.*;
    import oracle.jdbc.driver.*;
    (inbetween ur code)
    cStmt.registerOutParameter(4, OracleTypes.CURSOR);
    //in place of "cStmt.registerOutParameter(4, java.sql.Types.REF);"
    Hope u reply with joy.
    regards,
    Siva Kumar Annavaram

  • Create "dynamic" statements in a stored procedure

    hello
    in my stored procedure I need to write a statement like this:
    FUNCTION myfunct (mytable IN VARCHAR2)
    BEGIN
    EXECUTE IMMEDIATE
    'SELECT COUNT(*)
    INTO myvar
    FROM ' || mytable ||
    'WHERE mycol = ' || currval;
    In short I need to create a sort of "dynamic" statement in which the tablename (nad other...) is a variable....
    but I do not guess it's ok...
    How can I do?

    Hi,
    With dyanmic SQL, the INTO clause is part of the EXECUTE IMMEDIATE statement, not the query.
    Try something like this:
    CREATE OR REPLACE FUNCTION     myfunct
    (       mytable       IN     VARCHAR2
    RETURN     PLS_INTEGER
    IS
         return_num     PLS_INTEGER;
         sql_txt          VARCHAR2 (1000);
    BEGIN
         sql_txt := 'SELECT  COUNT (*)'
              || ' FROM ' || mytable;
         dbms_output.put_line (sql_txt || ' = sql_txt in myfunct');
    --     EXECUTE IMMEDIATE sql_txt INTO return_num;
         RETURN     return_num;
    END     myfunct
    /It's a good idea to develop dynamic SQL as shown above; putting the dynamic statement into a variable that can easily be displayed for debugging.
    When it looks right, then un-comment the EXECUTE IMMEDAITE statement.
    Before moving the code into Production, comment out (or remove) the put_line statement.

  • Extraction SQL statement from oracle stored procedure stored in file

    Hi,
    I am newbie to oracle stored procedure. I need to extract the list of sqls present in oracle stored procedure. Besides that I also want to parse these sql statements to get the list of tables and columns used. Is there any tool which can help me in doing thats.
    thanks,
    govind

    why don't check on user_dependencies table instead?
    select referenced_name,referenced_type
    from user_dependencies
    where name='<your stored procedure name in upper case>'
    and referenced_type = 'TABLE'HTH,
    Prazy

  • Extracting the DDL(Code) of only stored procedures

    DB Version:11g
    I have a list of Stored procedures (about 250) whose code ie. CREATE OR REPLACE STORED PROCEDURE statements i want to extract to a file.
    Is there an option in imp or impdp where i can extract only the code of these stored procedures?
    Apart from imp/impdp , is there any other way to do this?

    Is there an option in imp or impdp where i can extract only the code of these stored procedures?impdp, Yes. Write the contents to a file using, SQLFILE option.
    DUMPFILE = exp.dmp
    LOGFILE = exp.log
    SCHEMAS=TEST
    INCLUDE= PROCEDURE:"IN ('SP')"
    SQLFILE=sp..sql
    Or use dbms_metadata.get_ddl
    set long 99999
    set pagesize 0
    SELECT DBMS_METADATA.GET_DDL(object_type, object_name, owner) FROM DBA_OBJECTS WHERE OBJECT_TYPE = 'PROCEDURE' and OWNER='OWNER';HTH
    -Anantha

  • Got error when use case statement in oracle stored procedure

    Hi,
    I have a query like:
    select merchant_id,
    case
    when product_type='K' then 'Production'
    when product_carrier='UC' THEN 'Shipping'
    end the_type
    from product_tbl
    where merchant_id=10114
    It works fine. But as soon as I put it into a stored procedure, I got error like:
    Encountered the symbol "CASE" when expecting one of the following:
    ( - + mod null <an identifier>
    Please help!

    Oracle 8i doesn't support CASE into PL/SQL, so as Kamal said, create a view with the CASE, and use this view into your PL/SQL.
    Nicolas.
    And an example here :
    Re: Execute Immediate doesnot work in 8i (8.1.7) and Ref Cursor not exec qu
    Sorry Kamal.
    Message was edited by:
    N. Gasparotto

  • User can Execute SQL Statement but not Stored Procedure

    I have a function in Access that calls a stored procedure to update a table. When I run it, it works fine but when the users try to run it, they get an error.
    If I change it run the actual SQL syntax that is in the stored procedure then the users can run it and update the table without any problems, which makes no sense to me. It's doing the same exact thing as the stored procedure. I'd much rather have them be
    able to run the procedures then writing the SQL in VBA modules because that's going to end up being a lot of code.
    Any idea on why it's like this and how to correct it? Any assistance would be appreciated.

    Hello,
    When you give a user permission to run a stored procedure, everything on that procedure but Dynamic SQL will be executed
    without evaluating user permissions on the objects the stored procedure deals with.
    On the link I provided above, you will see how to provide permissions to stored procedures. Try creating database roles, add user to database roles, then assign permissions to database roles.
    Hope this helps.
    Regards,
    Alberto Morillo
    SQLCoffee.com

  • Fails to execute all statements in a stored procedure

    We are executing a stored proc on SQL Server 7.0. We are using the jdbc odbc bridge. The procedure has database inserts on seven tables.
    It executes fine when it's executed from the SQL server query analyzer. All the inserts go through without any problem.
    We have a problem when the proc is executed from the front-end. All the inserts except the last two go through without any problem. The sixth insert is partially completed (two out of 300 rows are inserted) and the seventh one is not at all executed.
    No error messages are generated. Surprisingly the stored proc executes fine when executed from the front-end from a Win 98 machine. It's giving a problem only on Win 2000. Could somebody please help us in finding a fix for this problem. Why is it behaving differently when executed from the front-end on a Win 2000 mahinne.
    Thanks in advance.

    Why is it behaving differently when executed from the front-end on a Win 2000 mahinne. Possible reasons.
    -Because you aren't sending the same data.
    -It is has an internal error and that is being ignored.
    -It isn't going to the same database and the stored proc is actually different.

  • Unable to execute EXECUTE IMMEDIATE statement in a stored procedure...

    Hi People,
    I use oracle 10g.In which i tried a procedure with follwing code and am thrown with the error that says,
    SQL>   create or replace procedure ptable(ptab varchar2) is
      2  lstmt varchar(200);
      3  begin
      4  lstmt:='CREATE TABLE '||ptab||'(a int,b varchar2(30)) ';
      5  execute immediate lstmt;
      6  end;
      7  /
    Procedure created.
    SQL>  exec ptable('hos');
    BEGIN ptable('hos'); END;
    ERROR at line 1:
    ORA-01031: insufficient privileges
    ORA-06512: at "VIDYA.PTABLE", line 5
    ORA-06512: at line 1Procedure has been created but error occurs while executing it.why do i get this error even though i possess DBA privilege?.which permission is lagging for me?.help me with ur suggestions.thanks in advance.
    With Regards
    VIDS

    Yep Yep.Perfect.It was the CREATE ANY TABLE privilege which i was lagging.I granted it to my schema and now got created with the table.Thanku so much for such a quick reply :).
    With Regards
    VIDS

  • A query in Stored Procedure

    Hi ,
    I am using EXECUTE IMMEDIATE to execute a DDL Statement inside a Stored Procedure .
    But i am unable to see this table , please tell me why .
    create or replace procedure NAmes
    is
    begin
    EXECUTE IMMEDIATE 'CREATE TABLE execute_table (col1 VARCHAR(10))';
    commit;
    end;
    Procedure created.

    Hi,
    user10503747 wrote:
    I am using EXECUTE IMMEDIATE to execute a DDL Statement inside a Stored Procedure .Why? Performing DDL in strored procedures is usually not the best way to do things in Oracle.
    Whenever you have a problem, post a compete test script that people can use to re-create the problem and test their ideas. The CREATE TABLE statement that you posted was a good start; add code to show how you ran it, and what you did to see if it worked or not.
    For example:
    create or replace procedure NAmes
    is
    begin
        EXECUTE IMMEDIATE 'CREATE TABLE execute_table (col1 VARCHAR(10))';
        commit;   -- Not needed.  DDL automatically causes COMMIT
    end;
    SHOW ERRORS
    PROMPT     =====  Before Running Names Procedure  =====
    DESCRIBE     execute_table
    EXEC  names;
    PROMPT     =====  After Running Names Procedure  =====
    DESCRIBE     execute_tableThis worked fine for me in Oracle 10.2.0.1.0 and SQL*Plus 10.2. Output:
    Procedure created.
    No errors.
    =====  Before Running Names Procedure  =====
    ERROR:
    ORA-04043: object execute_table does not exist
    PL/SQL procedure successfully completed.
    =====  After Running Names Procedure  =====
    Name                                      Null?    Type
    COL1                                               VARCHAR2(10)What are you doing different?
    Did you get any error messages?
    Are you creating the procedure in one schema, and running it in another?
    Did you call the names procedure from another procedure?
    Don't make people guess. Guessing is not the best way to solve problems.

Maybe you are looking for

  • Can I, and if so how do I updgrade my PowerBook G4 from OSX 10.5 to 10.5.6 or higher?

    I purchased and installed OS X 10.5 to my Powerbook PPC G4 this evening without any trouble. It did take me a little work to figure out how to get iTunes/Quicktime upgraded and working properly but the ASC had some great answers. My problem now is up

  • After clean install cannot print to pdf with Acrobat 8 Pro

    I recently had to do a clean install on my desktop computer.  Is there some way I can print to pdf with my Adobe Acrobat 8 Pro, which was working fine prior to the clean install.  I am aware Adobe no longer supports this version, but isn't this progr

  • G5 IMAC STARTUP FAILURE

    When I try to turn on my Imac, several things have happened or can happen. Grey screen comes up, loud "clicking" sound, fan kicks in, then screen either goes grey, or blue with alternating folders (question mark/os folder), or I get the white screen

  • Power View with dynamic connection string, but server-side

    We'd like to evaluate using Power View to provide exploratory BI over data output from HDInsight, onto Azure Storage. We need to let users select which segments of data to load but through a web-ui (not Excel on the desktop). We expect to have a stan

  • IPhoto not launching email when sending photos

    After I select photos to email in iPhoto, I hit the email icon and a new email screen doesnt pop up anymore. It used to launch it correctly w/ the photos attached but it has stopped. Does anyone have any suggestions?