Skip to Main Content
  • Questions
  • exporting packages,function etc. from one user to another.

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, ilker.

Asked: September 12, 2000 - 4:52 am UTC

Last updated: May 16, 2019 - 9:02 am UTC

Version: 8.1.6

Viewed 50K+ times! This question is

You Asked

Hi,

For example X user have many packages,functions,procedures etc. And I want to delete some of them after copying to another user (Y).
I mean I want to classify packages,functions etc... I can copy-paste by using Procedure Builder. But this way is not efficient because of the many packages, procedures etc.

can I do this by using more easy way...

best regards



and Tom said...

One way would be to

exp userid=old_user/pw owner=old_user rows=n

imp userid=new_user/pw fromuser=old_user touser=new_user


That would do a structural copy of the old_user to the new_user but not pull any data over. Now you can drop anything you don't want.


I also use a "getcode" routine myself. getcode looks like:

---------------- getcode.sql -------------------------------
set feedback off
set heading off
set termout off
set linesize 1000
set trimspool on
set verify off
spool &1..sql
prompt set define off
select decode( type||'-'||to_char(line,'fm99999'),
'PACKAGE BODY-1', '/'||chr(10),
null) ||
decode(line,1,'create or replace ', '' ) ||
text text
from user_source
where name = upper('&&1')
order by type, line;
prompt /
prompt set define on
spool off
set feedback on
set heading on
set termout on
set linesize 100
-----------------------------------------------------

That gets a single package/procedure/function and saves it to the current working directory. When I want ALL of the code, I use:

------------------- getallcode.sql -------------------------
set termout off
set heading off
set feedback off
set linesize 50
spool xtmpx.sql
select '@getcode ' || object_name
from user_objects
where object_type in ( 'PROCEDURE', 'FUNCTION', 'PACKAGE' )
/
spool off
spool getallcode_INSTALL
select '@' || object_name
from user_objects
where object_type in ( 'PROCEDURE', 'FUNCTION', 'PACKAGE' )
/
spool off
set heading on
set feedback on
set linesize 130
set termout on
@xtmpx.sql
----------------------------------------------

That gets ALL of the packages/etc from the current user, spools each one to a file and creates a getallcode_INSTALL script that will rerun the code in another schema for you.

Rating

  (20 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Great!!

Yogeeraj, February 11, 2002 - 12:15 am UTC

Thanks very much
saved me much time not having to re-write the same script ;)

I did only a very minor change:
spool getallcode_INSTALL
to:
spool getallcode_INSTALL.sql

Best Regards
Yogeeraj
PS. i was wondering there is a similar script that can help me do the same for tables n views. I know there are some tools offering the similar facility but... if you want to automate things they don't help much..


Tom Kyte
February 11, 2002 - 7:42 am UTC

</code> http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:683824732911 <code>

for getview, getallviews.

For tables -- I simply use EXP tables=(.....) rows=n and then IMP full=yes INDEXFILE=tables.sql

tables.sql will have all of the create tables in it.

Look into user.dba_source

Randy, February 11, 2002 - 10:32 am UTC

If your stored procedures are not wrapped than may be you are better of by selecting the source code from _dba_source and compiling the output under the new user account.




Tom Kyte
February 11, 2002 - 3:02 pm UTC

what does wrapping have to do with anything?

and why use dba_source (and how would wrapped code change what view you used) when user_source (a view to which we all have access) has everything you need???

Export Procedures only

Munz, May 23, 2002 - 5:57 pm UTC

Tom:

1. I did
exp userid=old_user/pw owner=old_user rows=n

imp userid=new_user/pw fromuser=old_user touser=new_user

and it exported data. Any ideas why?

2. I did it twice in two weeks. The 2nd time it told me that objects already exist and did not overwrite them.

How would you export procedures, functions, and packages only (code only) and overwrite the old ones.

Thank you,

Tom Kyte
May 24, 2002 - 9:16 am UTC

1) no it didn't. It exported data STRUCTURES, not the actual data.

2) imp will not overwrite objects.

In answer to "how would you ...." -- I would re-read the answer above and use getallcode.sql.



Good work

surinder, July 02, 2003 - 4:06 am UTC

