Script to add not null columns to all tables in database

Hello,
I need to add 5 not null columns to my existing database (all tables).
The problem is that i do not want to loose the current data.
I need a script so that i need not do this manually for each table.
Can u suggest?
Vishal

Hello,
I need to add 5 not null columns to my existing database (all tables).
The problem is that i do not want to loose the current data.
I need a script so that i need not do this manually for each table.
Can u suggest?
Vishal I always follow this step
1) Alter table <<tablename>> add(<<columnname>> <<datatype>>)
2) Update <<tablename>> set <<columnname>>=<<anyvalue>>
3) Alter table <<tablename>> modify(<<columnname>> <<datatype>> not null)
else
1) rename <<tablename>> to <<tablenamebk>>
2) drop table <<tablename>>
3) Alter table <<tablenamebk>> add(<<columnname>> <<datatype>>)
4) update <<tablenamebk>> set <<columnname>>=<<anyvalue>>
5) create table <<tablename>> (with additional columns with not null)
6) insert into <<tablename>> select * from <<tablenamebk>>

Similar Messages

  • Script or query to generate a report of null or not null columns

    I need a script/query it should pick up all the tables from user_tab_columns and produce a report for all the tables which are the columns are null and not null.

    As long as the columns were defined as NOT NULL on table create, or ALTERed NOT NULL, you can do this:
    SQL> CREATE TABLE t (id NUMBER NOT NULL, descr VARCHAR2(10));
    Table created.
    SQL> SELECT column_name, table_name, nullable
      2  FROM user_tab_columns
      3  WHERE table_name = 'T';
    COLUMN_NAME                    TABLE_NAME                     N
    ID                             T                              N
    DESCR                          T                              Y
    SQL> ALTER TABLE t modify (descr NOT NULL);
    Table altered.
    SQL> SELECT column_name, table_name, nullable
      2  FROM user_tab_columns
      3  WHERE table_name = 'T';
    COLUMN_NAME                    TABLE_NAME                     N
    ID                             T                              N
    DESCR                          T                              NNote that if you do:
    ALTER TABLE t ADD CONSTRAINT id_nn CHECK (id IS NOT NULL);then the nullable column in xxx_tab_columns will remain as Y.
    HTH
    John

  • ORA-01400 - not null column with default value and item with authorization

    I've searched - I would think someone has run into this. APEX 3.0.0.00.20 - I've created a simple form on a table. One of the column is a not null column with a default value. I have a select list on that item, but it has security on it - authorization scheme. So, it checks the user and if that user isn't of the right role, it will not even display that item. However, APEX appears to still send in the column in its sql! So, the default value is useless, it sends in null each time. Even if I set the default at the Item level, I get null. Argg. That's got to be a bug...
    In debug, I do not see the item listed at all. It's not used. That's fine - but why is it trying to insert the value? I would think it would leave it off??? I think because the item is associated with a database column. But, getting around this is ugly. Having to create a hidden item for each one, and then check to see if I need to take the list value... horrible. Any way to get around this???

    I should add - I guess I can always put my own custom process in to replace the DML. Just seems like a simple thing - if the value doesn't appear on the debug, isn't set with any default value... don't include it in the DML.

  • Do we have a function in oracle to select not null columns at the begining

    Hi,
    I have 8 columns. Some of them might be null.
    I want to display all 8 columns in my result. Not null columns will be first and null at the end.
    Here is a sample data :
    Employee table :
    Employee_id   Emp_fname  emp_lname  emp_mname  dept salary emp_height  emp_weight
       1               aaa        ddd                d1   100      6           180
       2               bbb                ccc             120                 169
       3               dfe                           d2            5.9         223The expected result is :
    result1 result2   result3 result4  result5  result6 result7 result8
    1        aaa        ddd     d1       100     6        180
    2        bbb        ccc     120      169
    3        dfe        d2      5.9      223Thanks.
    Edited by: BluShadow on 12-Jul-2012 16:12
    added {noformat}{noformat} tags for readability.  Please read {message:id=9360002} and do this yourself in future.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    The requirement doesn't make a lot of sense and tends to imply that you have an incorrect data model. If that's the case, you'd be much better off fixing the data model than in trying to write this sort of query (particularly since a poor data model is going to force you to write a bunch of creative SQL).
    If you define a type and a function
    CREATE TYPE num_tbl AS TABLE OF NUMBER;
    create or replace function count_null( l_nums in num_tbl )
      return number
    is
      l_cnt integer := 0;
    begin
      for i in 1..l_nums.count
      loop
        if( l_nums(i) is null )
        then
          l_cnt := l_cnt + 1;
        end if;
      end loop;
      return l_cnt;
    end;Then you can do something like this
    SQL> ed
    Wrote file afiedt.buf
      1  select product,
      2         coalesce( q1, q2, q3, q4 ) col1,
      3         (case when count_null( num_tbl( q1 ) ) = 0
      4               then coalesce( q2, q3, q4 )
      5               when count_null( num_tbl( q1, q2 ) ) = 1
      6               then coalesce( q3, q4 )
      7               when count_null( num_tbl( q1, q2, q3 ) ) = 1
      8               then q4
      9               else null
    10           end) col2,
    11         (case when count_null( num_tbl( q1, q2 ) ) = 0
    12               then coalesce( q3, q4 )
    13               when count_null( num_tbl(q1, q2, q3) ) = 1
    14               then q4
    15               else null
    16           end) col3,
    17         (case when count_null( num_tbl( q1, q2, q3 ) ) = 0
    18               then q4
    19               else null
    20           end) col4
    21*   from saleshist
    SQL> /
    PRODUCT                              COL1       COL2       COL3       COL4
    Oracle EE                             100        123        128
    Partitioning                          100        130        128Justin

  • Oracle BPEL - Does not cater for not null columns and use of "default".

    Oracle BPEL - Does not cater for not null columns and use of "default".
    BPEL fails with message:
    ORA-01400: cannot insert NULL into ("EDDB"."SEISMIC_LINES"."COORD_SYSTEM_ID")
    But SQL*PLUS command works:
    INSERT into EDDB.SEISMIC_LINES
    (etc)
    regards
    Allan Ford
    Analyst / Programmer - IT Application Services, IT Services, Shared Business Services
    Santos Ltd
    Level 4, 91 King William Street, Adelaide SA 5000
    Phone: 08 8224 7944 Fax: 08 8218 5320
    Email: [email protected]

    note: BPEL keeps it's own "offline" copy of table and database items. A column that is marked not null in the database can be marked as nullable in this area. (if you kmow that a trigger is going to cater for this ..)
    One workaround is to use a trigger to provide value rather than use the column default ..

  • How to create a single 'not null ' validation for all the items in a page ?

    Hi everyone ,
    how to create a single 'not null ' validation for all the items in a page ? I have many textfields . Instead of creating 'not null' validation for each item , I would like to create a a single validation control that will serve the purpose
    Thanks & Regards
    Umer

    Nice1 wrote:
    bob , as u said I have done the following :
    1) under create button , there are 9 items and for each item I have set Required to 'Yes'
    2) under delete button , there is 1 item and have set Required to 'Yes' for the item
    3) defined page validation for 9 items under 'create ' button and have set it to fire when 'create ' button clicked
    4) defined page validation for 1 item under 'delete ' button and have set it to fire when 'delete ' button clicked
    now , when I click 'create' button it even shows for the item under 'Delete ' button that it is a required itemSorry, I didn't see this note. The required template won't work, there is no way to attach it to the button.
    The best solution is as the reply a couple replies up
    Create 2 page type validations as a PL/SQL with code
    1st validation
    :P1_ITEM1 IS NOT NULL and :P1_ITEM2 IS NOT NULL ...... and :P1_ITEM9 IS NOT NULL  include all 9 items
    Set the When Button Pressed to the CREATE button
    2nd validation
    :P1_ITEM10 IS NOT NULL
    Set the When Button Pressed to the DELETE buttonI think that's going to be the easiest way to do it.
    Edited by: Bob37 on Apr 27, 2012 12:02 PM

  • Can't add a new column to LCD table? Option there, just wont add it.

    I have a LCD form with a table in it. Im trying to simply add a new column to the table. The option is there but the column just wont appear after clicking add Column to Left. I selected a column, right clicked the space > Insert > Column to left. But wont intsert. The table object properties tab shows rows and colum numbers but they are grayed out.
    See screenshots:
    http://www.emermed.net/staging/forums...
    http://www.emermed.net/staging/forums...
    This is a dynamic table where a new row can be inserted using a button. Is that dynamic nature the issue? Id hate to undo all the dynamic flow and programming just to add a column then reapply everything.
    Thanks!

    Hi,
    You don't add columns in the Object > Table palette (Screenshot 2). I suspect that the problem is that there is not enough on the page (within the content area) to actually add the column. In Screenshot 1, if you look at the width of the highlighted column versus the space available to the right of the table, you will see that a column cannot be added due to space restrictions.
    Reduce the width of the highlighted column (temporarily), then add a column. Once added, you can resize the columns to match the page width.
    Hope that helps,
    Niall

  • How to search all columns of all tables in a database

    i need to search all columns of all tables in a database , i already write the code below , but i've got the error message below when run this script
    DECLARE
    cnt number;
    v_data VARCHAR2(20);
    BEGIN
    v_data :='5C4CA98EAC4C';
    FOR t1 IN (SELECT table_name, column_name FROM all_tab_cols where owner='admin' and DATA_TYPE='VARCHAR2') LOOP
    EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM ' ||t1.table_name|| ' WHERE ' ||t1.column_name || ' = :1' INTO cnt USING v_data;
    IF cnt > 0 THEN
    dbms_output.put_line( t1.table_name ||' '||t1.column_name||' '||cnt );
    END IF;
    END LOOP;
    END;
    Error report:
    ORA-00933: SQL command not properly ended
    ORA-06512: at line 7
    00933. 00000 - "SQL command not properly ended"
    *Cause:   
    *Action:
    Any help please

    SQL solutions by Michaels
    michaels>  var val varchar2(5)
    michaels>  exec :val := 'as'
    PL/SQL procedure successfully completed.
    michaels>  select distinct substr (:val, 1, 11) "Searchword",
                    substr (table_name, 1, 14) "Table",
                    substr (t.column_value.getstringval (), 1, 50) "Column/Value"
               from cols,
                    table
                       (xmlsequence
                           (dbms_xmlgen.getxmltype ('select ' || column_name
                                                    || ' from ' || table_name
                                                    || ' where upper('
                                                    || column_name
                                                    || ') like upper(''%' || :val
                                                    || '%'')'
                                                   ).extract ('ROWSET/ROW/*')
                       ) t
    --        where table_name in ('EMPLOYEES', 'JOB_HISTORY', 'DEPARTMENTS')
           order by "Table"or
    11g upwards
    SQL> select table_name,
           column_name,
           :search_string search_string,
           result
      from (select column_name,
                   table_name,
                   'ora:view("' || table_name || '")/ROW/' || column_name || '[ora:contains(text(),"%' || :search_string || '%") > 0]' str
              from cols
             where table_name in ('EMP', 'DEPT')),
           xmltable (str columns result varchar2(10) path '.')
    TABLE_NAME                     COLUMN_NAME                    SEARCH_STRING                    RESULT   
    DEPT                           DNAME                          es                               RESEARCH 
    EMP                            ENAME                          es                               JAMES    
    EMP                            JOB                            es                               SALESMAN 
    EMP                            JOB                            es                               SALESMAN 
    4 rows selected.

  • How to find out the NULL columns in the table?

    Hi,
    Please provide the query to find the null columns in the table. Here, all rows in the table have same column as null. It won't change.
    Table
    c1 c2 c3 c4
    X C 10
    T D 20
    I wanna find out as C2 is the nullable column.
    Thanks in advance !!
    Regards,
    Vissu....

    Below code might be solution for finding NULL valued in columns in a table and if it is solution .kindly give me points for the same.
    declare
    cursor col_cur is
    select column_name
    from user_tab_cols -- all_tables can also be used in case the table is present in own schema
    where table_name = 'TABLE_NAME'; --provide the TABLE_NAME  
    stmt varchar2(1000):= '';
    v_count number;
    count_null number;
    begin
    execute immediate 'select count(*) from TABLE_NAME' into v_count; --provide the TABLE_NAME  
    for rec in col_cur loop
    execute immediate 'select count(*) from TABLE_NAME' where --provide the TABLE_NAME  
    '||rec.column_name||' IS NULL' into count_null;
    if count_null = v_count then
    stmt :=stmt|| rec.column_name ||chr(13);
    end if;
    end loop;
    dbms_output.put_line(stmt);
    end ;

  • How can L identify what are the not null fields of a table in a stored procedure ?

    How can L identify what are the not null fields of a table in a stored procedure ?

    You could query the data dictionary:
    SELECT column_name
    FROM all_tab_columns
    WHERE owner = '...'
    AND table_name = '...'
    AND nullable = 'N'

  • How to make allow null for a column in all table.

    Hi,
    I have a column "col1" in all tables (user made table) , i want to make it allow null
    please tel me how can it be done using single script.
    i mean in short way.
    yours sincerely.

    yes. absolutely you can.
    Look, what i got -
    Ranit>>  select 'alter table ' || table_name || ' modify("TIMESTAMP" null) ; ' from user_tab_columns where column_name='TIMESTAMP' and nullable ='N';
    'ALTERTABLE'||TABLE_NAME||'MODIFY("TIMESTAMP"NULL);'                           
    alter table FIXED_OBJ$ modify("TIMESTAMP" null) ;                              
    alter table SUMPARTLOG$ modify("TIMESTAMP" null) ;                             
    alter table SUMDELTA$ modify("TIMESTAMP" null) ;                               
    alter table ALL_SUMDELTA modify("TIMESTAMP" null) ;                            
    alter table DBA_EXP_FILES modify("TIMESTAMP" null) ;                           
    alter table WRI$_DBU_CPU_USAGE modify("TIMESTAMP" null) ;                      
    alter table DBA_CPU_USAGE_STATISTICS modify("TIMESTAMP" null) ;                
    7 rows selected.But, it is not over.
    Either - You have to do an 'EXECUTE IMMEDIATE' over these 'alter' scripts.
    OR - Take these scripts, put it into a <script_name>.sql file and then execute this file.
    Hope you understand.
    Edited by: ranit B on Dec 22, 2012 3:17 PM
    -- code added

  • OracleXMLSave - Insert into table with NOT NULL Columns

    Im having trouble using OracleXMLSave (XSU12) to insert into a
    table where columns are initialized on insert with system
    derived values (see table script below) I get the following
    error message.
    Message: 'java.sql.SQLException: ORA-01400: cannot insert
    NULL "TESTMEETING"."S_TERMID")
    Im inserting this XML:
    <ROWSET table="testmeeting">
    <ROW>
    <DESCRIPTION>TestMeeting</DESCRIPTION>
    <STARTDATE>2001-10-22 00:00:00.0</STARTDATE>
    <CATEGORYCODE>HR</CATEGORYCODE>
    <STATUS>O</STATUS>
    </ROW>
    </ROWSET>
    Table Script:
    CREATE TABLE TESTMEETING (
    S_TERMID VARCHAR2 (12) DEFAULT (USERENV('TERMINAL'))
    NOT NULL,
    S_RECORDCREATED DATE DEFAULT (SYSDATE) NOT NULL,
    S_RECORDCREATOR NUMBER (4) DEFAULT (UID) NOT NULL,
    DESCRIPTION VARCHAR2 (28),
    CATEGORYCODE VARCHAR2 (2),
    STARTDATE DATE,
    STATUS VARCHAR2 (1))
    If I remove these columns the insert works OK. Inserts also work
    fine from sqlplus etc.
    Apologies for the previous entry, itchy fingers.
    Thanks for your help

    Hi.
    try to insert data in column "TESTMEETING"."S_TERMID" by trigger.
    Thanks..

  • OBIEE not selecting columns from all LTS of a Conforming Degener@teDimen$ion

    Hello everyone
        I have a Degener@teDimen$ion Dimension A it has 4 LTS ( Fact1, Fact1A ,Fact2 and FACT2A).--> FACTS from
    Physical Layer.. FACTS in BMM are FACT_ONe and FACT_TWO.
    columns in Degener@teDimen$ion Dimension A
    col1 --> mapped to all 4 LTS(logical source). It is the key column.
    col2 --> mapped to all 4 LTS(logical source)
    col3 --> mapped to all 4 LTS(logical source)
    The content Tab of the LTS for this Dimension are mapped to Detail of
    Dimension Hierarchy name is DDimension A
    Dimension Hierarchy name is DDimension A, Levels in it are
    Total --> This has all the fact columns from FACT1 and FACT2. Basically dragged dropped from facts to
    this level.
    & Detail ---> It has col1,col2 and col3 and Degener@teDimen$ion Dimension A.
    FACT_ONE --> Fact1, Fact1A are LTS for this fact 
    FACT_TWO --> Fact2 and FACT2A are LTS for this fact.
    When i am doing a analysis with
    Year, Degener@teDimen$ion_Dimension A_COL2 , FACT_ONE.measure1, FACT_TWO.measure1
    It is basically showing the dimension value of FACT_TWO.measure1 only...
    even the select statement is not having column from FACT_ONE...
    Like
    select x,y, col2 from fact_two full outer join select x,y from fact_one...> I just provided this
    syntax just for an idea.. the this fact_one is not having that column in the select..
    I referred this link
    "http://www.rittmanmead.com/2010/01/oracle-bi-ee-10-1-3-4-1-modeling-degenerate-dimensions-fact-attributes/"
    and others but i am not able to find the reason why it is not issuing a select...
    Just to add more in the Total level of the hierarchy, if i added FACT_TWO only removed FACT_ONE measures..
    it is showing only fact_one value (this time the FACT_ONE SELECT is having col2 but not FACT_TWO)...
    the behaviuor is vice versa as well...

    I resolved this. The Issue is, when using Degener@teDimen$ion ( this is !nner joned to FACT tables in BMM) and if any of the dimensions {other than the Degener@teDimen$ion (Let us say Dim X) } have an ()uter join to any of the fact tables, and you were doing your analysis using Degener@teDimen$ion,  Dim X, Measure value you will face the following issues.
    when filtering the analysis on the ()uter join dimension ( Dim X), the IN filter will not work. Reason is that the filter is getting applied to both the Dimension and FACT tables and the values that exist in Dimension Dim X but not in FACT table wont show up.
         The above issue can be fixed by changing the join between the fact and Degener@teDimen$ion from inner to outer. I think this is a bug.. because  it is supposed to filter the fact  table after the entire outer joined result is obtained but not filter the fact table for the Dim X values and do a outer join. I think the BI should be intelligent enough.
         In order to solve the filtering issue what i did was mentioned in my Initial post. I think if we have the measures at the Totals level of dimension, it is picking the Degener@teDimen$ion,from the table that has less number of rows or the query from one of the two facts that gets submitted to database.. I couldn't find the relevant information in the oracle document though . . . .

  • Empty string in NOT NULL column

    I'm migrating data to Oracle 9i from a database engine that supports emtpy strings in column with NOT NULL specified in the DDL. I would have thought Oracle would support this, but any attempt to put empty string data into a column created with NOT NULL is returning a 'column does not support nulls'-type error.
    Is there an option to allow this, or does Oracle simply not support this and assumes blank is null?
    Thanks

    I would have thought Oracle would support this, but any attempt to put empty string data into a column created with NOT NULL is returning a 'column does not support nulls'-type error.In Oracle Null means null nothing not even an empty String.
    Yes DB's like Ingress supports this. But I think Oracle is right.
    Is there an option to allow this, or does Oracle simply not support this and assumes blank is null?I am not aware of any options as it is against the basic principles.
    you can find work arounds to do the migration. But storing a blank NO.
    Good Luck
    Vij

  • NULL in not null column

    Hi,
    I have a table
    SQL> desc tabula.M$$USERS;
    Name Null? Type
    USERLOGIN NOT NULL VARCHAR2(20)
    PASSWORD NOT NULL VARCHAR2(30)
    T$USER NOT NULL NUMBER(38)
    USERNAME NOT NULL VARCHAR2(30)
    USERID NOT NULL NUMBER(38)
    USERGROUP NOT NULL NUMBER(38)
    T$LINKID NOT NULL VARCHAR2(50)
    password column is not null.
    Application get ORA-01400 (can't insert to null into not null colum) and also when I run -
    Edited by: user10237262 on Nov 29, 2011 12:54 AM

    Hi welcome to forum
    see below example
    SQL> create table t(empno number(4) not null, ename varchar2(10))
      2  /
    Table created.
    SQL> insert into t values(100,'abc');
    1 row created.
    SQL> insert into t values(200,'def');
    1 row created.
    SQL> insert into t values(null,'def');
    insert into t values(null,'def')
    ERROR at line 1:
    ORA-01400: cannot insert NULL into ("SYSTEM"."T"."EMPNO")
    SQL> desc t
    Name                                      Null?    Type
    EMPNO                                     NOT NULL NUMBER(4)
    ENAME                                              VARCHAR2(10)
    SQL>"Null?" tells the perticular column is null or not...
    Usage
    The description for tables, views, types and synonyms contains the following information:
    each column's name
    whether or not null values are allowed (NULL or NOT NULL) for each column
    refer:
    http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12019.htm
    Edited by: newbie on Nov 29, 2011 12:54 AM

Maybe you are looking for

  • Probelm with Creating purchase order using BAPI_PO_CREATE1

    Hi Experts, I am trying to create a PO using the "BAPI_PO_CREATE1". The BAPI is returning a PO number but when I am checking for the same in ME23 its saying that the number doesnot exsists. In BAPI retrun I am gettig an info message as "Stock transpo

  • HT201413 i tryed to restore my phone but it turns of during the process and an error happens so what should i do

    i tryed to restore my phone but it turn off during the process and an error happens so should i do ?

  • BPM: percentage growth in table monitoring

    Hello all, I wanted to know whether it's possible to monitor a table in BPM with percentage growth values e.g. I get an alert when the table grows 10% or more per day. I only found the growth monitoring by absolute values like 100.000 records inserte

  • Help in sort key

    hii experts.. i am trying to create a new sort key throught OB16 but the systems shows me error ficore405...my client wants that the number should be visible  in the assignment field... please help me... Regards, Abhay

  • Wifi Link 5300 only shows 54Mbps on 5Ghz

    Using a T500 with its Intel wireless card Wifi Link 5300, XP Pro SP3, and Netgear router N900 (WNDR4500). Configured router to use WPA2-AES. Intranet transfers to my Netgear ReadyNAS (Duo v2) are slow. Using a lan wired connection the transfer rate i