Extract table procedure Oracle [closed] - sql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Is it possible to create any procedure to extract tables from Oracle database?
Something like i could select which table and which fields that i want to extract, so finally create a CSV file of table. In future i will need for oracle ERP too, but at this time just for database.
I'm working in a audit project, so field consultants needs constantly extract tables from clients, and all this is by hand-made (table per table, script per script, there is so many waste time). So this way i'm trying to automated these extractions.
Thank you!

I can just give you the idea.
Create a procedure which accepts the Table name and comma-separated column names as two input parameters with a third out parameter which will be used to pass the status of the procedure at the end to the calling user.
CREATE PROCEDURE FETCH_TABLE_DATA( P_IN_TABLE_NAME IN VARCHAR2,
P_IN_CS_COLUMN_NAMES IN VARCHAR2,
P_OUT_MESSAGE OUT VARCHAR2)
You can now create the cursor using dynamic query and use something like:
'SELECT ' || COLUMN_NAMES_COMMA_SEPRATED || ' FROM ' || TABLE_NAME
and using UTL_FILE, you can put this all data to the text file.
In case of success, you can assign the name of the file to the P_OUT_MESSAGE or in case of failure, You can show the error message by assigning error message to P_OUT_MESSAGE
Search all the keywords used in the answer to prepare the procedure by yourself.
Hope, It will give you proper direction.
Cheers!!

If you use SQL Developer or Toad for Oracle, you can simply export a query on a table or the whole table to csv.
Oracle SQL Developer:
This is by far the easiest way for someone not familiar with complex PL/SQL stored procedures to export data.
One way to do this is to create a Report in SQL Developer (see this tutorial at Oracle for more information on this feature) and export the results
Once the report is created and ran, right click on the results grid and choose “Export” from the context menu
In the next screen select CSV as the format and enter in the filename.csv and location
You can also export a single table by:
Right-click the table name in the object tree view.
Select Export.
Select CSV. The Export Data window shows up.
Click Format tab.
Select Format as: CSV
Enter a file name and location.
Click Columns tab.
Check the columns you wish to export
Click Where tab and add any criteria needed to filter the data
Click Apply.
PL/SQL:
The best way to export data via PL/SQL is to create a stored procedure that uses the UTL_FILE command.
For a great example stored procedure for this, please see this article at oracle.com. Note that there is a recommended change to the original stored procedure located in the comments (properly handles the quoting of data with reserved CSV characters).
Now you are ready to upload the new CSV File to SpatialKey.

Related

Program to update the database table from the parameter with the excel sheet from select option in ABAP

Will come directly to the question.
Have 2 parameter like filename and table name. The requirement is to upload the data from the excel sheet to the database table enter in the other parameter. This should be in run time. No hardcoding of field names and that program should be flexible enough to suite any table. Please help.
I can think of two possible approaches:
Dynamic code generation -- write a program which writes a program
Use dynamic type tools
For 1. try googling
For 2. see https://wiki.scn.sap.com/wiki/display/Snippets/Example+-+create+a+dynamic+internal+table - this wiki shows a way (not sure if it is overkill as it creates the type from scratch whereas any table in your SAP system is already a defined type in the Data Dictionary).
You can do easily reference a parameterised table in Open SQL e.g. MODIFY (p_tab) ...
Perhaps you could do a generic SPLIT of a line read in from file by the delimiter into a table of fields - you can then use ASSIGN COMPONENT to match the fields you have read in to the fields in your internal type.
If you are doing this I think a white list of allowed tables would be wise - and auth checks. Otherwise someone could upload SAP standard tables with no authorisation.

Populating tables in SQL Developer using data from a file

I have a txt file containing a table with two columns student ID and GPA. I want to create a similar table in Oracle SQL developer. Is there some way to copy this data directly into SQL developer
If you are looking for a simple GUI method, you can connect to a db, right click on "Tables", "import data..." and use the Data Import Wizard.
Select the correct options (csv/delimited, tableName, cloumnsToImport...) and click "Finish".
Sorry, I cant post pics yet see the example screenshot here: http://i.stack.imgur.com/S7HFx.png
You can create external table with that file, then copy the external table into a new internal table (usual table), here is a full example:
External Tables Concepts
Then go to:
Example: Creating and Loading an External Table Using ORACLE_LOADER

