Create text file in oracle using sql - sql

I need to grab data from some oracle database tables and format it into a fixed width text file.
I want to know if its possible to create a text file using sql.
I looked at some stuff and found the bc and xp_cmdshell but they are a bit confusing.
I am pretty new to sql and oracle databases.
Is this possible and how can I begin?
I don't need to open a file or check for existing file, overwriting is fine, what ever makes it easiest.
I don't need anything big or complex, a simple script to grab values and create a text file.
Just an update:
I don't think bcp works in the toad for oracle editor.
I found this tutorial here: http://www.sqlteam.com/article/exporting-data-programatically-with-bcp-and-xp_cmdshell
but the first bcp command does not compile, it says invalid sql query

If you are using the SQL*Plus client, you can spool to an output file. Here is a sample SQL*Plus file:
set serveroutput on size 1000000
set linesize 150
spool C:\path_to_file\filename.extension
-- Your SQL statement
select columns
from table
where somecondtion;
-- Your next SQL Statement
select ...
from ...;
spool off

I think you can use sqlplus command line tool todo this. See oracle manual for formatting hints.

Related

SQL developer spool to txt row

I have a bat file that calls sql developer and spool out a query to a text file, however the result is all in one row, it seems it doesn't know how to identify a row.
For example, if I run the spool script in sql developer manually, the txt file looks perfect like this:
"Item","Qty","Price"
"A11","4","0.86"
"A12","3","0.56"
"A14","5","0.3"
But if I ran it with the bat file, it came out like this:
"Item","Qty","Price""A11","4","0.86""A12","3","0.56""A14","5","0.3"
Without the right format, when I import it to excel file, all the data are just in one cell.
I have tried all kinds of format like SET PAGESIZE, SET TERMOUT...but none of these work. In my another device I ran exactly the same code, and I do not have this problem.
bat file code:
#echo off
C:
cd C:\sqldeveloper\sqldeveloper\bin
sdcli migration -actions=mkconn,runsql -connDetails=target_oracle:oracle:XXXXX -conn=target_oracle -sql="C:\Desktop\1.sql"
1.sql:
spool "C:\Desktop\test.txt"
#C:\2.sql as script(F5);
spool off
2.sql:
Select /*csv*/ * From (
select * from item
);
I got stuck here for a while, if you have solution please let me know, thank you.
The answer is to use the right SQL Developer command line interface for the job.
We have two:
SDCLI - this is a headless version of the full SQL Developer program - fancy way of saying, no GUI. It can do things like perform a database export or invoke a Cart feature. Using it to run a SQL statement via the Migration task is like using a flamethrower to defrost your car's windshield - although probably not nearly as fun.
SQLcl - this is a command-line, interactive interface to the Oracle database. It's a java based version of SQL*Plus. It has the same code as SQL Developer when it comes to making connections, running scripts, etc - but it's only 20MB vs 200+MB, and it only need a JRE vs a JDK.
Both of these programs are in your sqldeveloper\bin folder - but SQLcl is also a separate, standalone, supported product.
So, to do what you want, you need to change this:
sdcli migration -actions=mkconn,runsql -connDetails=target_oracle:oracle:XXXXX -conn=target_oracle -sql="C:\Desktop\1.sql"
to this:
sql user/pwd#server:port/service #c:\users\jdsmith\desktop\1.sql
And your 1.sql can be this:
spool c:\users\jdsmith\desktop\locations-so.csv
Select /*csv*/ * From locations;
spool off
exit
Which gives us this
Bonus: SQLcl will run through this MUCH faster.

Oracle SQL automated reports

I teached myself how to use Oracle SQL using Oracle SQL developer and in the right now I am able to generate some very useful overviews for my manager (as a separate task from my normal job). However, I always have to use Oracle SQL developer to get these overviews and then I have to export them to PDF/XLSX manually.
I am wondering if there is any possibility to generate these reports automatically and get a PDF/XLSX containing all information in you email or a specified folder. I tried some external software, but no succes.
in Sqlplus we can get CSV output or HTML output , try using Sqlplus
SQL>
SQL>set markup html on spool on
SQL>SPOOL /tmp/xyz.HTML
SQL>set pagesize 200
SQL>set echo on
SQL> select * from myTable;
SQL>spool off
SQL> set markup html off spool off
If you have enouh privileges to create view, you can compile one with statement of your query and link via ODBC with Excel file.

How to run an oracle query in linux with a table like output

