Monday, November 12, 2012

dbms_metadata.get_ddl

Have you ever tried to get the DDL for Materialized Views in Oracle using dbms_metadta.get_ddl and got the error ORA-06512?

Here is a little demo the explains the error and then provides a work around.
SQL> create materialized view mv
  2      as
  3      select d.dname, sum(sal) sal_per_dept, avg(sal) avg_per_dept, count(*) count_per_dept
  4     from emp e join dept d on (e.deptno= d.deptno)
  5     group by d.dname;

Materialized view created.


SQL>  select dbms_metadata.get_ddl(object_type, object_name) as mv_ddl
  2      from user_objects where object_type = 'MATERIALIZED VIEW';
ERROR:
ORA-31600: invalid input value MATERIALIZED VIEW for parameter OBJECT_TYPE in
function GET_DDL
ORA-06512: at "SYS.DBMS_METADATA", line 3773
ORA-06512: at "SYS.DBMS_METADATA", line 3828
ORA-06512: at "SYS.DBMS_METADATA", line 5678
ORA-06512: at line 1

no rows selected



The error is caused by the space in the object name MATERIALIZED VIEW. Replace the space with "_" and the object_type will become MATERIALIZED_VIEW and the function will work!

Similarly, DB LINK should be DB_LINK
PACKAGE SPECIFICATION should be PACKAGE_SPEC
and PACKAGE BODY should be PACKAGE_BODY

The object_type parameter will not accept space in the object_type name.

SQL>  set long 50000
SQL> col mv_ddl format a100 word_wrap
SQL>  set pagesize 100
SQL>
SQL> EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'STORAGE',false);

PL/SQL procedure successfully completed.

SQL> EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SEGMENT_ATTRIBUTES',false);

PL/SQL procedure successfully completed.

SQL> EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SQLTERMINATOR',true);

PL/SQL procedure successfully completed.

SQL> select dbms_metadata.get_ddl(replace(object_type,' ','_'), object_name) as mv_ddl
  2  from user_objects where object_type = 'MATERIALIZED VIEW';

MV_DDL
--------------------------------------------------------------------------------
CREATE MATERIALIZED VIEW "SCOTT"."MV" ("DNAME", "SAL_PER_DEPT",
"AVG_PER_DEPT", "COUNT_PER_DEPT")
ORGANIZATION HEAP PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS
LOGGING
TABLESPACE "USERS"
BUILD IMMEDIATE
USING INDEX
REFRESH FORCE ON DEMAND
USING DEFAULT LOCAL ROLLBACK SEGMENT
USING ENFORCED CONSTRAINTS DISABLE QUERY REWRITE
AS select d.dname, sum(sal) sal_per_dept, avg(sal) avg_per_dept, count(*)
count_per_dept
  from emp e join dept d on (e.deptno= d.deptno)
   group by d.dname;


If you are interested in the book "Oracle SQL Scipting". here is the link.
http://www.amazon.com/Oracle-SQL-Scripting-ebook/dp/B00E2T1DVA/ref=sr_1_1?ie=UTF8&qid=1374436123&sr=8-1&keywords=oracle+sql+scripting

Saturday, October 6, 2012

extract Oracle grants

On a database project with the multiple team members doing the development work, it becomes harder to keep track of all the grants given. Before promoting your changes to the higher environments like UAT, TEST or production, you can use the following script to extract the grants on all the database objects in a schema.

SQL> select 'grant ' || privilege || ' on ' || table_name ||
  2        ' to ' || grantee ||
  3        case when grantable = 'YES' then ' with grant option;'
  4             else ';'
  5        end as my_grant
  6  from user_tab_privs_made
  7  order by table_name;


MY_GRANT
-----------------------------------------------------------------------------
grant ALTER on BONUS to SYSTEM;
grant SELECT on DEPT to SCOTT;
grant INSERT on EMP to SCOTT;
grant SELECT on EMP to SCOTT;
grant UPDATE on EMP to SCOTT;
grant REFERENCES on EMP to SCOTT;
grant ON COMMIT REFRESH on EMP to SCOTT;
grant QUERY REWRITE on EMP to SCOTT;
grant DEBUG on EMP to SCOTT;
grant FLASHBACK on EMP to SCOTT;
grant INDEX on EMP to SCOTT;
grant ALTER on EMP to SCOTT;
grant DELETE on EMP to SCOTT;
grant SELECT on EMP to SYSTEM with grant option;
grant EXECUTE on P to HR;

15 rows selected.


Please notice the "with grant option" in the above query result. Also, notice that the procedure grants are also included there. In fact, all the grants will be included here.

The dbms_metadata is not a good idea to use for the grants. The above approach always works!
If you want to get the object grants for more than one schema, use the following version of the query.


SQL>  select 'grant ' || privilege || ' on ' || owner || '.' || table_name ||
  2           ' to ' || grantee ||
  3           case when grantable = 'YES' then ' with grant option;'
  4                else ';'
  5         end as my_grant
  6     from dba_tab_privs
  7      where owner in ('SCOTT','HR')
  8      order by owner,table_name;


