How to export Oracle schema and user detail in sqldeveloper - oracle

in sqldeveloper there is a useful tool for exporting on a sql file all the object that exist on a schema.
Anyhow i am unable to find something similar for exporting the schema and user informations.
The sqldeveloper version i'm using is 3.2
Thanks, Piwakkio.

DECLARE
OBJECTDDL CLOB;
BEGIN
FOR I IN (select * from user_objects WHERE OBJECT_TYPE IN(/*WHATEVER YOU WANT*/) ) LOOP
select dbms_metadata.get_ddl(I.OBJECT_TYPE,I.OBJECT_NAME) INTO OBJECTDDL from dual;
DBMS_OUTPUT.PUT_LINE(OBJECTDDL);
END LOOP;
END;
Good Luck :)

Related

Generate Oracle DDL without using dbms_metadata

I need to automate DDL generation on Oracle where I have read only access. When attempting to do this via dbms_metadata e.g. dbms_metadata.get_ddl('TABLE', 'SOME_TABLE') I get:
ORA-00604: error occurred at recursive SQL level 1
ORA-16000: database or pluggable database open for read-only access
Which is surprising as I am not asking to write to the database.
It is not possible for me to get more access to fix this.
However, I can successfully use Intellij or DataGrip to generate DDL https://www.jetbrains.com/datagrip/features/generation.html and the output is sufficient.
Any idea how the IDEs are doing this? Or what (if any) open source package Intellij are using?
I don't have a pluggable database but I used the following on livesql and my database and it works well.
The error indicates a read only database do you have a read/write db to test on?
Note if you are trying my example change the value of owner to meet your requirements
CREATE OR REPLACE PROCEDURE get_metadata(
p_object_type VARCHAR2,
p_object_name VARCHAR2,
p_owner VARCHAR2) authid current_user is
x clob;
begin
x :=
dbms_metadata.get_ddl(
p_object_type,
p_object_name,
p_owner);
dbms_output.put_line(x);
end;
/
BEGIN
FOR cur_r IN(
SELECT OBJECT_TYPE,
OBJECT_NAME,
OWNER
FROM ALL_OBJECTS
WHERE OWNER like 'SQL%'
AND OBJECT_TYPE IN ('TABLE', 'INDEX', 'FUNCTION', 'PROCEDURE', 'TYPE','PACKAGE', 'SEQUENCE') order by OBJECT_NAME
)
LOOP
get_metadata
(cur_r.OBJECT_TYPE,
cur_r.OBJECT_NAME,
cur_r.OWNER);
END LOOP;
END;
They might be selecting from all_source:
SELECT text
FROM all_source
WHERE owner = 'CEAADMIN'
AND name = 'APPLICATION_LIST'
AND TYPE IN ('PACKAGE', 'PACKAGE BODY')
ORDER BY TYPE, line;
View source can be retrieved from all_views. The difference is that the view is kept in a WIDEMEMO type.
SELECT *
FROM all_views
WHERE owner = 'CEAADMIN' AND view_name = 'MYLINKS_VW';

PL/SQL script for export database

I want to export all the data from a database like functions, procedures, views, triggers for which an user has owner privilegies. I know that SQL Developer has the option for exporting databases in sql file, but I want to do this from the code. When I run the code I want to create a file with .sql extension which must contain all data from the database. First of all, I want to know if that is possible, and if it is, can anyone tell me some hints for doing this?
I started with making a file:
CREATE DIRECTORY test_dir AS 'H:\';
DECLARE
out_File UTL_FILE.FILE_TYPE;
BEGIN
out_File := UTL_FILE.FOPEN('test_dir', 'test.sql' , 'W');
UTL_FILE.PUT_LINE(out_file , 'here will be the database export');
UTL_FILE.FCLOSE(out_file);
END;
The Oracle export/import utilities will export/import database objects in user mode (use OWNER parameter) using a binary file format
http://docs.oracle.com/cd/B28359_01/server.111/b28319/exp_imp.htm
However, if you want to export the metadata for your objects and data into sql files, you can query Oracle's catalog users views like user_objects -> for a user's database objects user_tables -> tables, user_constraints -> constraints (even user_source to get text for compiled objects). If you intend your .sql files to re-create objects and data, you are in for some heavy lifting, especially if you want your scripts to be portable. In that case I would look 3rd party tools.
I guess this should be the answer for what you are looking for. Hope it helps. This is just an example for what you are asking. Other types can also be incorporated as required.
spool on;
spool <target path>/filename;
SET SERVEROUTPUT ON SIZE UNLIMITED;
BEGIN
FOR I IN
(SELECT al.*
FROM ALL_SOURCE al
WHERE OWNER = 'AVROY'
ORDER BY name,
line
)
LOOP
dbms_output.put_line(I.TEXT);
END LOOP;
FOR I IN
(SELECT * FROM ALL_TABLES WHERE OWNER = 'AVROY'
)
LOOP
BEGIN
dbms_output.put_line(dbms_metadata.get_ddl('TABLE',i.table_name,'AVROY'));
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
END LOOP;
END;
SPOOL OFF;
You might want to use the export and import utility of Oracle database.
http://www.oracle-dba-online.com/export_and_import.htm

