Creating and Writing in a Text File using DOS Command - dos

I want to know how can I create a file named e.g myfile.txt in a D:\MyFiles directory and write first & last name in it
I wrote below code for that.
MD D:\Workdirectory\PRIVATE\DOCUMENT\type myfile.txt First_Name Last_Name

This would also work:
#echo First_Name Last_Name > D:\MyFiles\myfile.txt
If you want to append to the file you can do this:
#echo First_Name Last_Name >> D:\MyFiles\myfile.txt

You could do something like the following:
echo First_Name Last_Name > D:\MyFiles\myfile.txt

Related

How to get plain formatted output in csv with shell script for oracle sql query

This is my shell script.
echo "Start";echo #/opt/apps/Tests/SQLDir/Test1.sql | sqlplus Db1/Db1#//maydomain:port/abc;echo "Finish";
echo "Start";echo #/opt/apps/Tests/SQLDir/Test2.sql | sqlplus Db1/Db1#//maydomain:port/abc;echo "Finish";
I have 30 .sql files like this, added in one .sh file which results 30 .csv files
Test1.sql has
SPOOL /opt/apps/Tests/OF/output1.csv REPLACE;
select name from username where id = 10 and Sname is not NULL and ROWNUM < = 50000;
Test2.sql has
SPOOL /opt/apps/Tests/OF/output2.csv REPLACE;
select strname,ctyname from addr where city = 'NYC' and ROWNUM < = 50000;
My expected OP in output1.csv is
name
Abc
xyz
pqr
My expected OP in output2.csv is
strname | ctyname
10-AP NYC
11-KP MCH
90-ZP SDK
right now I am getting weird o/p in csv
name
-------------------------------
Abc
xyz
pqr
name
-------------------------------
TYU
KLH
50000 rows selected.
SQL>
So is there any way to remove those additional lines [--------- and 50000 rows selected.] with shell script/sql code?
And while executing shell script all sql result rows are getting printed on screen. how to avoid that?
Thanks in advance.
Following SQL*Plus command should do the job:
set markup csv on delimiter ' ' quote off
set feedback off
I must say, your method of passing the name of a script to sqlplus is the strangest I've ever seen. The usual practice (given your names) would be:
sqlplus Db1/Db1#//maydomain:port/abc #/opt/apps/Tests/SQLDir/Test1.sql
I don't see where your 'echo Start' and 'echo finished' accomplish anything, since there is no clarifying info coming along with it.
It looks to me like what you want in your scripts would be
set echo off trimsp on head off pagesize 0
spool /opt/apps/Tests/OF/output2.csv replace
select strname,ctyname from addr where city = 'NYC' and ROWNUM < = 50000;
spool off
BTW, 'spool' is a sqlplus command - a directive to sqlplus itself, not a sql statement. As such, it does not need a semi-colon at the end.
-- edit
Example of using environment variables on sqlplus command line:
username=scott
userpw=tiger
server=myserver
port=1521
dbname=mydb
sqlplus $username/$userpw#//$server:$port/$dbname
Though I would question why you need to set them as variables.
And I prefer to use tnsnames rather than ezconnect.

How to use a variable from Bash into SQLPLUS?

I'm trying to write a script which would accept the customer ID using BASH ( using a basic read command ) and then I want to use that BASH variable in my SQLPLUS query. How can I do that ? I'm trying to use below format, but it is not working.
echo "Enter Customer ID :- ";
read Customer
sqlplus username\password#host
select first_name from customer where customer_id = $Customer;
quit
exit
Typically, you would do:
echo "select first_name from customer where customer_id = $Customer;" | sqlplus username\password#host
If you want to run multiple queries, it is common to use a heredoc:
cat << EOF | sqlplus username\password#host
select first_name from customer where customer_id = $Customer;
select first_name from customer where customer_id = $Customer;
EOF
edited in response to query in comment:
to store the result of any command in a variable you can use process substitution. var=$( cmd ). In the heredoc case, the syntax is:
var=$( cat << EOF | sql...
query
query
EOF
)

What is wrong with this perl script