I'm totally new in running sql queries in linux and I'm having a hard time dealing with it's output.
So I managed to access my database in oracle in linux and trying to run a simple query right now:
SELECT IN_01, OUT_BD_01 FROM TRANSLATION_ROW WHERE IN_01 = 'LS3K5GB';
I'm expecting it to be in a table-like output but instead i got this:
Any Help would be much appreciated. By the way, I'm accessing my Oracle server through putty. I don't know if that helps in anything.
--forgot to mention that I also use sqlplus. Don't know if that would make any difference
Thanks in advance.
Welcome to the weird and wonderful world of Oracle.
Viewing large amounts of data (especially "wide" data) through sqlplus has always been less than pretty. Even back in the 1990s Oracle rival Ingres had a rather nice isql which made a much better fist of this, although the flipside of that was using isql to spool to a data file (no headers and trimmings, etc) was slightly harder. I think the rather primitive nature of SQLPLus is why TOAD/SQL*Developer etc have become popular.
To make the output easier to read, you need to learn the basics of sqlplus formatting, in particular SET LINES, PAGES, TRIMSPOOL, TAB, and the COLUMN formatting command.
Use COLUMN to control the formatting of each column.
One possible option is to use SET MARKUP and spool to a file, which formats the output as HTML table, but then you need a HTML viewer/browser to view the results.
On PuTTY your options are limited, but if you have xterm and can invoke the browser on Linux, you might find something like a shell script:
#!/bin/bash
sqlplus un/pw #the_file
firefox the_output.html
Contents of the_file.sql:
SET MARKUP ON
spool the_output.html
SELECT * FROM user_objects;
spool off
quit
If you have a share between the Linux system where the the_output.html resides and can mount that on WIndows, you could run the query on Linux with MARKUP oN, spool to the share, then click refresh on the Browser.
Clunky, and not really what you want, but try it and see what you get.
It displays the entire column that's it.
You can format your column before running the query with the below:
e.g.: format my column to display 10 characters only
column IN_01 format a10
There are some basic configuration tricks that you should apply when using SQLplus. A basic set of parameters would be something like this:
set pagesize 50000
set linesize 135
set long 50000
set trimspool on
set tab off
All these should be placed in a login.sql file which should be in the directory you are launching sqlplus from.
This will solve your current problem, but for further reading I suggest checking out this page: Configuring sqlplus.

Oracle SQL Developer Spool function is limiting my output?

I am working with SQL right now and I am trying to write a bit of code that pulls a section of data from a database and saves it off to a file. This particular section of code is usually formatted all on one line and about 22,000-23,000 characters long on average. I can already pull some of the code but the pull stops after 4002 characters. My current code looks something like this:
SET HEADING OFF
SET ECHO OFF
SET LONG 100000
SET WRAP OFF
SPOOL output.txt
Select ________ (my select statement already works on its own);
SPOOL OFF;
I don't know the SQL language at all, I'm looking for some direction as to what functions I could research to help me out?
My end goal with this code is to be able to enter a value in, then have my code use that value to pull a value from one database. From there use both values to pull a long string of code from another database, would this kind of thing be possible in SQL?
Try adding this
SET SERVEROUPUT ON SIZE 1000000
I would really suggest that you try and view the SQLPlus Help.
It's really useful, and will explain all parameters to you, which is very useful.
Good Luck :)
In SQL Developer set
Tools > Preferences >> Navigate to Database > Worksheet > Max rows to print in a script(Increase number)

Inserting a single row in SQL, but loading the contents from a file

I can INSERT a row into SQL like this:
INSERT INTO MyTable VALUES (0, 'This is some text...');
However, what if I wanted This is some text... to be the contents of C:\SomeFile.txt. Is there a method in Oracle that makes this possible?
I dug through the Docs a bit and found the LOAD FILE method, however it appears to be for bulk loading data. For example, it wants a FIELDS TERMINATED BY parameter and what not. I want to simply INSERT a single row and set a single column to by the contents of a file on the local disk.
You should never be reading files from the DATABASE SERVER's file system to insert into the db.
Why do you want to do this? You really should read the file in your application code, then insert the string or binary data through standard SQL.
If you really must do this you will need to use the oracle UTL_FILE function. and write some PL/SQL to store in a variable, then insert.
My first time answering a question, so forgive me.
If you're using PHP (which you may well not be, but this is what I know), then you could do something like this:
File: "importantfile.php"
$variable 1 = "0";
$variable 2 = "This is some text...";
File that inserts text: "index.php"
require "importantfile.php";
$query = mysql_query("INSERT INTO MyTable VALUES ('$variable1', '$variable2')");
I hope this helps you in some way :)
To do this I think you would have to use PL/SQL. This is technically an "advanced SQL".
search up a bit about PL/SQL. You can copy your SQL directly into PL/SQL and use the same database.