Tom,
i lost all the scripts to create the table structure, procedures, triggers and now i need to install this same application on other location which also requires the database creation. so now i need to export the table structures without rows, packages and function by the method you told but how we export the triggers and will it work on other database if they have the same language.

does any other method to create the same database structure over there

Tom Kyte
July 02, 2003 - 8:13 am UTC

just exp with rows=n owner=schema_you_lost

the dmp file will have your entire schema. just import that and there you go.

If you have 9i, you can use dbms_metadata to get your schema back.

If you have my book "Expert One On One Oracle" -- in there are scripts that get it back in 8i -- part of it from the dmp file, part of it from the data dictionary.

thanks

surinder, July 03, 2003 - 3:11 am UTC

Tom
Thanks for your valuable time.

I am using Oracle 8.1.7 on Linux7.2. Actually my question was that i need to generate the whole database structure through export and import it to our another location without data in tables. I read your another solution which was given to another DBA. By that solution what i got now is that we can export the whole database with

Rows =N
Triggers = y
constraint = y

and it will export the whole structure of the database with packages, procedures,table constraints,table structures, views,Synonyms and triggers without any table data. And at our new location I will create a database only with system tablespace and import the export file. It will automatically create all the tablespaces and datafiles.

Please rectify me if i am wrong
......

But now i have doubts

1)if i give Index =y
than will those indexes will be recreated on the tables without rows.

2) If i have a different directory structure over there can i open the export file in vi editor and change the file locations.

3)Can i export the database from os Linux 7.2 with oracle 8.1.7 and import it on Linux 8 with oracle 8.1.7 or Linux 9.

Tom Kyte
July 03, 2003 - 9:08 am UTC

it'll do what you say, yes. make sure the mount points are the same.


1) yes
2) no, it is a binary file. use symbolic links if you need to trick it out.
3) exports are cross system compatible, yes.

why import does not import these kind of procedures

A reader, July 07, 2005 - 8:11 am UTC

Hi


I am trying to import a schema into a new database, this schema has packages which references remote objects using database links. The database links are created but the destination database are not up even though I expect the packages get created during import but invalid but I see these messeages in import log file

IMP-00003: ORACLE error 4052 encountered
ORA-04052: error occurred when looking up remote object SF_ADMIN.PB_LOGPRC@PRCI
ORA-00604: error occurred at recursive SQL level 1
ORA-12545: Connect failed because target host or object does not exist
IMP-00017: following statement failed with ORACLE error 4043:
"ALTER PROCEDURE "P_VABS" COMPILE TIMESTAMP '2003-08-26:12:33:01'"
IMP-00003: ORACLE error 4043 encountered
ORA-04043: object P_VABS does not exist

I dont understand this, why the procedure is NOT created? I expect to see this:

5/1 PL/SQL: SQL Statement ignored
5/22 PL/SQL: ORA-04052: error occurred when looking up remote object
LSC.X@LNX101
ORA-00604: error occurred at recursive SQL level 1
ORA-02068: following severe error from LNX101
ORA-01034: ORACLE not available
ORA-27101: shared memory realm does not exist
Linux Error: 2: No such file or directory

Tom Kyte
July 07, 2005 - 9:49 am UTC

it cannot compile the procedure, therefore it is not able to create the procedure. Not sure what to tell you otherwise.

shouldnt it create but shown as invalid?

A reader, July 07, 2005 - 10:29 am UTC

I have done this test in sql*plus

create or replace procedure lsc$dblink01
as
a number;
begin
select 1 into a from x@lnx101;
end;
/

Warning: Procedure created with compilation errors.

lnx101 is DOWN

Errors for PROCEDURE LSC$DBLINK01:

LINE/COL ERROR
-------- -----------------------------------------------------------------
5/1 PL/SQL: SQL Statement ignored
5/22 PL/SQL: ORA-04052: error occurred when looking up remote object
LSC.X@LNX101.US.ORACLE.COM
ORA-00604: error occurred at recursive SQL level 1
ORA-02068: following severe error from LNX101
ORA-01034: ORACLE not available
ORA-27101: shared memory realm does not exist
Linux Error: 2: No such file or directory

You see, it gets created but invalid, why this doesnt happen with import? Import first runs CREATE PROCEDURE .... ... as well but doesnt create the procedure!