Show the results of a SYS_REFCURSOR from Oracle stored procedure, using Toad (9.0.1.8) with fully qualified schema name

Wow. I'm a Sql Server guy, trying to learn Oracle.
No horse in the race, just using the db that my job requires.
How do I see the results of a simple OUT SYS_REFCURSOR.
(Using TOAD 9.0.1.8)
(Oracle Environment = 10g)
Code below. Please know that using fully qualified names (schema + object_name) is the "norm" around here. (And please know I only kinda know what I'm talking about when it comes to Oracle)
CREATE OR REPLACE
PROCEDURE SYS.PROC_GET_MY_COOL_TABLES (p_recordset OUT SYS_REFCURSOR) AS
BEGIN
OPEN p_recordset FOR
SELECT
TABLE_NAME
, TABLE_TYPE
from
SYS.USER_CATALOG
where
TABLE_TYPE = 'TABLE' and TABLE_NAME NOT LIKE '%$%'
;
END PROC_GET_MY_COOL_TABLES;
/
variable myVarForARefCur refcursor;
exec SYS.PROC_GET_MY_COOL_TABLES( :myVarForARefCur );
print myVarForARefCur;
/

Oracle database link. Check for existence or overwrite?

I need to check if a database link already exists before I create one. How can I do that?
I am writing an SQL script that starts with this:
DROP DATABASE LINK mydblink
then I create one:
CREATE DATABASE LINK mydblink
CONNECT TO testuser
IDENTIFIED BY mypswd
USING 'mypersonaldb'
I will of course get an error in the first step if the database link doesn't exists. And if I omit the first step and just go ahead and create a db link, I will again get an error saying that it already exists with the same name.
What can I do in order to check if the the database link already exists?
SELECT COUNT(1)
FROM dba_objects -- user_objects
WHERE object_type = 'DATABASE LINK'
AND object_name = 'ARGUS51P';
For example (untested):
declare
l_link_cnt pls_integer := 0;
l_sql varchar2(32767);
begin
-- link creation sql (fill in details of how you want this created)
l_sql := 'create public database link ...';
select count(1)
into l_link_cnt
from dba_objects
where object_type = 'DATABASE LINK'
and object_name = 'SOME_LINK';
-- create link if it doesn't exist yet
if (l_link_cnt = 0) then
-- create link
execute immediate l_sql;
end if;
end;
Oracle has no way to test for existence before a DROP or CREATE. (Well, ok, you could write some PL/SQL, but, that's probably more trouble than it's worth.) In Oracle scripting, it's pretty standard to simply do both the DROP and the CREATE in a script. If the DROP errors out, so be it. It won't affect execution of the script.
-Mark

How to check if a stored procedure exist?

I have searched the net and I've found a post that uses the following snippet to check if a stored procedure exists:
select *
from USER_SOURCE
where type='PROCEDURE'
and name='my_stored_procedure.'
Is there any other way to check if a procedure exists?
Edited to add:
Before posting SQL Server ways, please I'm looking for ORACLE ways.
Alternatives:
USER_PROCEDURES:
SELECT *
FROM USER_PROCEDURES
WHERE object_name = 'MY_STORED_PROCEDURE'
USER_OBJECTS:
SELECT *
FROM USER_OBJECTS
WHERE object_type = 'PROCEDURE'
AND object_name = 'MY_STORED_PROCEDURE'
Something that worked for me!
SELECT text
FROM all_source
WHERE name = 'MY_SP_NAME'
ORDER BY line;
Alternatively you can try calling SP like this:
CALL MY_SP_NAME();
You might end up error like this, but that confirms you have SP defined there:
OCI Statement Execution failure.ORA-06553: PLS-306: wrong number or types of arguments in call to 'MY_SP_NAME'
The only way to see if a procedure exists in the database is though querying DBA_OBJECTS. The disadvantage here is that only a dba has access to this view. Second best is using all_objects. ALL_OBJECTS shows you the objects for which you have somehow a privilege. USER_OBJECTS only shows you your own objects.
Execute the query below in SQL*PLUS, ODBC Test,...
SELECT text FROM all_source WHERE name='MY_PROCEDURE' ORDER BY line
where MY_PROCEDURE is the stored procedure name.
Below is sample output:
Get Data All:
"TEXT"
"PROCEDURE Usp_Get_Blob
"(
"P_DOC_ID INT,
"P_DOC_TEXT OUT BLOB)
"as
"begin
" select B1
into p_doc_text
" from blobtest
" where ID = p_doc_id;
"end; "
I was not able to find stored procedure with any of the methods above.
Reason: stored procedure was inside package. Oracle uses packages to gather several stored procedures in one module.
This will display the stored procedure and its contents stored in the table.
select
name c1,
text c2
from
dba_source
where
name = upper('procedure_name')
order by
line asc;
select *
from USER_SOURCE
where type='PROCEDURE'
and name='my_stored_procedure.'

Resources