I need to create A procedure to extract all data of any Table or view in the parameter of the program
using pl/sql
And thanks in advance.
you could select the specified tables and views as a cursor from the systemables „all_tables“ and „all_views“ (https://docs.oracle.com/cd/B19306_01/server.102/b14237/statviews_2117.htm#REFRN20305) - loop through them and write the data using utl_file to disk. Easiest way to get into excel would be an separated ascii-file or CSV (http://nimishgarg.blogspot.com/2011/09/create-csv-file-using-plsql.html). For xml or xls you will have to use an package - depending on your database version.
If its all for an one-time-export use the sql-developer.
Related
I am beginner on PL/SQL, and I have to use it for data migration.
We are setting up a new Human Resources Management system. So, we want to:
Extract all data of the old system
Edit it with PL/SQL
Export it in XML files (another team gonna load the XML files to the new system after that)
We have to edit column names, column types, employees IDs, etc.. in a way that all data can be suitable for the new system.
Can anyone explain me steps to do this, show me links or similar examples.
This answer is keeping in mind that you are using a oracle data base to extract data and create xml files
For extracting data from database tables to xml you have several options
Using Sql
Example:-
SELECT XMLElement( "DEPARTMENT"
, department_name
)
FROM departments
WHERE department_id IN (10, 20);
output
<DEPARTMENT>Administration</DEPARTMENT> <DEPARTMENT>Marketing</DEPARTMENT>
2.Using PLSQL
using plsql XMLTYPE constructor
using DBMS_XMLGEN package
using dbms_xmldom package
With the XMLTYPE constructor and DBMS_XMLGEN package, you can create simple XML documents, fast and easy. When you need to create more advanced XML documents or want to have more control on how your XML document looks like, DBMS_XMLDOM can be used. The DBMS_XMLDOM package is a bit more complicated as you’ll have to create the entire document by calling functions and procedures of the package.
Refer following 2 articles
Generating XML from SQL and PL/SQL – Part 1
Generating XML from SQL and PL/SQL – Part 2
To Wite the xml in to a file you can use
Oracle Spool if you are using sql to generate
xml UTL_FILE package if you are using plsql to generate xml
With TOAD I know I can view the dependency (uses) graph of a stored procedure using the schema browser. And, the Oracle utility procedure deptree_fill can do something similar. What I want to do is script out all of the stored procedures, functions and table definition DLLs into a file that I can use to recreate those objects in another database. Is there a tool or an existing script for this purpose? My own searching has not found a solution. In my particular case the stored procedure uses a dozen other procedures, a few functions and twenty tables.
Edit 1
Maybe my original question was not clear. What I am looking for is something that will take the stored procedure I am interested in and script it and all of its dependency graph into one or more files.
The schema I am dealing with has hundreds of objects in it and the dependency graph has ~50 objects in it. So I'd rather not dig through large lists in TOAD or write an Oracle script myself if I can avoid it.
All sources can be extracted using the dbms_metadata package.
To get the source of a table:
select dbms_metadata.get_ddl('TABLE', 'SOME_TABLE')
from dual;
To get the source of a stored procedure:
select dbms_metadata.get_ddl('PROCEDURE', 'SOME_PROC')
from dual;
Using that you can create a SQL script that extracts everything and then spool the result to a file.
More details about the various functions in dbms_metadata can be found in the manual:
http://docs.oracle.com/cd/E11882_01/appdev.112/e25788/d_metada.htm#i1015856
Hmm, it is quite easy to find in google.
Get table DDL: How to get Oracle create table statement in SQL*Plus
Code of stored procedures can be found in table USER_SOURCE.
Also, for exporting schema to another DB you can use oracle utilities: http://docs.oracle.com/cd/B28359_01/server.111/b28319/exp_imp.htm#g1070082
In Toad see the Generate Schema Script window. You can get to it from the Database|Export menu. There are many options there to include/exclude what you want.
I want to update the database in oracle by taking input from file.Means I have some input fields in file and update the table by taking that input.
Can I do this by creating directory and using utl_file which is provided in Oracle.
Yes, you need to create a DIRECTORY object and then use UTL_FILE or something similar to open and read the file, then INSERT into your table. I can't be more specific since you didn't really tell us anything about what you're trying to do.
Perhaps the SQL*Loader or import utilities would work for you too:
SQL*Loader: http://docs.oracle.com/cd/B28359_01/server.111/b28319/ldr_concepts.htm#g1013706
import: http://docs.oracle.com/cd/B28359_01/server.111/b28319/dp_import.htm#g1025464
Based in the csv file column header it should create table dynamically and also insert records of that csv file into the newly create table.
Ex:
1) If i upload a file TEST.csv with 3 columns, it should create a table dynamically with three
2) Again if i upload a new file called TEST2.csv with 5 columns, it should create a table dynamically with five columns.
Every time it should create a table based on the uploaded csv file header..
how to achieve this in oracle APEX..
Thanks in Advance..
Without creating new tables you can treat the CSVs as tables using a TABLE function you can SELECT from. If you download the packages from the Alexandria Project you will find a function that will do just that inside CSV_UTIL_PKG (clob_to_csv is this function but you will find other goodies in here).
You would just upload the CSV and store in a CLOB column and then you can build reports on it using the CSV_UTIL_PKG code.
If you must create a new table for the upload you could still use this parser. Upload the file and then select just the first row (e.g. SELECT * FROM csv_util_pkg.clob_to_csv(your_clob) WHERE ROWNUM = 1). You could insert this row into an Apex Collection using APEX_COLLECTION.CREATE_COLLECTION_FROM_QUERY to make it easy to then iterate over each column.
You would need to determine the datatype for each column but could just use VARCHAR2 for everything.
But if you are just using generic columns you could just as easily just store one addition column as a name of this collection of records and store all of the uploads in the same table. Just build another table to store the column names.
Simply store this file as BLOB if structure is "dynamic".
You can use XML data type for this use case too but it won't be very different from BLOB column.
There is a SecureFile feature since 11g, It is a new BLOB implementation, it performs better than regular BLOB and it is good for unstructured or semi structured data.
I have never worked with Oracle. This is the first time and the job is quite tricky. I have a text file with records delimited with tab. These records are to be imported into a database using pl-sql. I have searched online but the solutions suggests using SQL Loader utility. But the requirement is to do that using sql statements. No command line utility. Preferable the SP or UDF will take file path and database name as input parameters and it will import the records when executed. Is this possible? Can someone provide me sample sql statements or any link that explain this process step by step? Also note that there can be blank records in file. Thanks in advance.
External Tables seems like the best approach.
http://docs.oracle.com/cd/B19306_01/server.102/b14215/et_concepts.htm
UTL_FILE is possible but you would have to write the code to parse the tab delimited text etc.
http://www.allroundautomations.nl/download/NewFeatures7.pdf
check on that file, easy to upload a csv file to a table