Tom Kyte
July 07, 2005 - 1:01 pm UTC

please contact support if you believe it is happening incorrectly.

the problem is this

A reader, July 14, 2005 - 10:57 am UTC

Hi

I want to share this with you, I asked the last question regarding ORA-04052.

I am not sure if this is correct behaviour but the problem is this, if the procedure has code like this (tab_x is a public synonym pointing to a remote object)

l_var1 tab_x.code_id%type;

then this doesnt even get created

if the procedure has code like this

select 1
into blah
from tab_x
where rownum < 2

then procedure creates but gets invalidated

cheers

Tom Kyte
July 14, 2005 - 11:25 am UTC

Thanks for the followup, that explains the difference - the plsql engine cannot even really get started compiling the code. Sort of makes sense.

the problem is this (again)

A reader, July 18, 2005 - 5:23 pm UTC

Hi

I want to share this again with you.
I mentioned the problem I had with ORA-04052 and PL/SQL codes.
There is another problem not sure if it´s version related.

If export is from 8.1.7.4 and import is 9.2.0.6 then if in the import parfile RESUMABLE is used then we get ORA-04052 for some PL/SQL codes as well.

Cheers

triggers

sam, October 09, 2006 - 12:24 pm UTC

Tom:

I think that getallcode pulls everything defiend as "code" in DB except triggers and views. How can you modify it to pull triggers? or do you have to create a separate script for that.

Tom Kyte
October 09, 2006 - 1:38 pm UTC

search site for gettrigg getaview getallviews ....

actually, in the year 2006, use dbms_metadata

How to remove newline

Oracle Learner, July 21, 2009 - 12:02 pm UTC

Tom,

On Oracle 10.2.0.4 on Sunos 5.10

SQL> create or replace package test
  2  is
  3    procedure test;
  4  end;
  5  /

Package created.

SQL> create or replace package body test
  2  is
  3    procedure test
  4    is
  5    begin
  6      null;
  7    end;
  8  end;
  9  /

Package body created.


When I run the original code I get:

create or replace package test
is
  procedure test;
end;
/
create or replace package body test

is
  procedure test
  is
  begin
    null;
  end;
end;
/


i.e there is a blank line after the
create or replace package body


A minor point, I know, but if there is some procedure that logs errors in the application, the error line number against an error logged in production might not match that in a development environment.

I may be missing something obvious, but I have tried amending the code to avoid the insert of the blank line, but to no avail.

Thanks



Tom Kyte
July 24, 2009 - 12:20 pm UTC

select text
  from (
select line, type, decode(line,1,'create or replace ', '') || text text from user_source where name = upper( '&&1' )
union all
select 1 line, 'MIDDLE' type, '/' text from dual
where exists(
select line, type, decode(line,1,'create or replace ', '') || text text from user_source where name = upper( '&&1' )
and type = 'PACKAGE BODY'
)
       )
 order by decode(type,'PACKAGE',1,'PACKAGE BODY',3,'MIDDLE',2, /* default */ 1 ), line
/



test test test

I wrote that rather rapidly....

supriya, August 13, 2009 - 11:29 am UTC

Hi
Can you tell me where export data stored when exp is used?
Tom Kyte
August 13, 2009 - 12:39 pm UTC

on the client, in a file you told it to store it.

export

A reader, April 22, 2010 - 7:37 pm UTC


exp

A reader, April 22, 2010 - 7:51 pm UTC


getcode.sql -> ORA-01489: result of string concatenation is too long

Richard, March 12, 2012 - 11:59 pm UTC

Dear Tom,

I have been "playing" around with your getcode and getallcode scripts, but my spooled files are all filled with 4000 column long lines (plus chr(10) and chr(13) I theorized because I get 4002 long lines!?) making my spooled packages all about 10megs big (instead of 60kb when I do a manual copy paste).
After reinserting these huge spooled package files into a schema followed by AGAIN running the getallcode script, it dies with a ORA-01489 error message inside the spooled files (because of that being the output of the sql query).

Since I never ever post anything, (this is my 1st post ever, but I promised I would have my part finished by tomorrow morning, far from it!). Generally just google around other people's questions and answers then combining the data found online, I have spend already 6 hours on this tiny piece of script having tried everything to get rid of, what appears to be, spaces till EOL fillings!