MY_GRANT
---------------------------------------------------------------------------------------
grant SELECT on SCOTT.BONUS to HR;
grant UPDATE on SCOTT.BONUS to HR;
grant INSERT on SCOTT.BONUS to HR;
grant ON COMMIT REFRESH on SCOTT.DEPT to PUBLIC;
grant REFERENCES on SCOTT.DEPT to PUBLIC;
grant UPDATE on SCOTT.DEPT to PUBLIC;
grant SELECT on SCOTT.DEPT to PUBLIC;
grant INSERT on SCOTT.DEPT to PUBLIC;
grant INDEX on SCOTT.DEPT to PUBLIC;
grant DELETE on SCOTT.DEPT to PUBLIC;
grant ALTER on SCOTT.DEPT to PUBLIC;
grant QUERY REWRITE on SCOTT.DEPT to PUBLIC;
grant DEBUG on SCOTT.DEPT to PUBLIC;
grant FLASHBACK on SCOTT.DEPT to PUBLIC;
grant UPDATE on SCOTT.DEPT to HR;
grant INSERT on SCOTT.DEPT to HR;
grant SELECT on SCOTT.DEPT to HR;
grant SELECT on SCOTT.LINEITEM to HR;
grant UPDATE on SCOTT.LINEITEM to HR;
grant INSERT on SCOTT.LINEITEM to HR;
grant SELECT on SCOTT.ORDERS to HR;
grant UPDATE on SCOTT.ORDERS to HR;
grant INSERT on SCOTT.ORDERS to HR;
grant UPDATE on SCOTT.PROJ to HR;
grant SELECT on SCOTT.PROJ to HR;
grant INSERT on SCOTT.PROJ to HR;
grant SELECT on SCOTT.SALGRADE to HR;
grant UPDATE on SCOTT.SALGRADE to HR;
grant INSERT on SCOTT.SALGRADE to HR;

29 rows selected.


Please note that we are using "dba_tab_privs" view now and also prefixing the object name with the owner.

Please let me know your feedback!
Thanks

If you are interested in the book "Oracle SQL Scipting". here is the link.
http://www.amazon.com/Oracle-SQL-Scripting-ebook/dp/B00E2T1DVA/ref=sr_1_1?ie=UTF8&qid=1374436123&sr=8-1&keywords=oracle+sql+scripting

Tuesday, September 18, 2012

Recompile Invalid database objects

When we run DDLs against Oracle tables, views and other database objects, some of the dependent objects become invalid.

Here is a script to recompile invalid objects.

The command to recompile is

alter object_type object_name compile;

The only exception is for the package body. The command to recompile the package body is
alter package package_name compile body;

To take that exception into account, here is the script that will general the "alter" for each invalid database object in the current schema.


 select 'alter ' || case when object_type = 'PACKAGE BODY'
                         then 'PACKAGE'
                         else object_type
                    end
 || ' ' || object_name || ' compile ' ||
                  case when object_type = 'PACKAGE BODY'
                        then 'body'
                        else null
                  end || ';' as mysql
 from USER_OBJECTS
 where status = 'INVALID';


Hope, it will save you time.
Thanks
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
If you are interested in the book "Oracle SQL Scipting". here is the link.

 

http://www.amazon.com/dp/B00E2T1DVA





Saturday, September 1, 2012

dbms_metadata

Oracle scripting can be used to extract DDL for database objects. The most common use is for getting the DDL for tables.
Here is a simple demo.

SQL> col my_ddl format a100 word_wrap
SQL> set long 50000
SQL> set pagesize 0
SQL>
SQL> select dbms_metadata.get_ddl('TABLE',table_name,user) AS my_ddl
  2  from all_tables where owner = 'SCOTT'
  3  and table_name = 'DEPT';

CREATE TABLE "SCOTT"."DEPT"
(       "DEPTNO" NUMBER(2,0),
"DNAME" CHAR(14),
"LOC" CHAR(13),
CONSTRAINT "DEPT_DEPTNO_PK" PRIMARY KEY ("DEPTNO")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE
DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS"  ENABLE
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE
DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS"

The above output needs some revision.
Let's get rid of the storage clause.

SQL> EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'STORAGE',false);

PL/SQL procedure successfully completed.

SQL> /

CREATE TABLE "SCOTT"."DEPT"
(       "DEPTNO" NUMBER(2,0),
"DNAME" CHAR(14),
"LOC" CHAR(13),
CONSTRAINT "DEPT_DEPTNO_PK" PRIMARY KEY ("DEPTNO")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
TABLESPACE "USERS"  ENABLE
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
TABLESPACE "USERS"


We also want to get rid of segment attributes, put a semicolon at the end as sql terminator and make the output pretty!
SQL> EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SEGMENT_ATTRIBUTES',false);

PL/SQL procedure successfully completed.

SQL> EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SQLTERMINATOR',true);

PL/SQL procedure successfully completed.

SQL> EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'PRETTY',true);

PL/SQL procedure successfully completed.

SQL> /

