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
Related
I'm currently trying to create txt files from all tables in the dbo schema
I have like 200s-300s tables there, so it would takes up too much times to create it manually..
I was thinking for creating a loop.
so as example (using AdventureWorks2019) :
select t.name as table_name
from sys.tables t
where schema_name(t.schema_id) = 'Person'
order by table_name;
This would get all the table name within the Person schema.
So I would loop :
Table input : select * from ${table_name}
But then i realized that for txt files, i need to declare all the field and their data types in pentaho, so it would become a problems.
Any ideas how to do this "backup" txt files?
Using Metadata Injection and more queries to the schema catalog tables in SQL Server. You not only need to retrieve the table name, you would need to afterwards retrieve the columns in that table and the data types, and inject that information (metadata) to the text output step.
You have in the samples directory of your spoon installation an example on how to use Metadata Injection, use it, along with the documentation, to build a simple example (the check to generate a transformation with the metadata you have injected is of great use to debug)
I have something similar to copy data from one database to another, both in Oracle, but with SQL Server you have similar catalog tables as in Oracle to retrieve the information you need. I created a simple, almost empty transformation to read one table and write to another. This transformation has almost no information, only the database origin in the Table Input step and the target database in the Table Output step:
And then I have a second transformation where I fill up all the information (metadata) to inject: The query to perform in the Table Input step, and all the data I need in the Table Output: Target table, if I need to truncate before inserting, the columns from (stream field) and to (Table field):
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.
i am working on a large database ,how do i Export some database tables without having dba privileges .do i have to copy the structures of the tables and using spool command to get the data in a text file then create the tables and inserting data from the text file?
One of the methods would be to install Oracle SQL Developer and export the required table structures and data using the wizard.
Here is the link to a tutorial which can guide you if you go with this option.
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r30/SQLdev3.0_Import_Export/sqldev3.0_import_export.htm
A second option would be to use SQL Loader to load data in your target tables. But for that you will have to first create the data structures on your target schema and spool the data from your source tables in CSV (comma separated values) or any other eligible format.
Here is a link for SQL Loader.
http://docs.oracle.com/cd/B28359_01/server.111/b28319/ldr_concepts.htm
A third option would be that you create the table structures on the target schema and generate the insert statements from the source schema using a script. Here is a link to such an example.
https://pandazen.wordpress.com/2008/08/18/generate-insert-statement-script-to-extract-data-from-oracle-table/
I would recommend going with the SQL Developer option since it is relatively simple.
in plsql when I create and compile a procedure, whether inside a package or standalone, how specification and body of procedure is saved in database?
Like all the other data about objects etc, the text of the code is stored in the dictionary tables. You can see the package contents by querying user_source, all_source or dba_source, depending on which level you're viewing the data at.
The actual compiled version of the code that Oracle keeps is, as far as I know, an internal thing that isn't available to be queried in the dictionary tables.
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.