truncate tables or replace old data with new data using imp oracle utility - oracle

I need import utility to remove old data then reinsert new data. How can I do that without using data pump?
The reason I can't use data pump is because I need to run this script on application server, I don't have the privilege to run this on database server. below is the script I'm currently trying to fix
imp < username >/< password >#//< host > fromuser=< schema > touser=< schema > file=exp_$TODAY.dmp log=exp_import_$TODAY.log ignore=y

IMP can't truncate tables prior to importing, so - you're out of luck here. If you could use the Data Pump Import, you could use the TABLE_EXISTS_ACTION parameter set to TRUNCATE (which would remove existing data) or even REPLACE (which drops table, creates it and loads data). Ask the DBA to provide access to a directory you could use for importing purposes.
Alternatively, as it seems that you're importing everything, consider dropping the target user (with the cascade option) and then import its objects.
Or, if it isn't too complicated, create a script which would truncate tables, for example
SQL> select 'truncate table ' || tname ||';' from tab;
'TRUNCATETABLE'||TNAME||';'
----------------------------------------------
truncate table BONUS;
truncate table DEPT;
truncate table EMP;
truncate table SALGRADE;
truncate table WC;
SQL>
so you'd run those TRUNCATE TABLE statements prior to importing.
Note that - if there are foreign key constraints - they won't let you do it unless you disable them; as above, you could use SQL to create SQL for you:
SQL> select 'alter table ' || table_name || ' disable constraint ' || constraint_name ||';'
2 from user_constraints
3 where constraint_type = 'R';
'ALTERTABLE'||TABLE_NAME||'DISABLECONSTRAINT'||CONSTRAINT_NAME||';'
---------------------------------------------------------------------------------------------
alter table EMP disable constraint FK_DEPTNO;
SQL>

Related

How to get the table name in trigger on which ddl operation will be performed

