Hello dear stackoverflowers,
I am writing a shell script in which I need to dump a sqlplus database content in a file, I need to have it done in one command line.
echo 'SET LINESIZE 1000; select * from myDb;'| sqlplus -S user/pass#host
But I get
SP2-0268: linesize option not a valid number
I tried without ';' but I have the same result.
I couldn't find any example here or elsewhere on how to do this.
Any help on that?
Thanks!
echo 'SET LINESIZE 1000\n select * from myDb;' |sed 's/\\n/\n/' |sqlplus -S user/pass#host
Related
I am trying to run a basic shell script to query my Oracle database and give me back results with a pipe | delimiter versus a comma.
No matter what I try it only returns with commas. Any help would be appreciated.
#!/bin/bash
sqlplus -s $DBUSER/$DBPASS#$DBCONN/$DBSID <<EOF
set colsep '|'
set echo off
#$SCRIPT_PATH/QRY_1.SQL
exit;
EOF
var=$(sqlplus -s username/pwd$#SID <<EOF
set heading on
set trimspool off
set linesize 200
set feedback off
SET MARKUP HTML ON
spool output.htmL
SELECT * FROM V$SESSION where username='FSUSER' and PROGRAM='SQL Developer' and OSUSER NOT IN ('username');
spool off;
exit;
EOF)
echo "$var" > output.txt
exit 0
but the output files shows as ORA error , manual connect is working without any issues from same server .
ERROR: ORA-12162: TNS:net service name is incorrectly specified
SP2-0306: Invalid option. Usage: CONN[ECT] [{logon|/|proxy} [AS
{SYSDBA|SYSOPER|SYSASM|SYSBACKUP|SYSDG|SYSKM}] [edition=value]] where
::= [/][#<connect_identifier>]
::= [][/][#<connect_identifier>]
Any help here is appreciated
With a here-document (the construction <<EOF ... EOF) the end-tag should be the only characters on the line. Therefor replace
EOF)
with
EOF
)
Your code has another problem: The $ in V$SESSION is evaluated/parsed, resulting in errors. Parsing can be avoided with a backslash V\$SESSION or by putting quotes around your start-tag EOF:
var=$(sqlplus -S /nolog << 'EOF'
....
EOF
)
I have to create 1 UNIX Shell script. In that shell script i want to run multiple SQL script files from the same directory. I have used like this-
#!usr/bin/ksh
SQLPATH = /usr/sql/
(cd $SQLPATH;
'sqlplus usr/password#sid <<EOF
spool <db_file>.log
#<db_name>.sql
set echo off
set heading off
spool off
&&
spool <db_file2>.log
#<db_name2>.sql
set echo off
set heading off
spool off
&&
spool <db_file3>.log
#<db_name3>.sql
set echo off
set heading off
spool off
exit;
EOF')
exit 0
There are multiple SQL scripts like this and for each SQL script I have to create log files so I used spool here. After every SQL script files execute
I used &&. So Is it good to use && here and in 3rd line ; when I define the PATH. Please provide me the better solution.
Don't override the system PATH (or now SQLPATH) and don't put commands in single quotes. Use lowercase for your private variables, and you can't have spaces around the equals sign in an assignment; but a variable you only use once is useless anyway, so I took it out, and hardcoded the cd argument.
I'm guessing you want something like
#!/usr/bin/ksh
# No spaces around equals sign, and don't use uppercase for private variables
# But why use a variable at all anyway
#sqlpath=/usr/sql
cd /usr/sql # is this really necessary and useful??
sqlplus usr/password#sid <<____EOF &&
spool <db_file>.log
#<db_name>.sql
set echo off
set heading off
spool off
____EOF
sqlplus usr/password#sid <<____EOF &&
spool <db_file2>.log
#<db_name2>.sql
set echo off
set heading off
spool off
____EOF
sqlplus usr/password#sid <<____EOF
spool <db_file3>.log
#<db_name3>.sql
set echo off
set heading off
spool off
____EOF
# exit 0 # Not necessary or useful
If the multiple sqlplus commands can be executed in a single session, that would be an obvious improvement; but I'm guessing sqlplus has no way of expressing what you appear to mean with && (this syntax seems to have a quite distinct meaning in sqlplus).
Make a function
#!/usr/bin/ksh
SQLPATH=/usr/sql # Offtopic: Better use lowercase for your own variables
process_sql_and_log() {
if [ $# -ne 2 ]; then
echo "Usage: $0 sqlfile logfile"
return 1
fi
sqlplus usr/password#sid <<EOF
spool ${2}
#${1}
set echo off
set heading off
spool off
exit;
EOF
}
cd "${SQLPATH}" &&
process_sql_and_log db_name.sql db_file.log &&
process_sql_and_log db_name2.sql db_file2.log &&
process_sql_and_log db_name3.sql db_file3.log
exit 0
I have written a shell script. The for loop runs twice. Not sure how to fix this.
Can any one please assist me in resolving the issue?
Code:
#!/bin/sh
mkdir -p C:/Users/spatro/Desktop/attachment_backup
feed=`sqlplus -s <<EOF
$1/$2
whenever sqlerror exit sql.sqlcode rollback
SET SERVEROUTPUT ON
SET VERIFY OFF
SET FEEDBACK OFF
SET HEADING OFF
SET PAGESIZE 0
select attach_sub_folder from DC_Purge_Files_log where entity_type = 'Attachment' and delete_complete = 'N';
/
exit
EOF`
for counter in $feed
do
mkdir -p C:/Users/spatro/Desktop/attachment_backup/$counter
echo "Sub folder created "$counter
done
Rule number one of safe shellscripting: Always use quotes
Your loop would have executed once, had the command substitution (the stuff in backticks) and $feed variable been quoted. As it stands, those are asking for trouble! Specifically, word splitting and glob expansion.
If you would like some tooling:
Shellcheck would answer your question, and Naziquote would fix it (see this question).
If you want the for loop to execute just once, use a break statement before "done"
for counter in $feed
do
mkdir -p C:/Users/spatro/Desktop/attachment_backup/$counter
echo "Sub folder created "$counter
break
done
I have a shell script, when I execute it, I get the error
syntax error at line 34 : `<<' unmatched in ksh script
column_name=`sqlplus -s $BASE_DB_CONN<<!!
WHENEVER SQLERROR exit ROLLBACK
set SQLPROMPT ''
set heading off
set pagesize 1000
set linesize 5000
set feedback off
set define on
set verify off
#smm9_stream_map.sql $STREAM_NAME $FIELD_NAME"
exit
!!`;
To my surprise the same thing works in the while loop altered script as below
while [ $a -lt ${NO_LOOP} ]
do
column_name=`sqlplus -s $BASE_DB_CONN<<!!
WHENEVER SQLERROR exit ROLLBACK
set SQLPROMPT ''
set heading off
set pagesize 1000
set linesize 5000
set feedback off
set define on
set verify off
#smm9_stream_map.sql $STREAM_NAME $FIELD_NAME"
exit
!!`;
a=`expr $a + 1`
done
Why does the second script work but not the first one?
The heredoc terminator must be the only text on that line, no other whitespace allowed (*)
column_name=$(sqlplus -s $BASE_DB_CONN<<!!
....
exit
!!
)
(*) the exception being, for heredocs using <<-, any leading tabs will be stripped from the heredoc, including from the terminator. I see in my ksh93 man page:
If # is appended to <<, then leading spaces and tabs will be
stripped off the first line of the document and up to an equivalent indentation will be
stripped from the remaining lines and from word.