How to check the record in a spooled file in Unix - sql

I have to generate a csv file by creating a sql query(oracle) and then mail it to the user. Up to this part I have completed and is working fine.
But if there is any data returned in the spooled file, then only the mail need to be sent.
I am having little trouble because my mail query is in sql part, so for checking the file do I need to displace its order
i.e. sqlplus -s user/pass <
mail logic(calling a procedure)
spool off;
exit
EOF
and how do I need to check the file size/count with which unix command s
or do I need to write this part in another sqlplus -s block and check with count of records
Can someone please suggest with the command or syntax for this as I am totally confused.Thanks in advance

You can create a unix script and call the sqlplus script within and before u send the email you can check if the file size is NOT zero.

Related

Large File export to postgreSQL

I need to export a 50gb file with inserts to a table in postgreSQL to be able to count the time it takes to perform the inserts, but I can't find any way to load that file, can someone help me?
If the file have you have contains syntactically valid SQL (like INSERT statements), this is very straightforward using the command line psql client that comes with a Postgres installation:
psql DATABASE_NAME < FILE_NAME.sql
You may also want to replace DATABASE_NAME with a connection string like postgres://user:pass#localhost/database_name.
This causes your shell to read the given file and pass it off to psql's stdin, which will cause it to execute commands against the database it's connected to.

Want remove the word "Connected" while connecting to the sql plus and taking the output in the csv file

I am writing the shell script to move the data from the SQL plus to Sybase. But before that I need to take the output to the csv file which I have done.
I want to remove the "Connected." word form the csv file so that I can have the data columns only so that I can read the data from the file and insert it in the sybase.
I tried searching but unable to find a proper command. I have used the following but none worked for me.
set heading off
set echo off
set feedback off
I found the solution:
I used /dev/null for silencing all the notifications and the spool command to output the result. It works great.

Avoiding Errors getting copied into CSV while spooling out in Oracle SQL

I have a SQL code which spools out data into a .csv file from Oracle databases.
set echo off
set feedback off
set linesize 1000
set pagesize 0
set sqlprompt ''
set trimspool on
set verify off
spool test.csv
/*Code Part*/
/
spool off
The problem is that, if at all any error occurs (e.g: resource busy issue) while executing code part, those error messages are getting copied into .csv file along with the spooled data. Is there any way to avoid it?
It would be much more helpful if some one suggests me a way to redirect these error messages to a .txt file (I dont know if it is possible or not).
Thanks!
You may want to look into using the UTL_FILE package to create the file you want rather than counting on SQL*Plus to redirect your output. While this would perhaps require some rewriting on your part you'd end up with better control of what is being written.
Another option would be to filter the output file to eliminate . For example, if the lines you don't want to see all start with "ORA-" you might be able to use something like the grep command:
grep -v ^"ORA-" test.csv
Or you might have to use something like sed or awk if your filtering requirements are more complicated.
Share and enjoy.

Log file avoid dumping sql query data in it.. c shell

I have sql script "example.sql":
SPOOL &1
Select '<.TR>'||'<.TD align="left">'||column_name||'<./TD>'||'<.TR>' from table1;
spool off
which dumps it contents to cshell script "getdata.csh" this is how i get data from sql script to csh script
sqlplus $ORA_UID/$ORA_PSWD #${SQL}example.sql ${DATA}${ext}
My problem is when I run my job to do this it dumbs all data extracted from sql query in log file too.. How can I avoid dumbing data into log files. What do I have to do in this code to do so? thank you
Look here for information about your issue: sqlplus spool
Essentially, you will need to add these two settings:
set echo off
set termout off

Oracle SQLPlus: How to display the output of a sqlplus command without having to first issue the spool off command?

Is there a way to display the output of a sqlplus command without having to first issue the spool off command?
I am spooling the results of a sqlplus session to a file while at the same time tailing the file. The reason for this is that for table with very long rows the format is easier to look at from a file. The problem is to see the output i have to issue the spool off command everytime i run a command in sqlplus.
Can i configure sqlplus so that after i have issued the spool command all the output is viewable straight away on the file.
(Formating the way the rows are displayed on the screen is not an option. )
THanks
SPOOL is really intended for creating a file of SQL*Plus output, for whatever purpose: logging, input to another process, etc. There is no facility for inflight viewing of its output.
There are a number of ways of solving this particular problem, but the easiest is surely to use an IDE which includes a data browser, thus obviating the need to tail a file. There are a number on the market, including Quest's TOAD and Allround Automation's PL/SQL Developer, but if you don't want to spring for a license fee then you should have a look at Oracle's own (free) SQL Developer.
If you are spooling the results of multiple statements you could turn spooling off and then turn it back on between each statement. When you turn spooling back on add the append keyword so that it will continue in the same file rather than overwriting it.
If you want to see the results of one query in your spool file you could break the query up into multiple queries that returned specific ranges of the data. This would be slower, but you could cycle spooling to get faster feedback.
If your problem is that you can't open the output file (as the spool process has a lock on it) then try copying the file output to another file and opening that file instead.
Since it sounds like your real problem is formatting of output in SQLPlus -- can you make your SQLPlus window wider and SET LINESIZE so the output looks better in SQLPlus to start with? Then you might not need to spool at all.
I tried to add a comment but for some reason it doesnt save it so ill try the "Answer your question" option :)
I do use SQLDeveloper but there are situations where i have to use sqlplus where SQLDeveloper is not available then i am stuck with plain old sqlplus.
There are other situations where i would use sqlplus over sqldeveloper purely for the fact that it would take me 1/2 minute to find out what i am looking for in sqlplus rather than several minutes with SQLDeveloper as it would take ages to load.
I have checked the time it takes before the output is flushed out and it looks like it does flush it out after a certain number of rows. Isnt there a way to reduce the buffer so that they are flushed out quicker?
There is no problem opening the file the problem is even with the file opened i cant see the output unless i issue the "spool off" command or the output has several hundred rows. I am using a free program called baretail (http://www.baretail.com) to tail the spool file on windows.
Thanks