How to write execution result of script to a file? - oracle

I have a script a2create . I must save execution result file. Instruction say remember to put sql plus command set echo on in the front of your file. I tried this
#a2create set echo on
or
set echo on #a2create
or
#a2create set echo on a2.lst
but did not work.
I want to write the result to a file which is a2.lst. Appreciate if anyone can help.

Try the following,
define spool_file = 'a2.lst'
SET ECHO OFF
SET NEWPAGE 0
SET SPACE 0
SET PAGESIZE 0
SET FEEDBACK OFF
SET HEADING OFF
spool a2.lst;
#a2create
spool off;

Related

shell script to pass table name as a parameter in sqlplus?

I have a file called table.txt which stores the table names. I want the sql update query to take the table name one by one from my table.txt file. My code is as follows:
while read LINE1; do
`sqlplus username/pwd#tname <<END |sed '/^$/d'
set head off;
set feedback off;
update &LINE1 set enterprise_id = '1234567890' where enterprise_id is NULL;
update &LINE1 set sim_inventory_id ='1234567890';
COMMIT;
exit;
END`
done < table.txt
it gives an error sqlplus not found. Can you please tell what is wrong?
This is nothing to do with passing the table names. The "sqlplus not found" error means it cannot find that binary executable, so it isn't getting as far as trying to connect or run the SQL commands.
Your shell script can only see environment variables from the calling shell if they were exported. If you've modified your PATH to include the location of the sqlplus binary then you may not have exported it; add export PATH after you set it.
Or you can set the script up to not rely on the shell environment.
export ORACLE_HOME=/path/to/oracle/installation
export PATH=${ORACLE_HOME}/bin:$PATH
export LD_LIBRARY_PATH=${ORACLE}/lib:${LD_LIBRARY_PATH}
while read LINE1; do
sqlplus username/pwd#tname <<END |sed '/^$/d'
set head off;
set feedback off;
update &LINE1 set enterprise_id = '1234567890' where enterprise_id is NULL;
update &LINE1 set sim_inventory_id ='1234567890';
COMMIT;
exit;
END
done < table.txt
Incidentally, updating the same table twice isn't necessary; you could do:
update &LINE1 set enterprise_id = nvl(enterprise_id, '1234567890'),
sim_inventory_id ='1234567890';
It would also be quicker to create a list of all the update statements from your file contents and run them all in a single SQL*Plus session, so you aren't repeatedly creating and tearing down connections. But that's outside the scope of what you asked.

how to use batch script to export data from oracle into excel file with column names

This is my bat script:
set linesize 4000 pagesize 0 colsep ','
set heading off feedback off verify off trimspool on trimout on
spool &1
select *
from siebel.s_contact;
spool off
exit;
Then I try to run below command. I can generate a Excel file with data but column names are missing.
C:\Users\jy70606\Documents\Script>sqlplus -s geneos/password#database #C:\Users\jy
70606\Documents\Script\daily.sql daily.csv
My question is, how to make the columns available in my Excel?
Your Bat Script contains the following command to remove the columns
set heading off
You need to switch it on
set heading on
Reference: https://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12040.htm#i2699001

BATCH files: dynamic variable depending on the parameters

I have a simple batch file
#echo off
set a1=application 1
set a2=application 2
set a3=application 3
set app = %%1%
ECHO %app%
What I would like to achieve is when I call my batch file (test.bat) with a parameter it would echo the desired string
so if I call:
test a1
-> application 1
test a3
-> application 3
DISCLAIMER: This is a simplified example.
CALL set app=%%%1%%
note that batch is sensitive to spaces on each side of the =. You attempted to set a variable named appSpace
#echo off
set a1=application 1
set a2=application 2
set a3=application 3
CALL SET app=%%%1%%
ECHO %app%
The call performs sonething similar to evalin eg. Perl, the %1 gets replaced and then the statement SET app=%a1%gets executed.#
Without the CALL you end up with %a1% as value of app, instead of application 1.

.sql file not returning the column headers in csv file

