With sqlplus command what is the use of col VALUE for A70
Found in many scripts but could not find the description.
It's the width of the column. A70 means 70 characters
Related
I have a .txt file containing ~1 million rows and 11 columns. Only the first column has alphabetical values while the rest 2-11 columns have numeric values. An example given below:
Image of the Input Table
I used datamash to group column 1 by taking the mean of each value in col 2 to col 11 columnwise. So the result must look like this
Output Table
I tried this code in bash script:
datamash --sort --whitespace --headers groupby 1 mean 2-11 < infilename.txt > outfilename.txt
It is giving me this error:
'atamash: invalid numeric value in line 2 field 11: '3.510344247
I have checked multiple times that there is no single quotation in my data at line 2 field 11..I even remove all the special characters and only kept the alphanumeric values and whitespace as delimiter, then again ran the same command and getting the same error. It is only targeting the column 11 becoz when I run the above command from 2-10 it is giving me the correct result.
What is it that i am doing wrong? Any help would be appreciated.
I searched for the same error in the internet and got the solution that the error 'atamash: invalid numeric value in is becoz i have to convert the dos .txt file into unix file. So using dos2unix command i converted the file the file and run the above code. Its worked! So i am posting this comment for future consideration.
I am using unix shell KSH scripting to do some table cleanup.
I have a file "partner.txt" with 5000 line like this
>cat partner.txt
aaa0000
aaa0001
aaa0002
...
...
aaa5000
Using this file, I am supposed to clean few tables with matching, say agreements of the partners.
So i am constructing a partner list string in the format that i can use in the sql statement with 'IN' clause (select * from tab where partner IN partner_list)
('aaa0000',
'aaa0001','aaa0002',...,'aaa0010',
'aaa0011','aaa0012',...,'aaa0020',
...
'aaa4990','aaa4991',...,'aaa5000')
I am assigning the string to partner_list variable like this.
export BO="("
export BC=")"
export BQ="('"
export QC="','"
export QB="')"
export C=","
export CE=","'\n'
export QCE="',"'\n'"'"
partnerListLine=${BO}
while read partnerline;
do
if [ `expr ${counter} % 10` -eq 0 ]
then
partnerListLine=${partnerListLine}${partnerline}${CE}
elif [ ${counter} -lt ${numOfObsoletePartner} ]
then
partnerListLine=${partnerListLine}${partnerline}${C}
fi
counter=`expr ${counter} + 1`
done < partner.txt
partnerListLine=${partnerListLine}${partnerline}${BC}
Then I am using this partner list to fetch my agreement list like
SQL_agreement='select distinct a.agreement from partner_agreement_map a where a.partner in ${partner_list} order by agreement asc;'
I needed the newline character in my partner list since i was using sqlplus and was encountering SP2-0027: Input is too long (> 2499 characters)
I am adding the newline character by appending the below to my partner list string after N partners
CE=","'\n'
This worked fine when i was using sqlplus directly in the script.
But when i try to pass this partner_list string as parameter to a sql script, it shows '\n' in the query.
This is how i call my sql script and pass the parameter
sqlplus -s ${REFERENCE_DB_USER}/${REFERENCE_DB_PASS}#${DATABASE_INSTANCE} << !!
set serveroutput on size 10000;
set feedback off;
set verify off;
set echo off;
set term off;
set pagesize 0;
SET linesize 1000;
SET TRIMSPOOL ON;
spool 1_del_agreement_spool_$$.lst;
#1_del_agreement.sql ${partner_list};
spool off;
exit;
/
!!
this is my spooled file
>cat 1_del_agreement_spool_18165.lst <
select distinct a.agreement from partner_agreement_map a where a.partner in ('aaa0000',\n'aaa0001','aaa0002','aaa0003',...'aaa0010',\n'aaa0011'...) order by agreement asc
*
ERROR at line 1:
ORA-00907: missing right parenthesis
How can i maintain the newline character when i pass the parameter to the sql script and not have it replaced to '\n'?
I have tried ANSI-C quoting but failed.
Please let me know if you would need more details of the shell or sql script
UPDATED MY ENTIRE SOLUTION DESIGN
After trying all night, i have given up.
Thanks Aaron and mplf for your inputs.
I have decided to change my solution from file based to table based. I will be reading the partner.txt file and inserting the partners in a dummy temporary table. Then I can formulate queries with ease on other tables.
In fact, i think this should have been my first design :) There may be something very minor that i was missing in the previous design. But anyways, this will be much easier
I wish my team lead ever reviews design rather than code formatting issues :P
If you are using bash you can use $'\n' to print a newline character which would make your example
if [ `expr ${counter} % 10` -eq 0 ]
then
partnerListLine=${partnerListLine}${partnerline}"',"$'\n'"'"
else
...
Example:
$ echo hello"',"$'\n'"'"
hello',
'
I don't have a working solution but a hint: '\n' means "insert the literal backslash followed by n". So you tell the shell to leave this string alone.
Try NL=$(echo -e '\n') or similar to get a string variable which actually contains a newline. Then you can define CE=",$NL"
The shell might preserve this new line character as it processes the string.
Or use a tool like awk to create a string value with newlines which you assign to partner_list with partner_list=$(awk ...) to prevent the shell from doing any kind of processing of the value.
If that doesn't work, you may have to write the data to a file (with new lines).
I need to write a simple shell function that returns a single field from an Oracle DB. Think of it as for example SELECT 'ABC' FROM dual; and ABC is what I am after.
Here is my function:
function getSomeOraVal
{
sqlplus $USER/$PASSWD#$ORADB<<!!
SET sqlprompt ''
SET sqlnumber off
SET verify off
SET pages 0
SET echo off
SET head on
SET feedback off
SET feed off
SET serveroutput on
SET escape '\'
VARIABLE v_someVal VARCHAR2(30);
BEGIN
SELECT 'ABC' INTO v_someVal FROM dual;
END;
/
SELECT :v_someVal FROM dual;
!!
}
However, I want to pipe the sqlplus output (data only -> 'ABC') into a shell variable, which the function then returns and can be called from other shell scripts. Unfortunately, sqlplus returns a whole lot of garbage, such as "Welcome to Oracle" on top and "Disconnected..." in the bottom.
How can I extract just the data from a SQL result set, or in this case a single value and pass it into a UNIX variable for further manipulation within the shell?
Thanks
There are a few different approaches in this askTom thread on returning values from SQL*Plus to a shell script.
One common approach is to select a constant token in addition to the value that you want to return (in Tom's example, that is the string "KEEP") and then use sed (or your favorite command-line parser) to extract the data you're actually interested in
#!/bin/ksh
x=`sqlplus / <<endl | grep KEEP | sed 's/KEEP//;s/[ ]//g'
select 'KEEP' , max(sal) from emp;
exit
endl`
echo the answer is $x
Other approaches, such as approaches that allow you to read multiple lines of output are also discussed in that thread.
If you don't want the header to be printed, you should be specifying
set head off
in your SQL*Plus script-- I'm not sure why you're explicitly setting the header on in the script if you don't want the header... Do you want to keep some part of the header?
Based on Justin's answer (which is great), when you only need to select one number (or token), I consider this a little shorter, yet more readable version:
x=`sqlplus -S / <<< "select 'KEEP' , max(sal) from emp;" | awk '/KEEP/{print $2}'`
I am trying to export some data using sqlplus and the Oracle spool functionality. The problem is that the SQL input file where I am defining my export is not letting me parameterize the table name from which I am exporting data -- it wants a literal :(
Calling sqlplus in a shell script:
sqlplus $USER/$PASSWD#$ORADB<<!
#export.sql $OUT_FILE $SOME_VAR $ENV
exit
!
export.sql:
set heading off
set head off
set term off
set tab off
set embedded on
set feedback off
set pagesize 0
set linesize 800
set trimspool on
set verify off
spool &1
SELECT '&2|' || some_col
FROM &3_TABLE
/
spool off
When $ENV is set to 'dev', I get
Enter value for 3_TABLE
whereas I want it to use dev_TABLE. When I unparameterize the table names in the sql file, the output runs fine. Also note that there is param &2, which is $SOME_VAR from the shell and it gets displayed evaluated fine. The only problem is in the FROM statement.
Is there any way to tell the sql input file to replace the parameterized table names before running SQL?
Thanks
The problem is that SQL*Plus is treating the whole string after the &, up to the next whitespace or simlar, as the substitution variable name. Clearly that isn't what you want here.
Fortunately they've thought of this, and you can denote the end of the variable name with a .:
FROM &3._TABLE
(At least, that works for named variables, and I'm almost sure it will for positional ones... if not then you'd need to define a new variable set to &3 as a workaround).
It is in the documentation, but blink and you'll miss it:
If you wish to append characters immediately after a substitution
variable, use a period to separate the variable from the character.
There's a related effect that you may want to bear in mind for the future. If the next character after the substitution variable is a . anyway - between the schema and table, or between table and column, for example - then that will be interpreted as the substitution terminator. Say you were passing the schema separately as &4, with value 'scott'; this:
FROM &4.&3._TABLE
looks reasonable but would be substituted as scottdev_TABLE, which won't be recognised. So in that instance you need to have an extra one:
FROM &4..&3._TABLE
which would be substituted as scott.dev_TABLE.
This is my script.
#!/usr/bin/sh
isql -UXx -Pxxxxxx <<!
set nocount on
use xxxx
go
select count(*) from BSC where bsc='$1'
go
!
exit
i am executing this script as :
temp2.sh 0000
the output is 0.
but when i execute the query manually then the output is 1 which is correct.
problem here is the command line argument $1 is not passed to the query.
how could i achieve this?
I have tried all these possiblities:
bsc='$1'- output is 0
bsc="$1"- output is 0
bsc=`$1`- Syntax error
bsc="'$1'"- output is 0
I am using solaris unix and DB is sybase.
The problem here, I think, is that the single quotes in your query are being interpreted by the shell, so that
select count(*) from BSC where bsc='$1'
is being interpreted as
select count(*) from BSC where bsc=$1
as a literal string.
I don't have anything available to me to test this with, but you might try replacing that like with
select count(*) from BSC where bsc="'$1'"
I think (and I stress again that I don't have anything available to test with right now) that the double quotes should make the single quotes print out as characters, so it will be interpreted as
select count(*) from BSC where bsc='0000'
given the command line input you provided.
Problem is solved:
instead of calling the script as
temp2.sh 0000
i called it as :
temp2.sh "0000"