Spool Command with dbms_metadata.GET_DDL function
Hi All,
My requirement is before applying a patch on the Database Schema, I need to take a backup of the db objects DDL script, so that incase if I want to rollback I can use it. what I am trying to use is the dbms_metadata.GET_DDL function to get the DDL script and with the help of spool command store that in a text file.
The problem I am encountering is the whole script runs without an error but whne I check the spool file for certain big packages the DDL script is not fully exported. The below is the script that I make use of to generate the DDL script.
set heading off
set feedback off
set echo off
set term off
set newpage none
set space 0
set trimout on
set TRIMSPOOL ON
column c1 format a4000
set long 99999
set lines 1000
exec DBMS_METADATA.SET_TRANSFORM_PARAM (DBMS_METADATA.SESSION_TRANSFORM, 'PRETTY', true);
spool c:\Rollback.sql
select dbms_metadata.GET_DDL('FUNCTION',u.object_name) || '/' c1
from user_objects u
where object_type = 'FUNCTION'
and object_name in ('FN_CALCMASTERDATA_AGE','FUNC_ASSIGNBENEFITSNONFLEX','FUNC_CHECKNEWEMPLOYEE');
select dbms_metadata.GET_DDL('PACKAGE_SPEC',u.object_name) || '/' c1
from user_objects u
where object_type = 'PACKAGE'
and object_name in('PKGT_BEN_YEAR_DETL','PKGT_BENS_TEST');
select dbms_metadata.GET_DDL('PACKAGE_BODY',u.object_name) || '/' c1
from user_objects u
where object_type = 'PACKAGE'
and object_name in ('PKGT_BEN_YEAR_DETL','PKGT_BENS_TEST','PKGT_FLEX_INITIALIZATION','PKGT_EMP');
spool off
Note: Database Oracle 10 g,
Client Machine Windows XP from where the script is ran.
Is there any limitation in amount of data that can be spooled? or is there is any better way to accomplish this task, please help.
Thanks
Saami
set long longer ;-)
set long 1000000 longchunksize 1000000 linesize 32000 pagesize 0
Similar Messages
-
Problem with DBMS_METADATA.GET_DDL and subpartitions
I have problems with DBMS_METADATA.GET_DDL in a 11gR2 (11.2.0.3) database on Linux. The following example shows the problem:
CREATE TABLE Test_tbl
C1 NUMBER(14),
C2 NUMBER(14),
C3 NUMBER(14)
PARTITION BY RANGE (C1)
SUBPARTITION BY LIST (C2)
PARTITION Part1 VALUES LESS THAN (1000)
, PARTITION Part2 VALUES LESS THAN (2000)
CREATE INDEX Test_idx ON Test_tbl (C3) LOCAL;
When I execute
SELECT DBMS_METADATA.GET_DDL('INDEX','TEST_IDX') from dual;
on a 10g database (10.2.0.2.0) on Solaris Sparc and on a 11.1.0.7 on Windows, it gives the correct DDL statement within some seconds.
On the 11.2.0.3 database (Linux) it hangs forever.
SQL> SELECT DBMS_METADATA.GET_DDL('INDEX','TEST_IDX') from dual;
^CERROR:
ORA-01013: user requested cancel of current operation
This behavior only seems to occur in combination with local indexes and subpartitions.
Anyone any idea?
Thx, Carstenuser592634 wrote:
I have problems with DBMS_METADATA.GET_DDL in a 11gR2 (11.2.0.3) database on Linux. The following example shows the problem:
CREATE TABLE Test_tbl
C1 NUMBER(14),
C2 NUMBER(14),
C3 NUMBER(14)
PARTITION BY RANGE (C1)
SUBPARTITION BY LIST (C2)
PARTITION Part1 VALUES LESS THAN (1000)
, PARTITION Part2 VALUES LESS THAN (2000)
CREATE INDEX Test_idx ON Test_tbl (C3) LOCAL;
When I execute
SELECT DBMS_METADATA.GET_DDL('INDEX','TEST_IDX') from dual;
on a 10g database (10.2.0.2.0) on Solaris Sparc and on a 11.1.0.7 on Windows, it gives the correct DDL statement within some seconds.
On the 11.2.0.3 database (Linux) it hangs forever.
SQL> SELECT DBMS_METADATA.GET_DDL('INDEX','TEST_IDX') from dual;
^CERROR:
ORA-01013: user requested cancel of current operationWhy did you cencel this operation? Usually it takes time. If its taking more then it would be better to gather dictionary stats once.
SQL>exec DBMS_STATS.GATHER_DICTIONARY_STATS;
>
This behavior only seems to occur in combination with local indexes and subpartitions.
Anyone any idea?
Thx, CarstenI didn't get any problem in 11.2.0.1 database:
SQL> CREATE TABLE Test_tbl
2 (
3 C1 NUMBER(14),
4 C2 NUMBER(14),
5 C3 NUMBER(14)
6 )
7 PARTITION BY RANGE (C1)
8 SUBPARTITION BY LIST (C2)
9 (
10 PARTITION Part1 VALUES LESS THAN (1000)
11 , PARTITION Part2 VALUES LESS THAN (2000)
12 );
Table created.
SQL>
SQL> CREATE INDEX Test_idx ON Test_tbl (C3) LOCAL;
Index created.
SQL>
SQL> SELECT DBMS_METADATA.GET_DDL('INDEX','TEST_IDX') from dual;
DBMS_METADATA.GET_DDL('INDEX','TEST_IDX')
CREATE INDEX "SYS"."TEST_IDX" ON "SYS"."TEST_TBL" ("C3")
PCTFREE 10 INITRAEdited by: 909592 on Mar 22, 2012 9:01 PM -
Problem with dbms_metadata.get_ddl
When I issue following statement:
Select DBMS_METADATA.GET_DDL ('TABLE', 'ACQ_OPSC')
From Dual;
I get these errors.
Error:
ORA-06502: PL/SQL: Numeric or Value Error.
LPX-00210: Expected '<' instead of 'n'
ORA-06512: at "sys.utl_xml", line 0
bunch more ORA-06512 errors.
Oracle version: Oracle9i Enterprise Edition Release 9.2.0.1.0 - 64bit Production
What is the problem?Please check dbmssml.sql and initmeta.sql under $ORACLE_HOME/rdbms/admin if you have changed your oracle home after installation to patch - A common practice though not blessed by oracle. These files will have value of original ORACLE_HOME hard coded in them. You have to update them manually with the new ORACLE_HOME. Once you update it run them on each database that is using this ORACLE_HOME.
-
How to Use SPOOL Command with Bind Variables
For the Following SPOOL I want the GEN_DATE to be entered by the User at the time of execution of the SQL Script. Is it possible in SPOOL Command.
set colsep , -- separate columns with a comma
set pagesize 1000 -- get rid of disturbing ---- between pages
set heading on -- Print column heading
set trimspool on -- remove trailing blanks. eliminating the spaces up to eol
set linesize 700 -- line size should be the sum of the column widths
spool spool_results.csv
SELECT *
FROM GUI_SITE_JOURNAL
WHERE GENDATE_ BETWEEN '2012-11-01 00:00:00:00000' AND '2012-11-02 00:00:00:00000'* ORDER BY GEN_DATE;
spool off;
The reason is to give the ability to user so that he can enter any range without modifying the code of the script.
Can Any one help me please.
Edited by: user10903866 on Feb 18, 2013 7:44 PMHi,
user10903866 wrote:
For the Following SPOOL I want the GEN_DATE to be entered by the User at the time of execution of the SQL Script. Is it possible in SPOOL Command.Do you want the user input in the SPOOL command, or do you want it in the query?
set colsep , -- separate columns with a comma
set pagesize 1000 -- get rid of disturbing ---- between pages
set heading on -- Print column heading
set trimspool on -- remove trailing blanks. eliminating the spaces up to eol
set linesize 700 -- line size should be the sum of the column widths
spool spool_results.csv
SELECT *
FROM GUI_SITE_JOURNAL
WHERE GENDATE_ BETWEEN '2012-11-01 00:00:00:00000' AND '2012-11-02 00:00:00:00000'* ORDER BY GEN_DATE;What is the data type oif gen_date?
If it's a string, that's a big mistake. Information about dates belongs in DATE columns.
If it's a DATE, then don't try to compare it to strings, such as '2012-11-01 00:00:00:00000' .
spool off;
The reason is to give the ability to user so that he can enter any range without modifying the code of the script.
Can Any one help me please.One way to do that is with substitution variables:
SET VERIFY OFF
ACCEPT start_gen_date PROMPT "Starting date (e.g., 2013-02-18 23:00:00.00000): "
ACCEPT end_gen_date PROMPT "Ending date (e.g., 2013-02-18 23:59:59.99999): "
SPOOL spool_results.csv
SELECT *
FROM gui_site_journal
WHERE gen_date BETWEEN '&start_gen_date'
AND '&end_gen_date'
ORDER BY gen_date;
SPOOL OFFThere are security considerations. Substitution variables give devious users the power to issue any SQL command, such as DROP TABLE. Users that have SQL*Plus access already have that power, anyway. -
DBMS_METADATA.GET_DDL doesn't include function parameters
Sometimes, when I call DBMS_METADATA.GET_DDL to get the DDL statement for a function (which we always do to ensure we're starting from the source that's actually in production), the returned text doesn't include the parameters. Other times, it does, and I'm not able to see any pattern.
I was going to just make a function and show the output, but that did show the parameters. Then I tried calling GET_DDL while logged in as SYS as SYSDBA on my development database, and I still did not get the parameters, so I don't think it's a permissions issue.
Does anyone have any idea what's going on here?It's really difficult to show you exactly what I'm doing when I can't consistently reproduce the problem, as I described.
But here, I'll try.
declare
the_ddl clob;
begin
the_ddl := dbms_metadata.get_ddl('FUNCTION', '*****', '*****');
dbms_output.enable(null);
dbms_output.put_line(the_ddl);
end;
/Sometimes, it produces output like this:
CREATE OR REPLACE FUNCTION "*****"."*****" (
***** in varchar2,
***** in number,
) return *****
...And other times, it produces output like this:
CREATE OR REPLACE FUNCTION "*****"."*****" return *****
...I am not able to determine any consistent reason for this. In our production database, I can log in as myself and get the parameters for a specific function, but another user (that our automated process uses) doesn't see the parameters. In my development database, I log in as myself and I do not see the parameters, but I still don't see them when I log in as SYS AS SYSDBA.
For what it's worth, if I recreate the function on my development database, then subsequent calls do show the parameters. -
Dbms_metadata.get_ddl and select_catalog_role
Hi,
We have been requested to give support staff the ability to see table triggers, stored procedures, etc only for specific schemas and they don't have access to the schema password. We can't give them SELECT_CATALOG_ROLE because they are not allowed to see all schemas. I wish there were a way to grant a version of SELECT_CATALOG_ROLE for only certain schemas......
I've played around with dbms_metadata.get_ddl with no luck (as per the documentation, but just had to try it anyway). I've even considered the script below, but it requires creating a view under the SYS schema and I can't figure out how to add triggers to the view.
Any ideas would be greatly appreciated!
Thanks,
Susan
accept 1 prompt "Enter Owner:"
create or replace view all_dev_source
(OWNER, NAME, TYPE, LINE, TEXT)
as
select u.name, o.name,
decode(o.type#, 7, 'PROCEDURE', 8, 'FUNCTION', 9, 'PACKAGE',
11, 'PACKAGE BODY', 13, 'TYPE', 14, 'TYPE BODY',
'UNDEFINED'),
s.line, s.source
from sys.obj$ o, sys.source$ s, sys.user$ u
where
u.name = upper('&&1') and
o.obj# = s.obj#
and o.owner# = u.user#
and o.type# in (7, 8, 9, 11, 13, 14)
and
o.owner# in (userenv('SCHEMAID'), 1 /* PUBLIC */)
or
(o.type# = 7 or o.type# = 8 or o.type# = 9)
and
o.obj# in (select obj# from sys.objauth$
where grantee# in (select kzsrorol from x$kzsro)
and privilege# = 12 /* EXECUTE */)
or
exists
select null from sys.sysauth$
where grantee# in (select kzsrorol from x$kzsro)
and
/* procedure */
(o.type# = 7 or o.type# = 8 or o.type# = 9)
or
privilege# = -144 /* EXECUTE ANY PROCEDURE */
or
privilege# = -141 /* CREATE ANY PROCEDURE */
or
/* package body */
o.type# = 11 or
privilege# = -141 /* CREATE ANY PROCEDURE */
or
/* type */
o.type# = 13
or
privilege# = -184 /* EXECUTE ANY TYPE */
or
privilege# = -181 /* CREATE ANY TYPE */
or
/* type body */
o.type# = 14 and
privilege# = -181 /* CREATE ANY TYPE */
union
select u.name, o.name, 'JAVA SOURCE', s.joxftlno, s.joxftsrc
from sys.obj$ o, x$joxfs s, sys.user$ u
where
u.name = upper('&&1') and
o.obj# = s.joxftobn
and o.owner# = u.user#
and o.type# = 28
and
o.owner# in (userenv('SCHEMAID'), 1 /* PUBLIC */)
or
o.obj# in (select obj# from sys.objauth$
where grantee# in (select kzsrorol from x$kzsro)
and privilege# = 12 /* EXECUTE */)
or
exists
select null from sys.sysauth$
where grantee# in (select kzsrorol from x$kzsro)
and
/* procedure */
privilege# = -144 /* EXECUTE ANY PROCEDURE */
or
privilege# = -141 /* CREATE ANY PROCEDURE */
comment on table all_dev_source is
'Current source on stored objects that user is allowed to create'
comment on column all_dev_source.OWNER is
'Owner of the object'
comment on column all_dev_source.NAME is
'Name of the object'
comment on column all_dev_source.TYPE is
'Type of the object: "TYPE", "TYPE BODY", "PROCEDURE", "FUNCTION",
"PACKAGE", "PACKAGE BODY" or "JAVA SOURCE"'
comment on column all_dev_source.LINE is
'Line number of this line of source'
comment on column all_dev_source.TEXT is
'Source text'
grant select on all_dev_source to <username>
/user632322 wrote:
I think I am misunderstanding your reply becauseI had to be more specific. By "privileged user" I meant SYS. SELECT_CATALOG_ROLE is a role itself, so it will be ignored by definer rights SP/SF. That is why it has to be owned by privileged user SYS:
SQL> select * from v$version
2 /
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Prod
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for 32-bit Windows: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
SQL> show user
USER is "SYS"
SQL> create user u1 identified by u1 default tablespace users quota unlimited on users
2 /
User created.
SQL> grant create session to u1
2 /
Grant succeeded.
SQL> create or replace
2 function get_ddl(
3 p_type varchar2,
4 p_object varchar2,
5 p_owner varchar2
6 )
7 return clob
8 is
9 begin
10 return dbms_metadata.get_ddl(p_type,p_object,p_owner);
11 end;
12 /
Function created.
SQL> grant execute on get_ddl to u1
2 /
Grant succeeded.
SQL> connect u1/u1
Connected.
SQL> set serveroutput on
SQL> exec dbms_output.put_line(dbms_metadata.get_ddl('TABLE','EMP','SCOTT'));
BEGIN dbms_output.put_line(dbms_metadata.get_ddl('TABLE','EMP','SCOTT')); END;
ERROR at line 1:
ORA-31603: object "EMP" of type TABLE not found in schema "SCOTT"
ORA-06512: at "SYS.DBMS_SYS_ERROR", line 105
ORA-06512: at "SYS.DBMS_METADATA", line 2806
ORA-06512: at "SYS.DBMS_METADATA", line 4333
ORA-06512: at line 1
SQL> exec dbms_output.put_line(sys.get_ddl('TABLE','EMP','SCOTT'));
CREATE TABLE "SCOTT"."EMP"
( "EMPNO" NUMBER(4,0),
"ENAME" VARCHAR2(10),
"JOB" VARCHAR2(9),
"MGR" NUMBER(4,0),
"HIREDATE" DATE,
"SAL"
NUMBER(7,2),
"COMM" NUMBER(7,2),
"DEPTNO" NUMBER(2,0),
CONSTRAINT
"PK_EMP" PRIMARY KEY ("EMPNO")
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)
TABLESPACE "USERS" ENABLE,
CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO")
REFERENCES "SCOTT"."DEPT" ("DEPTNO") ENABLE
) 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)
TABLESPACE "USERS"
PL/SQL procedure successfully completed.
SQL> Now one more correction to my previous reply. Driver table shoudl not be owned by SYS (that would be bad practice). Create it in some other schema. Just make sure your "support staff" has no access to it.
SY. -
Bug in DBMS_METADATA.get_ddl?
If you create the functions:
CREATE OR REPLACE FUNCTION testingit1 RETURN NUMBER IS
tmpVar NUMBER;
BEGIN
tmpVar := 0;
RETURN tmpVar;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END testingit1;
and
CREATE OR REPLACE FUNCTION
testingit2 RETURN NUMBER IS
tmpVar NUMBER;
BEGIN
tmpVar := 0;
RETURN tmpVar;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END testingit2;
And use DBMS_METADATA.get_ddl to return the code. When the function was created with the name on a seperate line (testingit2) the code returned has the name repeated
e..g., SELECT DBMS_METADATA.get_ddl('FUNCTION', 'TESTINGIT2')
FROM dual
returns
"CREATE OR REPLACE FUNCTION "TESTUSER"."TESTINGIT2"
testingit2 RETURN NUMBER IS
tmpVar NUMBER; ..."
Anyone seen this before and know how to fix it?using some sort of XML transform? Yes,
SQL> create or replace function get_code return clob is
2 l_xml XMLTYPE ;
3 l_xsl XMLTYPE ;
4 begin
5 select XMLTYPE(dbms_metadata.get_xml('FUNCTION', 'TESTINGIT2')) into l_xml from dual ;
6 l_xsl := XMLTYPE('<?xml version="1.0"?>'||
7 '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"'||
8 ' version="1.0"'||
9 ' result-ns=""'||
10 ' indent="no">'||
11 '<xsl:output method="text" media-type="text/text" indent="no"/>'||
12 '<xsl:template match="/">'||
13 'CREATE OR REPLACE <xsl:for-each select="//SOURCE_LINES/SOURCE_LINES_ITEM">'||
14 '<xsl:value-of select="./SOURCE" />'||
15 ' </xsl:for-each>'||
16 '</xsl:template>'||
17 '</xsl:stylesheet>') ;
18 return(l_xml.transform(l_xsl).getCLOBVal()) ;
19 end ;
20 /
Function created.
SQL> select get_code from dual ;
GET_CODE
CREATE OR REPLACE FUNCTION
testingit2 RETURN NUMBER IS
tmpVar NUMBER;
begin
null ;
end ;
1 row selected.
SQL> -
DBMS_METADATA.GET_DDL Not working
Hi All,
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
I am creating table as:
create table XX as select * from fnd;
select DBMS_METADATA.GET_DDL('TABLE','xx') from dualI get the below given error:
Error starting at line 1 in command:
select DBMS_METADATA.GET_DDL('TABLE','xx') from dual
Error report:
SQL Error: ORA-31603: object "xx" of type TABLE not found in schema "A395513"
ORA-06512: at "SYS.DBMS_METADATA", line 1548
ORA-06512: at "SYS.DBMS_METADATA", line 1585
ORA-06512: at "SYS.DBMS_METADATA", line 1902
ORA-06512: at "SYS.DBMS_METADATA", line 2793
ORA-06512: at "SYS.DBMS_METADATA", line 4333
ORA-06512: at line 1
31603. 00000 - "object \"%s\" of type %s not found in schema \"%s\""
*Cause: The specified object was not found in the database.
*Action: Correct the object specification and try the call again.Oracle names are stored in dictionary in upper case (unless quoted name is used at create time). Use XX as table name:
SQL> create table XX as select * from emp;
Table created.
SQL> set long 1000
SQL> select DBMS_METADATA.GET_DDL('TABLE','xx') from dual;
ERROR:
ORA-31603: object "xx" of type TABLE not found in schema "SCOTT"
ORA-06512: at "SYS.DBMS_METADATA", line 1548
ORA-06512: at "SYS.DBMS_METADATA", line 1585
ORA-06512: at "SYS.DBMS_METADATA", line 1902
ORA-06512: at "SYS.DBMS_METADATA", line 2793
ORA-06512: at "SYS.DBMS_METADATA", line 4333
ORA-06512: at line 1
no rows selected
SQL> select DBMS_METADATA.GET_DDL('TABLE','XX') from dual;
DBMS_METADATA.GET_DDL('TABLE','XX')
CREATE TABLE "SCOTT"."XX"
( "EMPNO" NUMBER(4,0),
"ENAME" VARCHAR2(10),
"JOB" VARCHAR2(9),
"MGR" NUMBER(4,0),
"HIREDATE" DATE,
"SAL" NUMBER(7,2),
"COMM" NUMBER(7,2),
"DEPTNO" NUMBER(2,0)
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
DBMS_METADATA.GET_DDL('TABLE','XX')
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "USERS"
SQL> SY. -
How to generate text file using spool command
How can I use a SPOOL command with DBMS_OUTPUT.PUT_LINE with PL/SQL to produce text file on oracle client machine.
You could try using a REF CURSOR as an alternative. Bare bones example listed below:
--- SQL Script
SET FEEDBACK OFF
COLUMN first_name FORMAT A25
COLUMN last_name FORMAT A25
VARIABLE example_data REFCURSOR
BEGIN
example.get_data(:example_data);
END;
SPOOL example_data.txt
PRINT example_data
SPOOL OFF
-----Output in example_data.txt
FIRST_NAME LAST_NAME
First Name 1 Last Name 1
First Name 2 Last Name 2
First Name 3 Last Name 3
First Name 4 Last Name 4
First Name 5 Last Name 5
First Name 6 Last Name 6
First Name 7 Last Name 7
First Name 8 Last Name 8
First Name 9 Last Name 9
First Name 10 Last Name 10
-----Example package used in SQL Script
CREATE OR REPLACE PACKAGE example
IS
TYPE result_set IS REF CURSOR;
PROCEDURE get_data
p_result_set OUT result_set
END;
SHOW ERRORS
CREATE OR REPLACE PACKAGE BODY example
IS
PROCEDURE get_data
p_result_set OUT result_set
IS
BEGIN
-- replace this with your query
OPEN p_result_set FOR
WITH fake_data AS (
SELECT
'First Name ' || level first_name,
'Last Name ' || level last_name
FROM DUAL
CONNECT BY LEVEL <= 10
SELECT *
FROM fake_data
END;
END;
SHOW ERRORS
SPOOL OFF -
Use of DBMS_METADATA.GET_DDL with respect to triggers
We are very pleased with DMBS_METADATA for punching DDLs in general, We use the following to create executable scripts for recreating any object in our databases.
SELECT DBMS_METADATA.GET_DDL('OBJECT_TYPE', 'OBJECT_NAME', 'SAINADM' ) from dual;
In most types of object, the DDL produced can be executed without errors, providing that the original target object was well founded. However, we have found that in the case of a triggers, the DDL produced does not function for the following reason:
EXAMPLE
-- This set of instructions is produced by a metascript too complicated to show here
SPOOL TRIGGER_NAME.trg
SELECT DBMS_METADATA.GET_DDL('TRIGGER', 'TRIGGER_NAME', 'SCHEMA_NAME' ) txt
FROM DUAL;
SPOOL OFF
END OF EXAMPLE
This will produce the following output, spooled to the file TRIGGER_NAME.trg
OUTPUT
-- we have anonymised our object names
-- the syntax is what I would like you to focus on
CREATE OR REPLACE TRIGGER "SCHEMA_NAME"."TRIGGER_NAME"
BEFORE INSERT on SCHEMA_NAME.TABLE_NAME FOR EACH ROW
BEGIN
select SCHEMA_NAME.SEQUENCE_NAME.nextval
into :new.colname
from dual;
END;
ALTER TRIGGER SCHEMA_NAME"."TRIGGER_NAME" ENABLE
_END OF OUTPUT_
Note what has happened.
1. The trigger DDL has been produced
2. So has a the ALTER TRIGGER ... ENABLE
3. BUT => between the trigger DDL and the alter trigger statement the is no slash (of course, because naturally DBMS_METADATA does not produce such characters). BUT this is problematic, because the combination of the Create trigger statement and the alter trigger statement, without a slash in between means that the spool file is atomically illegal. Because in real life we need a slash between the two staements.
Why is this a problem for us?
- Because we are about to introduce the automation of object changes between our "Development" "Integration" "Quality Assurance" and "Production" databases based on the execution of files produced by DBMS_METADATA.GET_DDL. For every other type of object, the file produced is well founded and executable DDL that will create the object. But in the case of triggers, the existence of the alter trigger statement renders the foregoing create trigger statement unexecutable.
The use of DMBS_METADATA is more or less vanilla. That is to say, there appears to be no scope for instructing DBMS_METADATA to abstain form including the alter trigger statement
SELECT DBMS_METADATA.GET_DDL('TRIGGER', 'TRIGGER_NAME', 'SCHEMA_NAME' ) txt
Here are my questions
1. How can we punch the DDL for triggers without bringing the alter trigger statement
2. Alternatively, how can we automate the insertion of the slash character in between the CREATE TRIGGER and the ALTER TRIGGER in the original metascript that creates the example spool file.
Thanks for your attention. Every response will be welcomed.
Edited by: user10248070 on Mar 2, 2010 3:54 AMYou can use
dbms_metadata.set_transform_param( DBMS_METADATA.SESSION_TRANSFORM, 'SQLTERMINATOR', TRUE );See http://download.oracle.com/docs/cd/E11882_01/appdev.112/e10577/d_metada.htm#BGBJBFGE.
Example:
SQL> set long 1000
SQL> set heading off
SQL> select * from v$version;
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Product
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
SQL> exec dbms_metadata.set_transform_param( DBMS_METADATA.SESSION_TRANSFORM, 'SQLTERMINATOR', TRUE );
PL/SQL procedure successfully completed.
SQL> SELECT DBMS_METADATA.GET_DDL('TRIGGER', 'UPDATE_JOB_HISTORY', 'HR' ) txt
2 FROM DUAL;
CREATE OR REPLACE TRIGGER "HR"."UPDATE_JOB_HISTORY"
AFTER UPDATE OF job_id, department_id ON employees
FOR EACH ROW
BEGIN
add_job_history(:old.employee_id, :old.hire_date, sysdate,
:old.job_id, :old.department_id);
END;
ALTER TRIGGER "HR"."UPDATE_JOB_HISTORY" ENABLE;Edited by: P. Forstmann on 2 mars 2010 13:48 -
I want to mirror a schema to a existing schema by creating DDL and recreate on the other schema with same name.
I wrote the code below:
create or replace
PROCEDURE SCHEMA_A."MAI__DWHMIRROR"
AS
v_sqlstatement CLOB:='bos';
str varchar2(3999);
BEGIN
select
replace(
replace(replace(
replace(DBMS_METADATA.GET_DDL('TABLE','XXXX','SCHEMA_A'),'(CLOB)',''),';','')
,'SCHEMA_A'
,'SCHEMA_B'
into v_sqlstatement
from dual;
select CAST(v_sqlstatement AS VARCHAR2(3999)) into str from dual;
execute immediate ''||str;
END;
And Executing this block with below code:
set serveroutput on
begin
SCHEMA_A.MAI__DWHMIRROR;
end;
But still getting the following error code:
Error report:
ORA-01031: insufficient privileges
ORA-06512: at "SCHEMA_A.MAI__DWHMIRROR", line 47
ORA-06512: at line 2
01031. 00000 - "insufficient privileges"
*Cause: An attempt was made to change the current username or password
without the appropriate privilege. This error also occurs if
attempting to install a database without the necessary operating
system privileges.
When Trusted Oracle is configure in DBMS MAC, this error may occur
if the user was granted the necessary privilege at a higher label
than the current login.
*Action: Ask the database administrator to perform the operation or grant
the required privileges.
For Trusted Oracle users getting this error although granted the
the appropriate privilege at a higher label, ask the database
administrator to regrant the privilege at the appropriate label.user5199319 wrote:
USER has DBA Role
when all else fails Read The Fine Manual
DBMS_METADATA -
Getting table script using dbms_metadata.get_ddl, but with clob field
So, Oracle 11g R2..
I'm using dbms_metadata.get_ddl to get table scripts and it's working fine..
now, I have a table with clob field, and it's not working... I got an 'missing right parenthesis (ora-0907)' error...
I could paste a script that I got, but I don't think it makes any sense..
does anybody have some experience on using this package on clob tables?
tnxthis is script that I got... it's long, and it looks like it's not good
CREATE TABLE "COMMON"."TEST_AAA2"
( "ID" NUMBER(10,0),
"TEKST" VARCHAR2(200 CHAR),
"UPDATESTAMP" DATE,
"OBJEKAT" CLOB,
CONSTRAINT "TEST_PART_PK2" PRIMARY KEY ("ID")
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"
ALTER INDEX "COMMON"."TEST_PART_PK2" UNUSABLE ENABLE
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "COMMON_DATA"
LOB ("OBJEKAT") STORE AS BASICFILE (
ENABLE STORAGE IN ROW CHUNK 8192 RETENTION
NOCACHE LOGGING
STORAGE(
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT))
PARTITION BY RANGE ("UPDATESTAMP")
(PARTITION "P_201012" VALUES LESS THAN (TO_DATE(' 2011-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
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 "COMMON_DATA"
LOB ("OBJEKAT") STORE AS BASICFILE (
TABLESPACE "USERS" ENABLE STORAGE IN ROW CHUNK 8192 PCTVERSION 10
NOCACHE 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)) NOCOMPRESS ,
PARTITION "P_201101" VALUES LESS THAN (TO_DATE(' 2011-02-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
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 "COMMON_DATA"
LOB ("OBJEKAT") STORE AS BASICFILE (
TABLESPACE "USERS" ENABLE STORAGE IN ROW CHUNK 8192 PCTVERSION 10
NOCACHE 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)) NOCOMPRESS ,
PARTITION "P_201102" VALUES LESS THAN (TO_DATE(' 2011-03-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
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 "COMMON_ARCHIVE"
LOB ("OBJEKAT") STORE AS BASICFILE (
TABLESPACE "COMMON_ARCHIVE" ENABLE STORAGE IN ROW CHUNK 8192 RETENTION
NOCACHE 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)) NOCOMPRESS ,
PARTITION "P_201103" VALUES LESS THAN (TO_DATE(' 2011-04-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
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 "COMMON_DATA"
LOB ("OBJEKAT") STORE AS BASICFILE (
TABLESPACE "USERS" ENABLE STORAGE IN ROW CHUNK 8192 PCTVERSION 10
NOCACHE 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)) NOCOMPRESS ,
PARTITION "P_201104" VALUES LESS THAN (TO_DATE(' 2011-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
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 "COMMON_ARCHIVE"
LOB ("OBJEKAT") STORE AS BASICFILE (
TABLESPACE "COMMON_ARCHIVE" ENABLE STORAGE IN ROW CHUNK 8192 RETENTION
NOCACHE 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)) NOCOMPRESS ,
PARTITION "P_201105" VALUES LESS THAN (TO_DATE(' 2011-06-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
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 "COMMON_DATA"
LOB ("OBJEKAT") STORE AS BASICFILE (
TABLESPACE "USERS" ENABLE STORAGE IN ROW CHUNK 8192 PCTVERSION 10
NOCACHE 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)) NOCOMPRESS ,
PARTITION "P_201106" VALUES LESS THAN (TO_DATE(' 2011-07-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
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 "COMMON_ARCHIVE"
LOB ("OBJEKAT") STORE AS BASICFILE (
TABLESPACE "COMMON_ARCHIVE" ENABLE STORAGE IN ROW CHUNK 8192 RETENTION
NOCACHE 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)) NOCOMPRESS ,
PARTITION "P_201107" VALUES LESS THAN (TO_DATE(' 2011-08-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
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 "COMMON_DATA"
LOB ("OBJEKAT") STORE AS BASICFILE (
TABLESPACE "COMMON_ARCHIVE" ENABLE STORAGE IN ROW CHUNK 8192 PCTVERSION 10
NOCACHE 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)) NOCOMPRESS ,
PARTITION "P_MAXVALUE" VALUES LESS THAN (MAXVALUE)
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
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 "COMMON_DATA"
LOB ("OBJEKAT") STORE AS BASICFILE (
TABLESPACE "COMMON_ARCHIVE" ENABLE STORAGE IN ROW CHUNK 8192 PCTVERSION 10
NOCACHE 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)) NOCOMPRESS ) -
Stange error when using dbms_metadata.get_ddl in PL/SQL procedure
Basic info:
Oracle 10.2.0.4.0 on linux.
I'm trying to extract ddl of indexes that I drop and recreate frequently during monthly loads and store it in a table.
This statement works on the command line:
insert into saved_indexes
select index_name,dbms_metadata.get_ddl('INDEX',index_name,owner_name)
from sys.all_indexes
where owner = owner_name
and table_name = table_name;
commit;
The table 'saved_indexes' is a two column table with a varchar2(40) and a CLOB.
When I use the following procedure, I get 'ORA-04044 procedure, function, package, or type is not allowed here -4044' every time.
PROCEDURE SAVE_INDEXES (v_table IN VARCHAR2, v_owner IN VARCHAR2) IS
v_errorcode number(8);
v_errortext varchar2(1000);
v_start_time date;
BEGIN
insert into saved_indexes
select index_name,dbms_metadata.get_ddl('INDEX',index_name,v_owner)
from sys.all_indexes
where owner = v_owner
and table_name = v_table;
commit;
EXCEPTION
WHEN others THEN
v_errorcode := sqlcode;
v_errortext := substr(sqlerrm, 1, 1000);
dbms_output.put_line(v_errortext || ' ' || v_errorcode);
END;
Alternatively I have tried it this way:
PROCEDURE SAVE_INDEXES (v_table IN VARCHAR2, v_owner IN VARCHAR2 ) IS
v_errorcode number(8);
v_errortext varchar2(1000);
v_index_ddl CLOB;
BEGIN
for x in (select index_name
from sys.all_indexes
where owner = v_owner
and table_name = v_table)
loop
select dbms_metadata.get_ddl('INDEX',x.index_name,v_owner) into v_index_ddl from dual;
insert into saved_indexes
values(v_table,v_index_ddl);
end loop;
commit;
EXCEPTION
WHEN others THEN
v_errorcode := sqlcode;
v_errortext := substr(sqlerrm, 1, 1000);
dbms_output.put_line(v_errortext || ' ' || v_errorcode);
END;
Always with the same result. I have poured over the documentation on this and have not found anything. All objects are in the same schema, so there is not an issues with invokers rights, or privileges.
Any suggestions would be helpful...qwe11126 wrote:
When I use the following procedure, I get 'ORA-04044 procedure, function, package, or type is not allowed here -4044' every time.There is nothing wrong with SP. Post a snippet of SQL*Plus code showing how you call SP along with errors.
SY. -
Error while using DBMS_METADATA.GET_DDL package.
Hi all,
I want script of DDL of all tables in Database not in particular schema,
As I am using below query
SELECT DBMS_METADATA.GET_DDL('TABLE',u.table_name)
FROM ALL_TABLES u
WHERE u.nested='NO'
AND (u.iot_type is null or u.iot_type='IOT')
but it gives error as below.
ORA-31603: object "ICOL$" of type TABLE not found in schema "SCOTT"
It should give DDL of all tables, also I am not having DBA Privs.
Please help me.
Thanks & Regards
Rajiv.It could be helpful if you have a look into [url http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_metada.htm#i1016867]documentation.
Security Model
The object views of the Oracle metadata model implement security as follows:
Nonprivileged users can see the metadata of only their own objects.
SYS and users with SELECT_CATALOG_ROLE can see all objects.
Nonprivileged users can also retrieve public synonyms, system privileges granted to them, and object privileges granted to them or by them to others. This also includes privileges granted to PUBLIC.
If callers request objects they are not privileged to retrieve, no exception is raised; the object is simply not retrieved.
If nonprivileged users are granted some form of access to an object in someone else's schema, they will be able to retrieve the grant specification through the Metadata API, but not the object's actual metadata.
In stored procedures, functions, and definers-rights packages, roles (such as SELECT_CATALOG_ROLE) are disabled. Therefore, such a PL/SQL program can only fetch metadata for objects in its own schema. If you want to write a PL/SQL program that fetches metadata for objects in a different schema (based on the invoker's possession of SELECT_CATALOG_ROLE), you must make the program invokers-rights.
Best regards
Maxim -
Dbms_metadata.get_ddl including comments.
Hey all,
Is there a way to use the dbms_metadata package to fetch the DDL of a package/function/procedure without removing the comments? For instance if you are using SQL Developer and right click a function and then click edit. The DDL for the function will appear along with the comments. This is what I would like to see.
Any ideas?
Cheers,
Tyson Jouglet
Edited by: Tyson Jouglet on Aug 27, 2009 2:29 PMIs there a way to use the dbms_metadata package to fetch the DDL of a package/function/procedure without removing the comments? Why do you think that the comments are removed?
SQL> CREATE OR REPLACE PROCEDURE p
/* first comment*/
AS
-- some comments
BEGIN
/* some other comments*/
NULL;
END p;
Procedure created.
SQL> SELECT dbms_metadata.get_ddl (
'PROCEDURE',
'P',
USER
) ddl
FROM DUAL
DDL
CREATE OR REPLACE PROCEDURE "MICHAEL"."P"
/* first comment*/
AS
-- some comments
BEGIN
/* some other comments*/
NULL;
END p;
1 row selected.
Maybe you are looking for
-
How to read data from a GPIB when sending a function generator command
I'm using Visual C++ with the ComponentWork ActiveX and I'm tring to plot a CWGraph using the data coming from a GPIB device. When I write command such as "*IDN?" to the GPIB device, everythings if fine and I can read what the GPIB device return me.
-
Hi, I created a template with a sidebar comtaining few anchors to navigate to different pageflows. Then every page in every pageflow uses this template. It seems in each pageflow, I need repeatedly create the same actions used in the template buttons
-
Problem with full database backup.
This is what I got after execute backup full database statement "RMAN-03009: failure of backup command on ORA_DISK_1 channel at 03/03/2009 14:15:15 ORA-19502: write error on file "/u01/app/oracle/backup/ORCL/ora_df680537660_s2_s1", blockno 25985 (blo
-
Is there an outage in Bethesda, MD
How do I find out if there is an outage in my area?
-
OVMAPI_6000E Internal Error: Connection refused Connection refused - OVM
Hi, Test environment, as per this doc http://www.oracle.com/technetwork/server-storage/vm/template-1482544.html Running the above OVMs on virtual box version 4.2.10 OVM is version 3.1.1.365 I setup my environment and was ready to import VM templates.