Below is the Perl script that I have written
my $COUNT_1;
my $parameter1 = 'PU_CLERK';
$COUNT_1 = `sqlplus -s hr/password\#dbname\#sql_script.sql $parameter1`;
SQL SCRIPT:
select count(*) from employees
where job_id <> '&1'
and salary > 9000
and commission_pct is not null
order by first_name desc
/
exit;
When I run this query by passing the argument &1 it is giving me a string with an error message. But when I run the same query by hardcoding I'm getting the output properly (the count is 15 which is the correct answer).
select count(*) from employees
where job_id <> 'PU_CLERK'
and salary > 9000
and commission_pct is not null
order by first_name desc
/
exit;
I'm not able to understand where I'm going wrong. How do I pass parameters in Perl. We used to do the same way in shell script and it was working absolutely fine.
EDIT:
This is the error message im getting
perl call_sql.pl
value of first variable isold 2: whe
re job_id <> '&1'
new 2: where job_id <> 'PU_CLERK'
15
So its basically not printing the 15 value its printing all those string also when i use '&1' in my sql script
EDIT2:
Hi Guys finally it is working. In my sql code instead of giving '&1' i gave '$1' Now i want to know is $1 of some significance in Perl? Thanks..
I don't know the answer to your current problem, but using the DBI module is the better solution, so I wrote a sample script to get you started. You may need to tweak some things to get it to work.
use strict;
use warnings;
use DBI;
my $dbname = "mydb";
my $user = "foo";
my $passwd = "bar";
my $dbh = DBI->connect("dbi:Oracle:$dbname", $user, $passwd)
or die $DBI::errstr;
my $parameter1 = 'PU_CLERK';
my $statement = "select count(*) from employees
where job_id <> ?
and salary > 9000
and commission_pct is not null
order by first_name desc";
my $sth = $dbh->prepare($statement) or die $dbh->errstr;
$sth->execute($parameter1) or die $sth->errstr;
while (my $row = $sth->fetchrow_arrayref) {
print "#$row"; # or whatever you want to do with it
}
$dbh->disconnect or warn $dbh->errstr;
This has nothing with perl.
Proof: make an shell script, say mytest.sh with the next content:
#!/bin/bash
echo "$0: Got $# args" >&2 #to stderr
i=0
for arg
do
let i++
echo "$0: arg($i)=$arg=" >&2 #to stderr
done
echo "15" #result to stdout
make it executable, with chmod 755 mytest.sh
Now modify your perl script as:
my $COUNT_1;
my $parameter1 = 'PU_CLERK';
$COUNT_1 = `./mytest.sh -s hr/password\#dbname\#sql_script.sql $parameter1`;
print "script returned: $COUNT_1\n";
run it
$ perl script.pl
result:
./mytest.sh: Got 3 args
./mytest.sh: arg(1)=-s=
./mytest.sh: arg(2)=hr/password#dbname#sql_script.sql=
./mytest.sh: arg(3)=PU_CLERK=
script returned: 15
e.g. the perl
correctly run the external script
correctly passes the arguments to it
so, search for the error in the sqlplus doccumentation...

how to create a file with these sqlite instruccions using a .bat script?

I want to create the next sqlite instructions using a .bat script and save them in a temp.txt file. The problem is that some parts of the instructions are variable and depend of a list of files in the current directory.
Example:
In my current directory I have the next files: file1.txt, file2.txt, file3.txt and some other that I dont care...
So I need to create the next sqlite sentences:
insert into table1(fileName) values('THE VARIABLE FILE NAME');
insert into table2(something1) select (idTable1) from table1 where fileName = 'THE VARIABLE FILE NAME';
.import THE VARIABLE FILE NAME table3
So the sentences must look like this:
insert into table1(fileName) values('file1.txt');
insert into table2(something1) select (idTable1) from table1 where fileName = 'file1.txt';
.import file1.txt table3
insert into table1(fileName) values('file2.txt');
insert into table2(something1) select (idTable1) from table1 where fileName = 'file2.txt';
.import file2.txt table3
insert into table1(fileName) values('file3.txt');
insert into table2(something1) select (idTable1) from table1 where fileName = 'file3.txt';
.import file3.txt table3
The file where those instruccions will be saved is named temp.txt
So in the end of myScript.bat I want to execute something like this:
sqlite3 < temp.txt
And the information in those files will be imported into my database.
And I already know that I can list those files in my cmd with the next instruction:
dir f*.txt /b
I also know that I can create a empty file like this:
echo. 2> temp.txt
And that I can write the text that I want into that temp.txt file like this:
echo TheseAreMyInstructions >> temp.txt
So myScript should start like this
#echo off // Wont show more info...
echo. 2> temp.txt // Will create the empty file
And I tried something like this
FOR %%a IN (dir f*.txt /b) DO echo insert into table1(fileName) values('%%a') >> temp.txt
I dont know how to put the other instructions here inside the for
And the results arent what i expected...
The results in my temp.txt file are:
insert into table1(fileName) values('dir') // This should not be here!
insert into table1(fileName) values('file1.txt')
insert into table1(fileName) values('file2.txt')
insert into table1(fileName) values('file3.txt')
insert into table1(fileName) values('/b') // This should not be here!
Please help me I really have to do this...
Thanks for Your help! ;)
To execute commands for a set of files, just specify the file pattern:
for %%a in (f*.txt) do (
echo ...%%a >> sql.txt
echo %%a... >> sql.txt
)

Including pipe running Oracle sql from DOS batch script

We use the approach below to run SQL from a DOS batch script.
The model works fine but this specific code doesn't work. I believe because of the || characters. I tried using ^|^| but this didn't work.
Any ideas?
(
echo update mytable set file_path = 'C' || substr(file_path, 2);
echo commit;
echo exit
) | sqlplus x/x#orcl
Store the SQL as file and redirect SQL Plus's input:
sqlplus x/x#orcl <sql.txt
You can use CONCAT instead of the || operator
Escaping the || with ^|^| leaves you with yet another problem: cmd.exe thinks that the closing parenthesis of substr(file_path, 2); belongs to the opening parenthesis in the first line. It is therefore not printed to SQL*Plus, thus rendering the update statement to something like update mytable set file_path = 'C' || substr(file_path, 2 which obviously cannot be interpreted by Oracle.
You can solve this if you put the entire update statement into double quotes and feed this to (yet another) cmd.exe, like so:
(
#echo select * from mytable;
#cmd /c "echo update mytable set file_path = 'C' ^|^| substr (file_path, 2);"
#echo commit;
#echo exit
) | sqlplus x/x#orcl

Resources