The below code is in batch file script(.bat file) which calls the sql file.
del C:\SREE\csvfile.csv
sqlplus SERVERNAME/Test123#ldptstb #C:\SREE\sree.sql
set from_email="SENDER_EMAIL_ID"
set to_email="TO_EMAIL_ID"
set cc_email="CC_EMAIL_ID"
set email_message="Csv file from application server"
set body_email=C:\SREE\sree.txt
set sendmail=C:\Interface\sqlldr\common\SENDMAIL.VBS
set interface_log=C:\SREE\csvfile.csv
cscript %sendmail% -F %from_email% -T %to_email% -C %cc_email% -S %email_message% -B %body_email% -A %interface_log% -O "ATTACHFILE" -A %body_email% -O "FILEASTEXT"
exit
This below content in .sql file code which executes the SQL Query and stores the data into csv file:
set pagesize 0
set heading on
set feedback off
set trimspool on
set linesize 32767
set termout off
set verify off
set colsep ","
spool C:\SREE\csvfile.csv
SELECT Name, ID, Email, Role, Status FROM csvfile
exit
The output is stored in csv file and getting this file in email.
But theproblem is I am not getting the Column Names in the csv file. I had tried in many scenarios to get the names as a cloumn headings in csv file.
Anyone please help me out with the code to get the column names in the csv file.. thanks in advance...
When you set pagesize 0 headings are suppressed:
SET PAGES[IZE] {14 | n}
Sets the number of lines on each page of output. You can set PAGESIZE to zero to suppress all headings, page breaks, titles, the initial blank line, and other formatting information.
That's the case even if you then explicitly set headings on.
You can either set pagesize to something very large instead, or possibly more helpfully as you probably don't really want the separator line of dashes, generate them yourself with:
PROMPT Name,ID,Email,Role,Status
... before your select statement.
Use GENERATE_HEADER configuration setting amd set it to Yes like
SET GENERATE_HEADER = 'Yes'
See this related thread here https://community.oracle.com/thread/2325171?start=0&tstart=0

Modify "C:\Windows\System32\drivers\etc\hosts" file using VBScript

I want to append a line to C:\Windows\System32\drivers\etc\hosts using VBScript. I tried to read this file first using this code:
Set filestreamIN = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Windows\System32\drivers\etc\hosts",2,true)
file = Split(filestreamIN.ReadAll(), vbCrLf)
for i = LBound(file) to UBound(file)
msgbox file(i)
Next
filestreamIN.Close()
Set filestreamIN = Nothing
But I got an error in the second line: Bad file mode. I ran it using this command:
cscript "D:\Project\AXA\AXADEPROJ-867\add host.vbs"
with cmd being run as an administrator. Any help would be great.
Open the file for appending, and simply output what you want. It will automatically be appended.
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oHosts = oFSO.GetFile("C:\Windows\System32\drivers\etc\hosts")
WScript.Echo oHosts.attributes
Set fileAPPEND = _
oFSO.OpenTextFile("C:\Windows\System32\drivers\etc\hosts", 8, true)
fileAPPEND.Write("192.168.0.1 MyMachine")
fileAPPEND.Close()
Set fileAPPEND = Nothing
Set oHosts = Nothing
Set oFSO = Nothing
Of course that does not address a potential issue of appending data that is already in the file.
If you want to read the file first, open it for reading, read the data, close it, then re-open it for appending and make your changes. There is no need to open it for writing.
If you want to edit the file, read it in, close it, reopen it for writing, and write out the edited data.
C:\Windows\System32\drivers\etc is a directory.
here is the bat file inc case you need
type "%windir%\system32\drivers\etc\hosts" | find /i "WEBSITE1" || echo 10.0.0.0 WEBSITE1 >> "%windir%\system32\drivers\etc\hosts"
type "%windir%\system32\drivers\etc\hosts" | find /i "SERVER1" || echo 10.0.0.0 SERVER1 >> "%windir%\system32\drivers\etc\hosts"

Resources