Execute DDL statements from pl / sql

Hi !!!!
I have to create every object (tables, sequences, etc) using a pl / sql block, so I need first looking for the object in the data dictionary and if it doesn't exist execute a ddl statement to create it.
I wanted to execute at the same "execute immediate " :
- create table.
- create synonym for the table created before.
- comments on table
- comments on table's columns
- grants on the table.
But I found some errors:
- I can`t have semicolon after every statement so I get an "ORA-00911: invalid character". I did'nt try to scape the semicolon so I need it.
- I split the execute immediate so now I have one execute immediate for every statement but I got an error in this statement:
EXECUTE IMMEDIATE 'COMMENT ON TABLE SYSADM.CVP_PAQ_SMS_REC_PRUEBA IS "COMMENT ON TABLE"  '; The error I get is this:
ORA-01780: string literal required
Any suggestions about this ?
Thanks.

The comment itself needs to be a SQL string so it needs to be delimited with single quotes not double quotes. You can either use two single quotes
EXECUTE IMMEDIATE 'COMMENT ON TABLE SYSADM.CVP_PAQ_SMS_REC_PRUEBA IS ''COMMENT ON TABLE''  ';or you can use the q quoting syntax
EXECUTE IMMEDIATE q'[COMMENT ON TABLE SYSADM.CVP_PAQ_SMS_REC_PRUEBA IS 'COMMENT ON TABLE'  ]';You cannot combine multiple DDL statements into a single block (well, you could use dynamic PL/SQL, but then your string would have a bunch of embedded EXECUTE IMMEDIATE statements which would presumably not buy you much).
Justin

Similar Messages

  • Why there is implicit commit before and after executing DDL Statements

    Hi Guys,
    Please let me know why there is implicit commit before and after executing DDL Statements ?
    Regards,
    sushmita

    Helyos wrote:
    This is because Oracle has design it like this.Come on Helyos, that's a bit of a weak answer. :)
    The reason is that it makes no sense to update the structure of the database whilst there is outstanding data updates that have not been committed.
    Imagine having a column that is VARCHAR2(50) that currently only has data that is up to 20 characters in size.
    Someone (person A) decides that it would make sense to alter the table and reduce the size of the column to varchar2(20) instead.
    Before they do that, someone else (person B) has inserted data that is 30 characters in size, but not yet committed it.
    As far as person B is concerned that insert statement has been successful as they received no error, and they are continuing on with their process until they reach a suitable point to commit.
    Person A then attempts to alter the database to make it varchar2(20).
    If the database allowed that to happen then the column would be varchar2(20) and the uncommitted data would no longer fit, even though the insert was successful. When is Person B going to find out about this? It would be wrong to tell them when they try and commit, because all their transactions were successful, so why should a commit fail.
    In this case, because it's two different people, then the database will recognise there is uncommitted transactions on that table and not let person B alter it.
    If it was just one person doing both things in the same session, then the data would be automatically committed, the alter statement executed and the person informed that they can't alter the database because there is (now) data exceeding the size they want to set it to.
    It makes perfect sense to have the database in a data consistent state before any alterations are made to it, hence why a commit is issued beforehand.
    Here's something I wrote the other day on the subject...
    DDL's issue a commit before carrying out the actual action
    As long as the DDL is syntactically ok (i.e. the parser is happy with it) then the commit is issued, even if the actual DDL cannot be executed for another reason.
    Example...
    We have a table with some data in it...
    SQL> create table xtest as select rownum rn from dual;
    Table created.
    SQL> select * from xtest;
            RN
             1We then delete the data but don't commit (demonstrated by the fact we can roll it back)
    SQL> delete from xtest;
    1 row deleted.
    SQL> select * from xtest;
    no rows selected
    SQL> rollback;
    Rollback complete.
    SQL> select * from xtest;
            RN
             1
    SQL> delete from xtest;
    1 row deleted.
    SQL> select * from xtest;
    no rows selectedSo now our data is deleted, but not committed, what if we issue a DDL that is syntactically incorrect...
    SQL> alter tab xtest blah;
    alter tab xtest blah
    ERROR at line 1:
    ORA-00940: invalid ALTER command
    SQL> rollback;
    Rollback complete.
    SQL> select * from xtest;
            RN
             1... the data can still be rolled back. This is because the parser was not happy with the syntax of the DDL statement.
    So let's delete the data again, without committing it, and issue a DDL that is syntactically correct, but cannot execute for another reason (i.e. the database object it refers to doesn't exist)...
    SQL> delete from xtest;
    1 row deleted.
    SQL> select * from xtest;
    no rows selected
    SQL> truncate table bob;
    truncate table bob
    ERROR at line 1:
    ORA-00942: table or view does not exist
    SQL> rollback;
    Rollback complete.
    SQL> select * from xtest;
    no rows selectedSo, there we have it. Just because the statement was syntactically correct, the deletion of the data was committed, even though the DDL couldn't be performed.
    This makes sense really, because if we are planning on altering the definition of the database where the data is stored, it can only really take place if the database is in a state where the data is where it should be rather than being in limbo. For example, imagine the confusion if you updated some data on a column and then altered that columns datatype to be a different size e.g. reducing a varchar2 column from 50 character down to 20 characters. If you had data that you'd just updated to larger than 20 characters whereas previously there wasn't, then the alter table command would not know about it, would alter the column size and then the data wouldn't be valid to fit whereas the update statement at the time didn't fail.
    Example...
    We have a table that only allows 20 characters in a column. If we try and insert more into that column we get an error for our insert statement as expected...
    SQL> create table xtest (x varchar2(20));
    Table created.
    SQL> insert into xtest values ('012345678901234567890123456789');
    insert into xtest values ('012345678901234567890123456789')
    ERROR at line 1:
    ORA-12899: value too large for column "SCOTT"."XTEST"."X" (actual: 30, maximum: 20)Now if our table allowed more characters our insert statement is successful. As far as our "application" goes we believe, nay, we have been told by the database, we have successfully inserted our data...
    SQL> alter table xtest modify (x varchar2(50));
    Table altered.
    SQL> insert into xtest values ('012345678901234567890123456789');
    1 row created.Now if we tried to alter our database column back to 20 characters and it didn't automatically commit the data beforehand then it would be happy to alter the column, but then when the data was committed it wouldn't fit. However the database has already told us that the data was inserted, so it can't go back on that now.
    Instead we can see that the data is committed first because the alter command returns an error telling us that the data in the table is too big, and also we cannot rollback the insert after the attempted alter statement...
    SQL> alter table xtest modify (x varchar2(20));
    alter table xtest modify (x varchar2(20))
    ERROR at line 1:
    ORA-01441: cannot decrease column length because some value is too big
    SQL> rollback;
    Rollback complete.
    SQL> select * from xtest;
    X
    012345678901234567890123456789
    SQL>Obviously, because a commit statement is for the existing session, if we had tried to alter the table column from another session we would have got
    SQL> alter table xtest modify (x varchar2(20));
    alter table xtest modify (x varchar2(20))
    ERROR at line 1:
    ORA-00054: resource busy and acquire with NOWAIT specified
    SQL>... which is basically saying that we can't alter the table because someone else is using it and they haven't committed their data yet.
    Once the other session has committed the data we get the expected error...
    ORA-01441: cannot decrease column length because some value is too bigHope that explains it

  • Executing ddl statment in pl/sql block

    Hi all,
    could anyone pls help in method for executing ddl statment in pl/sql block other than 'execute immediate' ?

    could anyone pls help in method for executing ddl statment in pl/sql block other than 'execute immediate' ?On newer db versions you have more options:
    SQL> desc t
    Error: object t does not exist
    SQL> exec sys.dbms_prvtaqim.execute_stmt('create table michael.t (a integer)')
    PL/SQL procedure successfully completed.
    SQL> desc t
    TABLE t
    Name                                      Null?    Type                       
    A                                                  NUMBER                     
    SQL> exec sys.dbms_utility.exec_ddl_statement('drop table michael.t')
    PL/SQL procedure successfully completed.
    SQL> desc t
    Error: object t does not exist;)

  • Executing *.bat file from PL/SQL

    Is there anyway for Executing *.bat file from PL/SQL ?
    Thanks

    Try here:
    asktom.oracle.com/pls/ask/f?p=4950:8:15427268967155079552::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:952229840241

  • Executing a DDL statement from java code

    Hi all,
    this is code from jdev11.1.1.3 version. I am trying to execute a DDL statement in oracle db from java code, but "ORA-00900: invalid SQL statement" error is coming.
    I am trying to create a table in same schema in same db by using 'Copy' command.
    Same DDL command is executing from sql command prompt & table is being created. Plz help me , as how to do from java?
            public String cmb_action() {
            // Add event code here...
            try {
                //getting source db connection
                InitialContext initialContext = new InitialContext();
                DataSource ds = (DataSource) initialContext.lookup("java:comp/env/jdbc/SourceConnDS");
                Connection sourceconn = ds.getConnection();
                sourceconn.setAutoCommit(false);
                String sql = "Copy from myschema/mypass@DB insert t_dept using select * from dept;"                       
                Statement stat = sourceconn.createStatement();
                stat.executeUpdate(sql);
                sourceconn.commit();
                System.out.println("done");
              catch (Exception ne) {
                // TODO: Add catch code
                ne.printStackTrace();
            return null;
        }

    I have a requirement to transfer data from one db to another db from Java Application Layer.Maybe, maye not. We get all sorts of weird "requirements" - which are nothing but thoughts or proposed solutions.
    But,
    Did the "requirement" mention whether the table existed already or not in the target database? - If not, did it tell you to create it - drop/create it?
    Did the "requirement" deliver some explanation to why this copying was neeeded? - Are we talking replication? - Or a one time cloning?
    Etc, etc,
    Personally I would always argue against a "reuirement" like that. - It just isn't the way to do it. Period.
    Regards
    Peter
    P.S: If you are satisfied with what COPY does, then you could let Java make an OS call and do it from there?

  • Limitation on SQL executing select statement from ADO and Oracle 8.1.7.1 OleDB Driver

    Hi,
    we are running a query with a big dunamic select statement from VB code using ADO command object. When Execute method is called system hangs and control won't return back to the application. it seems to be that there is some type limitation on Query string length. Please tell us if there is any?
    we are running Oracle 8.1.7 Server on Windows 200 Server and connecting from a W2K professional, ADO 2.6 and Oracle OLEDB 8.1.7.1 OLEDB Driver.
    Sample code:
    Dim rs As ADODB.Recordset
    Dim cmd As ADODB.Command
    Set cmd = New Command
    With cmd
    .CommandText = ' some text with more than 2500 characters
    .CommandType = adCmdText
    Set rs = .Execute
    End With
    when i debug using VB6 and when .Execute line is called system hangs or return a message method <<somemethod> of <<some class name>> failed error.
    Any help is appreciated.
    Thanks,
    Anil

    A stored procedure would only slow you down here if it was poorly written. I suspect you want to use the translate function. I'm cutting & pasting examples from the documentation-- a search at tahiti.oracle.com will give you all the info you'll need.
    Examples
    The following statement translates a license number. All letters 'ABC...Z' are translated to 'X' and all digits '012 . . . 9' are translated to '9':
    SELECT TRANSLATE('2KRW229',
    '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
    '9999999999XXXXXXXXXXXXXXXXXXXXXXXXXX') "License"
    FROM DUAL;
    License
    9XXX999
    The following statement returns a license number with the characters removed and the digits remaining:
    SELECT TRANSLATE('2KRW229',
    '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123456789')
    "Translate example"
    FROM DUAL;
    Translate example
    2229
    Also, LIKE '%<string>%' is going to be rather expensive simply because it has to compare the entire string and because it forces full table scans, rather than using indexes. You could speed this sort of query up by using interMedia Text (Oracle Text now in 9i). If you can eliminate one of the '%' options, you could also improve things.
    My guess is that your stored procedure is inefficient and that's causing the problem-- 5k rows per table should be pretty trivial.
    If you post your query over on the PL/SQL forum, there are better performance tuners than I that might have more hints for you. To get really good advice, though, you'lllikely have to get at least the execution plan for this statement and may need to do some profiling to identify the problem areas.
    Justin

  • DDL statements and dynamic  sql  in stored procedure

    I created a stored procedure to create and drop tables, using dynamic sql.
    When I try to do the inserts using dynamic sql, i.e
    v_string := 'INSERT statement';
    EXECUTE IMMEDIATE v_string;
    I get the following error message:
    ERROR at line 1:
    ORA-00942: table or view does not exist
    ORA-06512: at line 63
    Line 63 happens to be the line that the EXECUTE IMMEDIATE v_string; statement is in.
    I am able to describe the table that the inserts are being made into, so I know that the table exists.
    Any idea why I'm getting this error message would be appreciated.

    Yes I do and I have been able to create other tables using dynamic sql.
    The table that I am having problems with SELECTs data from another table to get its column values; within the SELECT statement, the CAST function is used:
    ie. CAST(CASE SUBSTR(CAST(E_MOD AS VARCHAR(7)),2,3)
    WHEN 'AAA' THEN 'A55'
    ELSE ............
    I get the following error message:
    ERROR at line 18: (this line starts the CAST statement)
    ORA-06550: line 18, column 13:
    PLS-00103: Encountered the symbol "AAA" when expecting one of the following:
    . ( * @ % & = - + ; < / > at in is mod not rem return
    returning <an exponent (**)> <> or != or ~= >= <= <> and or
    like between into using || bulk
    When I remove the quotes or add another single quote, the same error cascades to 'A55'.
    After doing the same for the next error, I get the error message below:
    ERROR at line 1: (this line has the EXECUTE IMMEDIATE statement)
    ORA-00936: missing expression
    ORA-06512: at line 6
    Any idea what the problem could be?
    Also is there another way to have DDL statements as stored procedures other than using dynamic sql or the DBMS_SQL package?

  • Errors when executing DDL statements against MaxDB

    Hi guys,
    We are experiencing problems with DDL execution against MaxDB. I have installed version 7.8.00.17 on my 32-bit Windows XP XP3 machine. I got the following errors:
    1. MaxDB complained that the "VARCHAR(8008)" data type has invalid length.
    2. When fixing this, I got the problem, that a unique constraint has too long name.
    3. After shortening this name, now I get the following:
      u2190[4;35;1mSQL (0.0ms)u2190[0m   u2190[0mActiveRecord::JDBCError: SAP DBTech JDBC: [-4006] (at 25): Unknown domain name:PRIMARY_KEY: CREATE TABLE photos (id primary_key, title varchar(8008), created_at timestamp, updated_at timestamp) u2190[0m
    rake aborted!
    All these DDL statements, are generated as part of a rake task execution (we are testing a simple Rails application, that uses persistence), and they execute fine on MySQL for instance. But on MaxDB it seems that there are more severe limitations about name lengths, data types, etc.
    Could you please advice about this problem ?
    Thanks and Kindest Regards,
    Krum.

    Hmm... what's wrong with the internet on your side, that you don't have access to the MaxDB documentation?
    ad 1. You are using a UNICODE varchar, aren't you? check [here|http://maxdb.sap.com/doc/7_7/45/33337d9faf2b34e10000000a1553f7/frameset.htm] why it fails.
    ad 2. Unique constraints don't get names in MaxDB [see here|http://maxdb.sap.com/doc/7_7/45/50fa4272c31796e10000000a1553f6/frameset.htm]
    ad 3. You're simply using the wrong DDL format. "PRIMARY_KEY" is not even a MaxDB keyword.
    I'm pretty sure you'll find the correct syntax for primary key definition yourself, by simply browsing the documentation, so I leave this to you now.
    regards,
    Lars

  • Execute .Bat file from pl/sql code

    Hi,
    Can you please let me know that how can I execute the .Bat file from pl/sql procedure? Does anybody have a sample code??
    Thanks.

    Hi
    This may help you
    http://www.dba-oracle.com/t_running_windows_bat_file_dbms_scheduler.htm
    br,Jari

  • How to execute exe file from pl/sql

    How to execute an exe file or an operating system command from pl/sql in oracle 9i.

    If it is part of a pl/sql block, use the java suggestion. If you are not in a blck, you can use the "HOST" command. see
    http://download-west.oracle.com/docs/cd/A87860_01/doc/server.817/a82950/ch2.htm#1001697

  • How to change Bulk Insert statement from MS SQL to Oracle

    Hi All,
    Good day, I would like to bulk insert the content of a file into Oracle db. May I know how to change the below MS SQL syntax to Oracle syntax?
    Statement statement = objConnection.createStatement();
    statement.execute("BULK INSERT [TBL_MERCHANT] FROM '" MERCHANT_FILE_DIR "' WITH ( FIELDTERMINATOR = '~~', ROWTERMINATOR = '##' )");
    Thanks in advance.
    cs.

    Oracle SQL Loader utility allows you to insert data from flat file to database tables.
    Go to SQL Loader links on following url to learn more on this utility
    http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/server.920/a96652/toc.htm
    Chandar

  • Executing PLSQL statements on Oracle SQL Developer

    Hello All,
    I am trying to execute following PLSQL statements through SQL Developer:
    DECLARE
    myvar VARCHAR2(30);
    BEGIN
    myvar := 'TEST NAME';
    DBMS_OUTPUT.PUT_LINE(myvar);
    END;
    But nothing is being displayed. And when I execute the same on SQL*Plus, the output is properly being displayed.
    How can I a make it work on SQL Developer also.

    hi
    try this ......
    set serveroutput on;
    DECLARE
    myvar VARCHAR2(30);
    BEGIN
    myvar := 'TEST NAME';
    DBMS_OUTPUT.PUT_LINE(myvar);
    END;
    if it is correct then please make it correct /helpful
    Thanks
    shyam~

  • DDL statements in v$sql tables, how can i get the password.....

    Hi friends,
    When I create a user by issuing a statement like
    create user abcd identified by xyz;
    user created.
    but, how can i get the password without encryption in the v$XXXXX tables. Is there any dynamic table which stores the statement executed above in the current session, so that I can get the original password given.
    but when selecting the password by the query
    select password from sys.user$
    where name = 'abcd';
    Password
    ==========
    345ISDF9K4590DFJ35
    .........this is not my password(yes it is!) but not understandable..,
    Anybody please, show me the right way or query..,
    Post-Query Thanx..
    Praveenkumar Talla (Pune, India)

    I would hope there is no way to get this data. I wouldn't want a mechanism to grab passwords out of the SGA. Passwords are stored encrypted in the database. If you need to know the password, run another 'alter user username identified by password' and make not of what you changed it to.

  • How to execute this statement from oracle reports

    have to use the following command from oracle reports.
    In oracle forms we can use the HOST command but what about oracle reports2.5.
    I have to email the attached file 100245.pdf from oracle reports to the given email id
    uuencode 100245.pdf 100245.pdf | mailx -s "test" [email protected]

    > u can execute any exe using host that host must contain a bat file
    Incorrect. There is no HOST command in PL/SQL.
    And please note that this is the Database » SQL and PL/SQL forum - and not the Oracle Forms or SQL*Plus forum.
    Also, when using the SQL*Plus HOST command for example, you can execute any executable and not just a BAT file. SQL*Plus on Windows uses the Win32 API call CreateProcess() - and it has no limits as to what/which executable can be executed.

  • Help with a select statement from a SQL Server within a DTS !!

    Hello Gurus!
    I help with the script bellow, when I run it within DTS (in SQL Sever 2000), I got the error Invalid number/or not a valid month.
    Please bellow with the WHERE CLASUE '08/01/2001' AND '03/09/2002'
    And in the other hand I change this forma to '01-AUG-01' AND
    '03-MAR-2002', the DTS start and run witha successful messages, but it does not returns row, which is wrong.
    Somebady please help!
    Thanks Gurus!
    GET Total ANIs with Trafic By Area Code
    select
         substr(b.ct_num, 0,3) as Area_Codes,
         COUNT(DISTINCT B.CT_NUM) AS ANIS
    from
         wasabi.v_trans A,
         wasabi.V_Sur_Universal B,
         wasabi.V_Sub C,
         wasabi.V_Trans_Typ D
    where
         D.Trans_typ = A.Trans_Typ AND
         A.Sur_ID = B.Sur_ID AND
         C.Sub_ID = A.Sub_ID AND
         a.trans_stat != 'X' AND     
         a.Trans_DTTM >= '08/01/2001'AND
         a.Trans_DTTM < '03/09/2002 AND
         B.AMA3 = 'PHONE1'
         AND C.SUB_ID not in (100117)
    GROUP BY
         substr(b.ct_num, 0,3)
    ORDER BY
         Area_Codes

    I think that you need a "to_date" function eg
    change '08/01/2001' to to_date('08/01/2001','dd/mm/yyyy')

Maybe you are looking for