Oracle SQL Developer Spool function is limiting my output? - sql

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)

Related

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.

Create text file in oracle using 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.

replacing data in SQL server

I have multiple tables with corrupted data like this:
Good data</title>and some random HTML<p></p><
What I would like to do is only keep "Good data" in the cell and remove everything else. I thought I can just create a small project with EF and clean the db with C# but there might be a quicker solution with SQL only? Can you use regex or some kind of substring function in SQL?
I will manually look at the table and select the field that needs to run through the code, there is no need to automate that at this point.
UPDATE dbo.SQLInjectionVictimTableName
SET UnprotectedColumn = LEFT(UnprotectedColumn, CHARINDEX('<', UnprotectedColumn) - 1)
WHERE UnprotectedColumn LIKE '%<%';
If good data is consistently followed by </, you could use:
UPDATE YourTable
SET BadField = LEFT(Badfield,CHARINDEX('</',BadField)-1)
WHERE CHARINDEX('</',Badfield) > 0

Select count doesnt returns all rows in table

I do have a table in the database, which supposed to have more than 1k rows. The DB is Postgress. I use the following command:
select count(*) from icdten; it returns 1000 which is wrong
and also
select * from icdten;
returns the first 1000 rows, which is wrong I want to have all of them. Googling didnt find, or maybe I was googling the wrong thing.
EDIT1: I use PgAdmin, maybe it is PgAdmin issue.. I just did not find that option looking through the interface. It supposed to have 14k rows.
Possible limit is set in PgAdmin. Please look at Options/Query tab of PgAdmin like described in http://www.pgadmin.org/docs/1.4/options-tab3.html (http://www.pgadmin.org/docs/1.16/options-tab4.html for version 1.16 you mentioned):
Maximum rows to retrieve can be set to 1000 (default is 100, people usually change this value), and also "Count rows if estimated less than" may affect to COUNT function (it can use count from table statistics instead of real table count, so try to set temporary this value to a big number)
Here is how to troubleshoot this issue:
First try from psql. While select count(*) should give you the same on both, maybe there is something going on. Also there is no limit there, so you can:
\o testfile
\i select * from icdten
\q (exits psql)
wc -l testfile
If that still shows 1000 rows then you probably have 1000 rows in that table, start making sure you are connected to the right db, querying the table you think you are, etc.
EXPLAIN ANALYSE
May be helpful in that case.
This is not an error. It's simply a default parameter set in some versions of PostgreSQL.
You can change the parameter by going to the PostgreSQL's installation directory and navigating to pgAdmin 4\web. The directory for version 12 looks like this:
C:\Program Files\PostgreSQL\12\pgAdmin 4\web
Open config.py with a text editor and search for ON_DEMAND_RECORD_COUNT. It is initially set to 1000.
##########################################################################
# Number of records to fetch in one batch in query tool when query result
# set is large.
##########################################################################
ON_DEMAND_RECORD_COUNT = 1000
You can comment this line out but it could result in other errors. I would suggest to change it to a large value like 10,000,000. If you can't save the file after the modification you need to copy the file somewhere else, make the changes and save, then copy back to the original folder and replace with the original file.

Way to know the amount of data generated by a given query?

Typically, it is possible to know how many lines a query returns by using COUNT(*).
In the same manner, is there any way know how many megabytes for example the output of a given query is ?
Something like
SELECT MEMORYUSE(*) FROM bla bla bla
EDIT : I like the *exec sp_spaceused ... * approach, as it can be scripted!
For completeness, there are a couple of options to give you more information about the executing / executed query that you can view / set using SSMS as well. As shown below, the rowcount for the query is shown in the bottom right of SSMS by default. Also, I've highlighted the advanced query options, which you can set globally as shown here. Of course, you can also turn any of these options on for the particular statement or batch by including them in the query, i.e. 'set showplan_test on', etc..
Also you can turn on 'show client statistics' in SSMS as shown below (with sample output).
If you're using SQL Server, turn on Client Statistics and you 'll find "Bytes sent from client" and "Bytes received from server"
Here is related question
SQL Finding the size of query result
I think this will be useful:
SQL Server Query Size of Results Set
I don't think there is anyway without creating a temp table for the results and checking the size.