how to get the DDL of a table with different schema - oracle

I have few databases and all the databases have the same tables (i.e. table names). Now i want to get the DDL of the table with different schema.

Use the dbms_metadata package to get the DDL of any object of the DB.
SELECT
DBMS_METADATA.GET_DDL('<Object type>', '<Object name>', '<object schema>')
FROM
DUAL; -- How to
SELECT
DBMS_METADATA.GET_DDL('TABLE', 'MY_TABLE', 'MY_SCHEMA')
FROM
DUAL; -- In your case use something like this
Also, You can format the output using dbms_metadata.set_transform_param.
See Oracle documentation for more information on it.
Cheers!!

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.

plsql. is there way to get column ddl?

I use dbms_metadata.get_ddl(...) to get object ddl but it is not generate ddl for column.
is there way to get column ddl oracle 12 version?
thanks
I think following query may usefull for you.
SELECT *
FROM ADMIN.DDL_HISTORY_LOG L
WHERE L.OBJECT_TYPE = 'TABLE'
AND L.DDL = 'ALTER'
AND L.OBJECT_NAME = 'TABLE_NAME' --just change table name here
AND UPPER(L.DDL_SQL) LIKE '%ALTER%TABLE%ADD%'
AND UPPER(L.DDL_SQL) NOT LIKE '%ADD%CONSTRAINT%'
this oracle dictionary is holding all historic DDL statements. so, If you specify your table name, you can get all DDL statements for this table in the past.

how to extract tables attributes like column name , datatype ,nullable for all the tables from the database from oracle pl/sql

Is there any query that can be used to retrive the Tables and its column attributes like column name , datatype, nullable etc for all the tables inside the database
For Oracle Pl/SQL
The Oracle SQL you need would be the following (run as user 'SYS'):
select owner, table_name, column_name, data_type, nullable
from dba_tab_columns;
If you do a desc dba_tab_columns you will get a list of many more columns which may be of interest to you as part of your result set.
You can use a SQL tool (i.e. SQL*Plus) to run this query or you can use PL/SQL to call this query and put the results in PL/SQL variables then print them out via DBMS_OUTPUT.PUT_LINE().
HTH

How to change lots table columns datatypes from one to another in oracle

I have a table with a lots of columns with BLOB type and I need to change it to nvarchar2.
So, to change type I can use following script:
alter table AUDIT_LOG
modify
(
column_name type_name,
column_name2 type_name2
-- etc
);
And to get all columns with given datatype I can use the following:
select column_name, 'NVARCHAR2(4000)'
from all_tab_columns
where table_name = 'TAB_NAME' and data_type = 'BLOB';
But how to join this two scripts into one?
You cannot do DML and DDL operation together in same query. You have to use dynamic SQL in a PL/SQL block
Create a variable and generate the whole alter table query in it.
Execute Immidiate
Refer this and I am sure you will be able to add rest of the logic as per your requirement.
http://www.java2s.com/Tutorial/Oracle/0440__PL-SQL-Statements/EXECUTEIMMEDIATEdynamicsqltoaltersession.htm

TYPE v_id IS table of table#dblink.column%type;

I have table 'test' in schema A and from schema B I want to run the test query.
so created DBlink to A.test and dblink name 'dbl_test' .
now, I am able to query as
select * from test#dbl_test;
but when I try to use TYPE v_id IS TABLE OF test#dbl_test.id%TYPE; in plsql procedure block with cursor, it is giving errors as must declare and dbl_test is another database and not able to access.
When I query the select statement it is working why not for TYPE?
Put the column before the #:
TYPE v_id IS TABLE OF test.id#dbl_test%TYPE;
Database links are used to link two databases, not schemas.
In your case it is unclear if you really need a database link. Are your two schemas in the same database? If they are, you need only give the relevant rights to schema B and he will be able to reference schema A's datatype. I Think GRANT SELECT ON A.TEST TO B should be sufficient to reference its datatype.
Edit: it is actually possible to reference a remote datatype (I didn't know!) See #jonearles' answer.

Resources