Quantcast
Channel: Oracle Bloggers
Viewing all articles
Browse latest Browse all 19780

Excel to sqlplus shell script

$
0
0

I wrote about copy&pasting Excel data into sqlplus running on tty before.

Copy&Paste from Excel to sqlplus. Multi-row multi-column data (INOUE Katsumi @ Tokyo)

with Excel 2007, if you copy multi-row and multi-column data from Excel,
the data is delimited by 'new-line' character and 'TAB' character.

I turned this into shell script finally. There may be no practical use but it was fun.
Here's bank statement data on Excel 2007 sheet which I want to run SELECT SQL on.


Here's one way to run. Please replace 'bashh' on top line with 'bash'. This blog server doesn't allow the 'bash' string here.

I copy&pasted from Excel to PuTTy terminal window after 2nd 'cat' line and
hit Ctrl-D.

$cat ./heredocCTE.sh
#!/bin/bashh
read col1 col2 col3 col4
(echo "
set pagesize 50000 linesize 256
column $col1 format a20
column $col2 format a20
column $col3 format a20
column $col4 format a20
with heredoc_rows as (
select regexp_substr('" \&&
cat \&&
echo "','[^
]+',1,level) h_row from dual connect by level < 16),
heredoc as (
select
regexp_substr(h_row, '[^        ]+',1,1) $col1,       --  TAB char, not space
regexp_substr(h_row, '[^        ]+',1,2) $col2,
regexp_substr(h_row, '[^        ]+',1,3) $col3,
regexp_substr(h_row, '[^        ]+',1,4) $col4
from heredoc_rows where h_row is not null)
select $1 from heredoc $2
/") |
sqlplus -S scott/tiger$cat |./heredocCTE.sh Description,Credit,Debit 'where  Credit>100000'
vDate   Description     Debit   Credit
2003/01/24      ATM withdrawal  19000
2003/01/26      ATM withdrawal  30000
2003/01/29      wire transfer   115000
2003/01/29      wire transfer   200000
2003/01/30      ATM withdrawal  19000
2003/02/03      wire transfer   10300
2003/02/07      wire transfer   3500
2003/02/08      wire transfer   4800
2003/02/17      ATM deposit     N/A     100000
2003/02/17      wire transfer   200000
2003/02/18      ATM deposit     N/A     300000
2003/02/20      wire transfer   111000

DESCRIPTION          CREDIT               DEBIT
-------------------- -------------------- --------------------
ATM deposit          300000               N/A

I'm trying to mimic Excel analytic functions now.


Viewing all articles
Browse latest Browse all 19780

Trending Articles