CREATE TABLE "SCOTT"."DEPT"
(       "DEPTNO" NUMBER(2,0),
"DNAME" CHAR(14),
"LOC" CHAR(13),
CONSTRAINT "DEPT_DEPTNO_PK" PRIMARY KEY ("DEPTNO") ENABLE
 ) ;

 

Now, the output looks little better!

If we want to get the DDL for all the tables in the schema, let's remove the table_name filter.

 

SQL> select dbms_metadata.get_ddl('TABLE',table_name,user) AS my_ddl
  2  from all_tables where owner = 'SCOTT';

CREATE TABLE "SCOTT"."EMP"
(       "EMPNO" NUMBER(4,0) NOT NULL ENABLE,
"ENAME" CHAR(10),
"JOB" CHAR(9),
"MGR" NUMBER(4,0),
"HIREDATE" DATE,
"SAL" NUMBER(7,2),
"COMM" NUMBER(7,2),
"DEPTNO" NUMBER(2,0),
"PROJNO" NUMBER,
"LOADSEQ" NUMBER,
CONSTRAINT "EMP_DEPTNO_FK" FOREIGN KEY ("DEPTNO")
REFERENCES "SCOTT"."DEPT" ("DEPTNO") ENABLE
   ) ;

CREATE TABLE "SCOTT"."SALGRADE"
(       "GRADE" NUMBER,
"LOSAL" NUMBER,
"HISAL" NUMBER
   ) ;

CREATE TABLE "SCOTT"."DEPT"
(       "DEPTNO" NUMBER(2,0),
"DNAME" CHAR(14),
"LOC" CHAR(13),
CONSTRAINT "DEPT_DEPTNO_PK" PRIMARY KEY ("DEPTNO") ENABLE
 ) ;


Looks good? Well, the only catch is, the table creation script may not be in the right order. In the above case, we want the dept table to be created first and then emp table. Otherwise, we are going to get error.
We will try to figure out how to achieve that in the next post!
Meanwhile, if you have a better idea, please weigh in!


==========================================================
If you are interested in the book "Oracle SQL Scipting". here is the link.
http://www.amazon.com/Oracle-SQL-Scripting-ebook/dp/B00E2T1DVA/ref=sr_1_1?ie=UTF8&qid=1374436123&sr=8-1&keywords=oracle+sql+scripting

Friday, June 1, 2012

SQL Loader Log Analyzer

If you are very heavy user of SQL Loader to load data into Oracle tables, then you can benefit from the tool called "SQL Loader Load Analyzer" from PoojaSoft.

The tool analyzes all the log files and bad files and gives you nice break downs of all the Oracle errors and the sample data from bad file that caused the error. This will help you debug your data issues very fast.

It also creates three reports that give you the summary of the data load and sample error report which you can share with your team or other department.

The tool is in beta phase and available for free trial download at www,poojasoft.com



Thanks



Oracle Scripting


Oracle scripting is very powerful to automate lots of SQL tasks. It can save you lots of time, boost your productivity and make your life easier!

If you are Oracle DBA or Oracle Developer, SQL scripting will be very useful tool for you. Oracle script will write your SQL statements for you using very easy to understand but powerful technique.

Here is a simple example. Let's say, you want to drop  all your tables in the current schema. You have lots of tables in the schema. So, you don't want to type individual drop table statements.

Here is a simple SQL script that will do that for you!

select 'drop  table ' || table_name || 
       ' cascade constraints;' as my_trunc_stmt
from user_tables;

The output of the above query will be as shown below.

MY_TRUNC_STMT
---------------------------------------------------------------
drop  table NEW_DEPT cascade constraints;
drop  table NEW_EMP cascade constraints;
drop  table NEW_T cascade constraints;

You can use some SQL*PLUS commands to disable heading, feedback etc and spool the output to another file and run that file to drop all the tables in the schema!

set feedback off
set heading off
spool c:\temp\a.sql
select 'drop  table ' || table_name || 
        ' cascade constraints;' as my_trunc_stmt
from user_tables;
spool off
set feedback on
set heading off

The output will go to the file c:\temp\a.sql.
Here is the content of the file.

SQL> get c:\temp\a.sql
  1  SQL>
  2  SQL> select 'drop  table ' || table_name || ' cascade constraints;' as my_trunc_stmt
  3    2  from user_tables;
  4  drop  table NEW_DEPT cascade constraints;
  5  drop  table NEW_EMP cascade constraints;
  6  drop  table NEW_T cascade constraints;
  7  3 rows selected.
  8* SQL> spool off

You can either copy/paste the content of the file, or you can run it to drop all the tables.

SQL> @c:\temp\a.sql
SQL> drop  table NEW_DEPT cascade constraints;

Table dropped.

SQL> drop  table NEW_EMP cascade constraints;

Table dropped.

SQL> drop  table NEW_T cascade constraints;

Table dropped.

With the power of scripting, the number of tables don't matter.

Hope you like this simple demo.

==================================================================
If you are interested in the book "Oracle SQL Scipting". here is the link.

http://www.amazon.com/Oracle-SQL-Scripting-ebook/dp/B00E2T1DVA/ref=sr_1_1?ie=UTF8&qid=1374436123&sr=8-1&keywords=oracle+sql+scripting