I tried a whole bunch of techniques but nothing appears to work, if you have a simple answer it would be greatly appreciated.

Thanks in advance,
Richard

ps. I have found many others struggling with the same, never leading to an answer on any forum. Consequently, Rate 4 has been given.
Tom Kyte
March 13, 2012 - 7:39 am UTC

share your login.sql and glogin.sql files

probably some non-default setting you have

here is my login.sql (i don't have a glogin.sql)

set termout off

define e=vi
column E new_value E
column S new_value SETTINGS
select decode( substr( dbms_utility.port_string, 1, 5 ), 'IBMPC', 'vi', 'vi' ) E,
       decode( substr( dbms_utility.port_string, 1, 5 ),
               'IBMPC', 'c:\temp\xtmpx'||to_char(sysdate,'yyyymmddsssss')||'.sql',
                        '/tmp/xtmpx' || to_char(sysdate,'yyyymmddsssss') ||'.sql' ) S
  from dual;
define _editor=&E


set serveroutput on size 1000000

column object_name format a30
column segment_name format a30
column file_name format a40
column name format a30
column file_name format a30
column what format a30 word_wrapped

set trimspool on
set long 5000
set linesize 79
set pagesize 9999

define gname=idle
column global_name new_value gname
select lower(user) || '%' || substr( global_name, 1, decode( dot, 0, length(global_name), dot-1) ) global_name
  from (select global_name, instr(global_name,'.') dot from global_name );
set sqlprompt '&gname> '

column P_TO format a20 word_wrapped
column P_FROM format a20 word_wrapped
column P_SUBJ format a20 word_wrapped
column P_CC format a20 word_wrapped
column P_BCC format a20 word_wrapped
column P_BODY format a40 word_wrapped

column plan_plus_exp format a100
set tab off
set termout on

new findings

Richard, March 13, 2012 - 10:51 am UTC

Thank you very much for your follow up.

I am locally running an XE edition of Oracle. I could only find a GLOGIN.SQL which is EMPTY :) (besides the comments from Oracle -- NAME/DESCRIPTION/USAGE.
Tried it with running your login.sql, no restuls..

Instead, I started from scratch:
dropped all packages, left one object type in the user_source table (which had nothing to do with the getallcode script to begin with, so which I believe to be unaffected by anything previously done).

simple test-----

spool c:/temp/empdump.sql --(yes, it's a windoze)
select * from emp;
spool off

results are good! Just what you expect: the output with decent lines.

now I do this:

spool c:/temp/typedump.sql
select * from user_source where type = 'TYPE';
spool off

and voila: (actually, I shouldn't paste this here... we don't want 5 pages of blanks per rowline.
The column field "TEXT" appears to be utterly damaged, or does the database always fill this field with 4000 characters? (I tried the same adding trimspool on, and others)


NAME TYPE LINE TEXT
------------------------------ ------------ ---- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
(I took out the rest as this specific line continues 68x and has 60 characters in width giving a grand total of 4080 character '-' in this header line. (80 times '-' more due to NAME, TYPE, LINE columns I suppose)

Am I leaking somewhat over into another topic here?

Any help is greatly appreciated. I was astonished this morning to see your answer, 10 years of answering questions and still such a fast reply! Thanks!
Tom Kyte
March 13, 2012 - 4:46 pm UTC

text is a varchar2(4000), it will print out 4000 dashes for the column heading


issue:

SQL> column text format a80 word_wrapped



but this shouldn't affect my getcode script, what does getcode have to do with this problem?

conclusion?

Richard, March 13, 2012 - 7:20 pm UTC

(Oops, so many tabs open, this review ended up also in the "P11_QUESTION_ID:1317802337341", not my intention at all!! Please delete)

Sorry for the confusing interpretation of the previous example. It was about having 4000 blanks 
whereas normally there would be a newline or C/R immediately after the last character. (I thought 
the heading would be trimmed according to the longest line, of course it doesn't)

One thing is sure, it has nothing to do with your script getcode or getallcode, I see that now 
after running this:

CREATE TABLE TABLE1 
(
  COLUMN1 VARCHAR2(4000) 
);

INSERT INTO TABLE1 VALUES ('Hello World! Once more we meet again');

running script "spool.sql":

column text format a80 word_wrapped   /* next to placing it here I also ran this command in SQL> 
and on a SQL Worksheet in SQL Developer */
set termout off
set heading off
set trimspool on
set feedback off
set linesize 50
spool c:/temp/table.sql
SELECT * FROM TABLE1;
spool off

output file: table1.sql:
Hello World! Once more we meet again                                                                
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
                                                                                                    
 
So this is where the blanks end again. So yes, max varchar2 is 4000, I realize, but no trimming, no 
column text format (or I run it wrongly, I tested it everywhere, not even with ltrim, trim within 
the SQL query, only "to_clob(column1)" gave me somewhat satisfying result (shortened lines), but 
still...  I sent the same script to my brother in order to check it on his server, to no avail. 
Same results, no, actually he had 1000 characters per line ?? (so he would have set line length 
1000 in his login or so? Would have to ask). Am I the only one getting these long lines without 
being able to trim or limit them? Can't be.

I'm sorry that this leaked over into another topic as I understand now it has nothing to do with 
the origianl script you provided. However, since the script is so useful, more people must have had 
this issue as I am having, so this is an add-on. Therefore, I rate the review Useful - 3 

I recreated the script to work with the UTL FILE package. If I need the files locally, I'll copy 
them from the server.

I don't know what the case is that I can't trim nor limit these lines (maybe hard-code the column 
text format parameter in the glogin.sql). But the script using UTL FILE is doing it's job well, 
thanks to your script I was able to write it. Thank you for your time, very appreciated, even 
inspiring.
My brother wondered whether the same person is still answering these questions after so many years, 
he doubted it. (Tom isn't Tom anymore?) One thing he was sure of: Thanks to this forum "asktom", 
Oracle became a lot more attractive to many people!

Tom Kyte
March 13, 2012 - 7:44 pm UTC

goto the other place, I already responded over there...

exp

A reader, May 22, 2012 - 6:38 pm UTC


Very useful scripts, I have enhanced them here

Krishnamoorthy Sreenivasarao, August 22, 2013 - 1:03 am UTC

I found those scripts to be very useful for extracting packages, procedures, functions etc. Thanks for the same. The following enhanced scripts will all a DBA to extract all those objects for all users from one database to another. This is especially useful after an exp from an older version of the database and imp to a newer version.

1. getallcode.sql
set termout off
set heading off
set feedback off
set linesize 50
spool xtmpx.sql
select '@getcode ' || owner || ' ' || object_name
from all_objects
where object_type in ( 'PROCEDURE', 'FUNCTION', 'PACKAGE' )
and owner not in ( 'SYS', 'SYSTEM', 'OUTLN', 'ORACLE_OCM', 'DBSNMP' )
/
spool off
spool getallcode_INSTALL.sql
select '@' || object_name
from all_objects
where object_type in ( 'PROCEDURE', 'FUNCTION', 'PACKAGE' )
and owner not in ( 'SYS', 'SYSTEM', 'OUTLN', 'ORACLE_OCM', 'DBSNMP' )
/
spool off
set heading on
set feedback on
set linesize 130
set termout on
@xtmpx.sql

--

2. getcode.sql
set feedback off
set heading off
set termout off
set linesize 1000
set trimspool on
set verify off
spool &2..sql
prompt set define off
select decode( type||'-'||to_char(line,'fm99999'),
'PACKAGE BODY-1', '/'||chr(10),
null) ||
case when line=1 then 'CREATE OR REPLACE ' || replace( upper(text), upper( name), owner || '.' || upper(name) )
else text
end
text
from all_source
where name = upper('&&2')
and owner = upper('&&1')
order by type, line;
prompt /
prompt set define on
spool off
set feedback on
set heading on
set termout on
set linesize 100



Fantastic

Linda A Stewart, May 15, 2019 - 7:40 pm UTC

Saved us a lot of time when we had to copy a database to a fresh schema and get all of the objects! Long story of why a simple expdp and impdp would not work.

the getallcode is great!

Thanks retired Tom!
Chris Saxon
May 16, 2019 - 9:02 am UTC

Thanks indeed :)

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library