Oracle - How to generate script from sql developer - oracle

How to take script for schema of the tables, stored procedures of Oracle through SQL Developer tool (SQLPLUS command line interface)?

SQL Developer -> Tools -> Database Export...

If you want to see DDL for the objects, you can use
select dbms_metadata.get_ddl('OBJECT_TYPE','OBJECT_NAME','OBJECT_OWNER')
from dual
/
For example this will give you the DDL script for emp table.
select dbms_metadata.get_ddl('TABLE','EMP','HR')
from dual
/
You may need to set the long type format to big number. For packages, you need to access dba_source, user_source, all_source tables. You can query for object name and type to see what code is stored.

This worked for me:
In SQL Developer, right click the object that you want to generate a script for. i.e. the table name
Select Quick DLL > Save To File
This will then write the create statement to an external sql file.
Note, you can also highlight multiple objects at the same time, so you could generate one script that contains create statements for all tables within the database.

In Oracle the location that contains information about all database objects including tables and stored procedures is called the Data Dictionary. It is a collection of views that provides you with access to the metadata that defines the database. You can query the Data Dictionary views for a list of desired database objects and then use the functions available in dbms_metadata package to get the DDL for each object. Alternative is to investigate the support in dbms_metadata to export DDLs for a collection of objects.
For a few pointers, for example to get a list of tables you can use the following Data Dictionary views
user_tables contains all tables owned by the user
all_tables contains all tables that are accessible by the user
and so on...

use the dbms_metadata package, as described here

This worked for me:
PL SQL Developer -> Tools -> Export User Objects
Select checkboxes: Include privilege and Include storage
Select your file name. Hit export.
You can later use generated export file to create table in another schema.

step 1. select * from <tablename>;
step 2. just right click on your output(t.e data) then go to last option export it will give u some extension then click on your required extension then apply u will get new file including data.

The basic answer appears to be 'use the dbms_metadata package'. The axuilliary question is:
But what if I want to generate a script for all the tables at a time?
And the answer, presumably, is to interrogate the system catalog for the names and owners of all the tables:
SELECT dbms_metadata.get_ddl('TABLE', s.tabname, s.tabowner)
FROM system_catalog_describing_tables AS s
WHERE ...any conditions that are needed...
I'm not sufficiently familiar with Oracle to know the system catalog. In Informix, which I do know, assuming that there was a procedure dbms_metadata.get_ddl, the query would be:
SELECT dbms_metadata.get_ddl('TABLE', s.tabname, s.owner)
FROM "informix".systables AS s
WHERE tabid >= 100 AND tabtype = 'T';
In Informix, tabids less than 100 are reserved for the system catalog, and non-tables (views, synonyms, sequences and a few other esoteric things) are excluded by requiring the right 'tabtype'.

I did not know about DMBS_METADATA, but your answers prompted me to create a utility to script all objects owned by an Oracle user.

Oracle SQL Developer > View > DBA > Select your connection > Expand > Security > Users > Right click your user > Create like > Fill in fields > Copy SQL script > Close
If your user has object privileges, do this also
Oracle SQL Developer > View > DBA > Select your connection > Expand > Security > Users > Double click your user > Object Privs > Select all data > Right click > Export > Export as text file
Edit that text file to grant object privileges to your user.

Related

Viewing the last edit date of a procedure in Oracle SQL Developer

In SQL Server Management Studio, there's a tab called Object Explorer Details that you can look at to see when a table or a procedure is created or last modified.
Is there something similar for Oracle SQL Developer?
View > Reports > Data Dictionary Reports
Open the All Objects Report
You can filter/sort the report to restrict to a specific schema or object type, or even filter on the last updated date (Last_DDL)
* Query way *
With Oracle, you can query the view all_objects
* GUI way *
Search for the objects that you want and the check the Details tab
Let say you want information about OE_ACKNOWLEDGMENT_PUB package body
Then you click on the object name
Then you click on Details tab

how to find the query used for creation of Temporary table in Oracle sql developer

I have created a temporary table in oracle sql developer but I forgot to save it and now I want to reuse the query but I don't remember the code used then. Is there a process to get query used creation of temp table?
You can use dbms_metadata.get_ddl()
select dbms_metadata.get_ddl('TABLE', 'YOUR_TABLE_NAME_HERE')
from dual;
The result is a CLOB with the complete DDL. You might need to adjust the display in SQL Developer to make the content of that value fully visible (I don't use SQL Developer, so I don't know if that is necessary and if so, what you would need to do)
Edit:
It seems SQL Developer can't display the result of this query properly unless you use the "Run Script" option. And with that you need to use a SET LONG 60000 (or some other big number) before you run it, to see the complete source code:

How to copy data from one database/table to another database/table in oracle using toad

I am trying to copy a table data from dev box db to uat db which are 2 different data bases . I am trying in toad.All the connection details are correct but its not working and throwing the following error.
[Error] Execution (12: 1): ORA-00900: invalid SQL statement
This is what i am trying
copy from abc/cde#//abc.abc.com:1521/devbox to abc/cde#//abc.abc.com/uatbox
INSERT TOOL_SERVICE_MAPPING (*)
USING (SELECT * FROM TOOL_SERVICE_MAPPING)
If your table doesn't have a huge number of rows you can use Toad's Export function: it creates an insert statement for each row. You can then run these statements in destination DB to re-create your table's data.
Here are the steps:
A. Create a copy of the table in destination DB
in source DB in a schema browser window click on the table you want to copy, select "script" tab in the right part of the window: you will find the script to re-create your table; copy this script
paste the script in a new SQL editor window in destination DB and run it. This should create the new table
B. Copy data in new table
in a schema browser window right click on table name in source DB
select "Export Data" from context menu
write "where" statement of your export query (leave it blank if you want to copy the entire table)
select destination: clipboard
click "ok" (now insert statements are stored in your clipboard)
paste insert statements in a new SQL editor window in destination DB
run statements as script (shortcut F5)
copy is a SQL*Plus command, not a SQL statement. I would be surprised if Toad had implemented that particular SQL*Plus command (it does implement many of the simpler commands). If you want to use the copy command, you would need to use SQL*Plus, not Toad.
If you want to use Toad, you would need to use a SQL statement to copy the data. You could create a database link in the destination database that points to the source database and then
INSERT INTO tool_service_mapping
SELECT *
FROM tool_service_mapping#<<db link to source database>>
The easyest and most error-free way I have experienced so far is: Database->Compare->Schemas
It's not too complicated as it looks (lots of checkboxes), but you tick boxes for objects you need to be created in an empty database, and at the end of comparison you end up with SQL script including all objects (triggers, views, sequences, packges) that you selected (checkboxes).
I clearly see all tables, triggers, data, etc in generated sql script and even can tick these I don't wish to create (if any)... Before executing script, TOAD asks you to confirm against which database you are running the script - saved me few times... As ackward as it looks, it works perfectly.
I have arround 200 tables I don't know if this is suitable for huge databases.

Oracle query output in excel