How to copy data and schema of all tables into a different database on the same server? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Using a script (however without using data import/export, without using backup restore, without using database copy) how can I copy complete tables (schema and data) from one database on the same server to another database on the same server?
I've already tried to do what I've italicized above; however, due to user permissions etc, I was unable to use those methods.
Perhaps this will work:
SELECT *
INTO newdb.dbo.newtable
FROM olddb.dbo.oldtable
But I would like to apply this to every table (around 100 tables).
If you're truly a sysadmin, I don't understand what's wrong with backup and restore.
BACKUP DATABASE <DatabaseName, sysname, DatabaseName>
TO DISK=N'<backup_location, varchar, BackupLocation>.bak'
WITH INIT, FORMAT, COMPRESSION, COPY_ONLY
RESTORE DATABASE <NewDatabaseName, sysname, NewDatabaseName>
FROM DISK = N'<backup_location, varchar, BackupLocation>\
<DatabaseName, sysname, DatabaseName>.bak'
WITH
MOVE '<DataFileName, sysname, DataFileName>' TO '<DataMDFPath, nvarchar(600), DataMDFPath>',
MOVE '<LogFilePath, sysname, LogFilePath>' TO '<LogLDFPath, nvarchar(600), LogLDFPath>',
REPLACE;
Here, see, I even templated it for you.
If you have SSMS, you can right-click the database and select "Script Database As" -> "Create To". This will generate DDL scripts for all the objects you want (tables, indices, views, etc)
After you run those, use:
SELECT 'INSERT newdb..'+name+' SELECT * FROM olddb..'+name FROM sys.tables
To generate the copy commands. If you have computed columns, you'll need to join the sys.columns table to filter those out. If you have multiple schemas, you'll need to specify them. You'll also need to sort these DML commands based on your foreign key hierarchy. With only 100 tables, it's feasible to do that manually.
Are you using Microsoft SQL Server Management Studio? If so...
You can use the Script Table As... Option and select the New Query Editor Form under the CREATE TO option.
From there you should just be able to change either the Table Name, or the targeted Database.
In conjunction with this you can use the information found here: https://stackoverflow.com/a/7515236/2543917 in order to move over your data.
If you need some more actual code examples then let me know and I can provide.

Procedure to create .csv ouput [duplicate]

This question already has answers here:
Procedure to export table to multiple csv files
(2 answers)
Closed 9 years ago.
please help me to create a procedure to export data to specified path from oracle database as .csv file take study names from another table.
QUERY : select * from enrollment where study_name = 'FTY67' ;
I have another table(studies) in same database with all studynames.
is there any way to create procedure that will take study names from studies table and repeat this procedure to create .csv files for all studies?
read some articles in internet but not found anything related to this.
please help.
You should look into the spool and set command. Since the database is usually running on a remote server, it can not write files to your local computer. In order to achieve this yu must write an sql where you disable certain characteristics in the terminal, and then spool the result into a file that you can access.
Something like this might get you started:
set term off colsep ";" pause off
spool myfile
select * from x;
spool off
exit
For an overview of the options you can use with SET refer to the oracle documentation here: http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12040.htm
With the proper set commands you are able to create the CSV file.
The above set commands are just a few you might need, but you will probably need addtional parameters to make your CSV usable.
Best to write this in a file.sql and run it using sqlplus:
sqlplus user#db #file

update script for BLOB column in ORACLE needs to be created

I want to create update script for BLOB column in Table which stores XSL Data in ORACLE. Can anybody help me in simple way without creating any directory. Here number of character involved is also more than 4000.
I have modified in TOAD by 'Save to File' and again from 'Load to File'. Now I want to transfer it to some other database using SQL Script.
Using the Oracle IMP and EXP utilities you can export a table into a file and import it into another database. Here is some information on how to use them:
http://www.orafaq.com/wiki/Import_Export_FAQ
It is not SQL but it also doesn't involve creating directories.