I want to do something like
create or replace trigger t1 before ddl on database
begin
insert into table1 values(//the table name on which the ddl will be performed);
end;
so if i create a table named "Hello" than "Hello"(table name) will be inserted in table1
so i don't know how to fetch the table name actually
That's show here in the Database Docs
So you could do something like...
CREATE OR REPLACE TRIGGER ddl_trig
AFTER DDL
ON DATABASE
BEGIN
INSERT INTO loguser.ddl_log
(user_name, ddl_date, ddl_type,
object_type, owner,
object_name)
VALUES
(ora_login_user, SYSDATE, ora_sysevent,
ora_dict_obj_type, ora_dict_obj_owner,
ora_dict_obj_name);
END ddl_trig;
/
BUT, you shouldn't build your own auditing software/code. You should instead use the database's built in auditing system.
See this.
Disclaimer: I work for Oracle and am a product manager on the database team.

Can I declare local temporary table in Oracle 12c

I'm trying to declare local temporary table using the example from Oracle / PLSQL: LOCAL TEMPORARY TABLES. But when I try to insert it into sqlplus' CLI and hit Enter, it does not execute anything and I don't know what to do next to complete the command except pressing Ctrl+C interrupting command inputing:
SQL> DECLARE LOCAL TEMPORARY TABLE suppliers_temp
( supplier_id number(10) NOT NULL,
supplier_name varchar2(50) NOT NULL,
contact_name varchar2(50)
); 2 3 4 5
6
7 ;
8 ^C
To execute this query I've logged in as SYSTEM user.
Why this example does not work for me?
local temporary tables aren't a thing in the Oracle RDBMS. Instead, you can have a Global Temporary Table (GTT) (which creates a permanent table, but the data is held at session level) or, introduced in 18c, you can have a Private Temporary Table (PTT) (the table definition and data are held at session level).
Both are similar to the standard create table statement, so to create a GTT that drops the rows when you commit, you would do something like:
create global temporary table table_name (col1 number, col2 varchar2(20))
on commit delete rows;

Take backup and then drop all tables with same prefix in Oracle

In one of my Windows form application in asp .net, I am creating tables on daily basis sharp at 00:00 am with name as "data_YYYY_MM_DD" in Oracle database. A large amount of data exist in each table as after every 5 seconds I am writing some useful data into these.
Now consider all tables of month Oct 2016. All will have name like 'data_2016_10%'. How can I take backup of only these tables (not backup of entire database) and then drop these tables from the database.
You can take logical backup of these tables using data pump. It creates dump file(binary file) which can be imported as per your needs.
Export:Table Mode
A table mode export is specified using the TABLES parameter. In table mode, only a specified set of tables, partitions, and their dependent objects are unloaded.
Example:
expdp hr TABLES=hr.employees VERSION=LATEST DIRECTORY=dpump_dir1 DUMPFILE=emp.dmp NOLOGFILE=YES
Reference:
Data Pump Export
Try like below,
you can schedule it in dbms_scheduler jobs,So that every day it will run and create backup table and drop existing table.Also please add exceptions like table does not exist...if you want
create or replace procedure backup_monthly_table
as
prev_date varchar2(20) := to_char(sysdate-1, 'yyyy-mm-dd');
begin
for i in (select table_name from dba_tables where upper(table_name) like '%'||'"'||prev_date||'_%')
loop
dbms_output.put_line('working');
dbms_output.put_line(i.table_name);
execute immediate 'create table sysman.'||'"'||i.table_name||'_bkp'||'"'||' as select * from sysman.'||'"'||i.table_name||'"';
execute immediate 'drop table sysman.'||'"'||i.table_name||'"';
end loop;
end;
verification output:
select owner,table_name from dba_tables where upper (table_name) like '%2017-%'
SYSMAN 2017-02-01_test1_bkp
SYSMAN 2017-02-01_test2_bkp
SYSMAN 2017-02-01_test3_bkp
SYSMAN 2017-02-01_test4_bkp

How to export empty tables in oracle

I have a big problem When i take a dump of database the empty tables are truncated
How i can export the all tables in oracle 10g
exp SYSTEM/password FILE=expdat.dmp
This might be because of these tables may not be extent allocated. Before taking backup you need to identify all the tables that do not have data. Then alter these tables to allocate extent.
ALTER TABLE <table_name> ALLOCATE EXTENT;
Use the below script to alter all tables they do not have extent allocated.
SELECT 'ALTER TABLE '||table_name||' ALLOCATE EXTENT;' FROM user_tables WHERE segment_created = 'NO';
Copy the output and execute it.
You might consider expdp (data pump) instead. There's a parameter CONTENT=METADATA_ONLY that might get you what you want.
I found another solution here.
Oracle has an option called DEFERRED_SEGMENT_CREATION, default true.
from the docs:
If set to true, then segments for tables and their dependent objects (LOBs, indexes) will not be created until the first row is inserted into the table.
I'll sum up the solution from the above link:
SQL> alter system set DEFERRED_SEGMENT_CREATION=FALSE scope=both;
Run the output of the following statement:
SQL> select 'alter table ' || table_name || ' move;' from user_tables where num_rows=0;
This made my exp export empty tables as well.

How to move table from one tablespace to another in oracle 11g

I run oracle 11g and need to move table (tbl1) from one tablespace (tblspc1) to another (tblspc2). What is the easiest way to do that?
Try this:-
ALTER TABLE <TABLE NAME to be moved> MOVE TABLESPACE <destination TABLESPACE NAME>
Very nice suggestion from IVAN in comments so thought to add in my answer
Note: this will invalidate all table's indexes. So this command is usually followed by
alter index <owner>."<index_name>" rebuild;
Use sql from sql:
spool output of this to a file:
select 'alter index '||owner||'.'||index_name||' rebuild tablespace TO_TABLESPACE_NAME;' from all_indexes where owner='OWNERNAME';
spoolfile will have something like this:
alter index OWNER.PK_INDEX rebuild tablespace CORRECT_TS_NAME;
Moving tables:
First run:
SELECT 'ALTER TABLE <schema_name>.' || OBJECT_NAME ||' MOVE TABLESPACE '||' <tablespace_name>; '
FROM ALL_OBJECTS
WHERE OWNER = '<schema_name>'
AND OBJECT_TYPE = 'TABLE' <> '<TABLESPACE_NAME>';
-- Or suggested in the comments (did not test it myself)
SELECT 'ALTER TABLE <SCHEMA>.' || TABLE_NAME ||' MOVE TABLESPACE '||' TABLESPACE_NAME>; '
FROM dba_tables
WHERE OWNER = '<SCHEMA>'
AND TABLESPACE_NAME <> '<TABLESPACE_NAME>
Where <schema_name> is the name of the user.
And <tablespace_name> is the destination tablespace.
As a result you get lines like:
ALTER TABLE SCOT.PARTS MOVE TABLESPACE USERS;
Paste the results in a script or in a oracle sql developer like application and run it.
Moving indexes:
First run:
SELECT 'ALTER INDEX <schema_name>.'||INDEX_NAME||' REBUILD TABLESPACE <tablespace_name>;'
FROM ALL_INDEXES
WHERE OWNER = '<schema_name>'
AND TABLESPACE_NAME NOT LIKE '<tablespace_name>';
The last line in this code could save you a lot of time because it filters out the indexes which are already in the correct tablespace.
As a result you should get something like:
ALTER INDEX SCOT.PARTS_NO_PK REBUILD TABLESPACE USERS;
Paste the results in a script or in a oracle sql developer like application and run it.
Last but not least, moving LOBs:
First run:
SELECT 'ALTER TABLE <schema_name>.'||LOWER(TABLE_NAME)||' MOVE LOB('||LOWER(COLUMN_NAME)||') STORE AS (TABLESPACE <table_space>);'
FROM DBA_TAB_COLS
WHERE OWNER = '<schema_name>' AND DATA_TYPE like '%LOB%';
This moves the LOB objects to the other tablespace.
As a result you should get something like:
ALTER TABLE SCOT.bin$6t926o3phqjgqkjabaetqg==$0 MOVE LOB(calendar) STORE AS (TABLESPACE USERS);
Paste the results in a script or in a oracle sql developer like application and run it.
O and there is one more thing:
For some reason I wasn't able to move 'DOMAIN' type indexes. As a work around I dropped the index. changed the default tablespace of the user into de desired tablespace. and then recreate the index again.
There is propably a better way but it worked for me.
Try this to move your table (tbl1) to tablespace (tblspc2).
alter table tb11 move tablespace tblspc2;
I tried many scripts but they didn't work for all objects. You can't move clustered objects from one tablespace to another. For that you will have to use expdp, so I will suggest expdp is the best option to move all objects to a different tablespace.
Below is the command:
nohup expdp \"/ as sysdba\" DIRECTORY=test_dir DUMPFILE=users.dmp LOGFILE=users.log TABLESPACES=USERS &
You can check this link for details.

Resources