I have an Oracle 10G database and I need to write a fairly straightforward query that joins two tables and selects some data. However, I'd like to export the result list to an excel, so end users can use this .xls document to see the results and filter by one of the fields (location)
When I write the query, is there an easy way I can generate/ create an excel document that would hold these results as described above? The SQL doesn't need to run from within excel, but I guess that would be a useful feature now that I think about it!
Thanks.
There is simple solution for your request.
By using ora_excel, small pl/sql package which generates Excel xlsx file, you can select data and export selected data to Excel and set filtering.
Please see following example:
BEGIN
ORA_EXCEL.new_document;
ORA_EXCEL.add_sheet('My sheet');
ORA_EXCEL.query_to_sheet('select * from employees'); -- Select data from database
ORA_EXCEL.set_cells_filter('A1', 'K1'); -- Add cell filtering from column A1 to column K1
-- Save generated Excel to file with name example.xlsx to Oracle folder EXAMPLE_XLSX
ORA_EXCEL.save_to_file('EXPORT_DIR', 'example.xlsx');
END;
For more details please check here
Cheers
Pretty easy to do in excel; and when done user can right click the data and say "Refresh" to get the latest updates.
but why reinvent the wheel lots of online articles already explain how to do this... Here's one
http://blog.mclaughlinsoftware.com/microsoft-excel/how-to-query-oracle-from-excel-2007/
After you've connected to a table, you can edit the properties on the connection and enter custom SQL (copy and paste from your developer tools)
Since you cannot use OLE DB in your version of Excel. Use SPOOL to create a CSV file.
SQL> SET echo off
SQL> SET verify off
SQL> SET colsep ,
SQL> SET pagesize 0
SQL> SET trimspool on
SQL> SET feedback off
SQL> SPOOL ON
SQL> SPOOL C:\data.csv
SQL> SELECT COLUMN1,COLUMN2,COLUMN3....
FROM TABLE;
SQL> SPOOL OFF
The .csv file should open in Excel by default. Use proper column aliases so that users understand the column headers.
Quick way:
At first create a view which contains your Query(Best way because you might need to change this query later).
Be sure to properly have installed oracle client.
In Excel(2007 and above) in Data tab go this way:
From Other sources -> From Data Connection Wizard -> Microsoft Data Access - OLE DB Provider for Oracle
Now Enter your DataSource Name(Stored in tnsnames.ora) and user password
Find you view and Then You'll have what you need.
You can save password and set option to refresh automatically in connection properties.
You are able to query an oracle database directly from Excel 2003 however, your sql statements are interpreted by MS Query and because of this it can often be frustrating. I will assume the machine in question already has the ability to query your database and has properly configured the database naming.
To query your database from excel 2003 you must:
Install and configure oracle's ODBC Driver (You must have the 32bit drivers installed since excel03 is a 32bit application). ODBC can be configured under start > administrative tools > ODBC Data Source Administrator
Open excel 2003 and goto data > import external data > new database query.
This should bring up MS Query which is an Access-like interface.
Obviously this is a very brief starter to get you stepping in the right direction. If you have any specific questions, please comment and I will try and help you.
Step 1
Run Your Query
Right Click on Resultenter image description here
Step 2
Click on Export
enter image description here
Step 3
Select Format To Excel
Enter datasheet name and location
Step 4
Click on Next and then finish
enter image description here
You can do one thing.
First generate the output in a form that includes column separators using symbols (like , or #).
Import the data to the excel and then define the placeholders as the column separators.

Any tools to export the whole Oracle DB as SQL scripts

Here is my problem, I wants to create a baseline on our development Dateabase (Oracle 10g), and check into our svn for version control, and after this we will use liquibase to help us manage the incremental database changes.
My problem is how should I create baseline of Oracle 10g? the database now consists of 500+ tables, with large amount of configuration data, and I wants my db baseline to base on a set SQL scripts to check into subversion, rather then check in Oracle dump..
I have try use liquibase generateChangeLog, but it have some performance problem.. can anyone can recommends me any tools that will help me
1. Scan any Oracle Schema
2. Generate a set of SQL Scripts (With Table structures, and Data)..
Thanks in advance
James!
Something like
SELECT DBMS_METADATA.GET_DDL('TABLE',table_name) FROM USER_TABLES;
is a good start. You can tweak it with PL/SQL and UTL_FILE to get it to write each table to a different file. You will probably need to do sequences too (though versioning them is fairly pointless), and maybe triggers/procedures/functions/packages etc.
Don't forget grants.
Have you tried Oracle's free SQLDeveloper tool? It gives you the possibility of exporting DDL and data.
EXPDP with CONTENT=METADATA_ONLY option, then IMPDP with SQLFILE=your_script.sql ?
Nicolas.
More general solution would be to dump DDL sql for selected list of tables, but additionally also other types of objects. This could be done by using all_objects and all_users views.
Example that worked for me:
select dbms_metadata.GET_DDL(u.object_type,u.object_name, u.owner)
from all_objects u
where 1=1
-- filter only selected object types
and u.object_type in ('TABLE', 'INDEX', 'FUNCTION', 'PROCEDURE', 'VIEW',
'TYPE', 'TRIGGER', 'SEQUENCE')
-- don't want system objects, generated, temp, invalid etc.
and u.object_name not like 'SYS_%'
and temporary!='Y'
and generated!='Y'
and status!='INVALID'
and u.object_name not like 'TMP_%'
and u.object_name not like '%$%'
-- if you want to filter only changed from some date/timestamp:
-- and u.last_ddl_time > '2014-04-02'
-- filter by owner
and owner in (
select username from dba_USERS where DEFAULT_TABLESPACE not like 'SYS%'
and username not in ('ORACLE_OCM')
and username not like '%$%'
)
;
I wrote a python script that refreshes db schema in incremental mode based on similar sql:
runs sql with last_ddl_time>=max(last_ddl_time from last refresh)
at the end stores last_ddl_time somewhere in filesystem for next refresh
References:
oracle dbms_metadata.GET_DDL function
oracle all_objects view

Resources