Query Vertica through Shell script

Moderator: NorbertKrupa

Post Reply
sach
Newbie
Newbie
Posts: 3
Joined: Sun Jul 01, 2012 5:31 am

Query Vertica through Shell script

Post by sach » Sun Jul 01, 2012 5:34 am

Hi,

Is there any sample example to connect to Vertica and execute sql's through shell script ?.I tried with option \i <file.sql>, but the vertica ignores the arguments...

Thanks in Advance,
Sach

User avatar
nnani
Master
Master
Posts: 302
Joined: Fri Apr 13, 2012 6:28 am
Contact:

Re: Query Vertica through Shell script

Post by nnani » Sun Jul 01, 2012 11:12 am

Sach,

If you are trying to connect Vertica and then query vertica through script,
try doing this:


SQL File - vertica_query.sql ( with no unix commands )

1. Connect Vertica
2. \i vertica_query.sql
3. You can see the results there itself

Through Shell Script
shell script file - vertica_query.sh( use "vsql -c" command)
run the shell script there itself without connecting vertica.
nnani........
Long way to go

You can check out my blogs at vertica-howto

sach
Newbie
Newbie
Posts: 3
Joined: Sun Jul 01, 2012 5:31 am

Re: Query Vertica through Shell script

Post by sach » Mon Jul 02, 2012 7:20 am

Hi,

I was able to connect to vertica as below

vsql -U ${VERTICA_USR} -d ${VERTICA_DB} -c "<sql query>' -At -o <output_file> -F ',' -q.

The above resulted the output without header and comma separated

Thanks
Sach

banurajadurai
Newbie
Newbie
Posts: 21
Joined: Tue Aug 27, 2013 10:31 am

Re: Query Vertica through Shell script

Post by banurajadurai » Fri Aug 30, 2013 6:01 am

i tried using \i command but i got an error as below

dbadmin=> \i vmart_queries_explain.sql
vmart_queries_explain.sql: No such file or directory

the sql file is in this path /opt/vertica/examples/VMart_Schema

what is to be done ?

id10t
GURU
GURU
Posts: 732
Joined: Mon Apr 16, 2012 2:44 pm

Re: Query Vertica through Shell script

Post by id10t » Fri Aug 30, 2013 6:37 am

Hi!

>> what is to be done ?

Code: Select all

dbadmin=> \i /opt/vertica/examples/VMart_Schema/vmart_queries_explain.sql

User avatar
nnani
Master
Master
Posts: 302
Joined: Fri Apr 13, 2012 6:28 am
Contact:

Re: Query Vertica through Shell script

Post by nnani » Fri Aug 30, 2013 7:35 am

Hello Sach

An example for you on using vsql through shell scripts

Code: Select all

#!/bin/bash
tab_nm='test'
echo $tab_nm
var=`vsql -At -h hostname -d databasename -U username -w xxxx -c "select count(column_name)from columns where upper(table_name)='$t
ab_nm' group by table_name;"`
not_null_count=`vsql -At -h hostname -d databasename -U username -w xxxx  -c "select count(is_nullable) from columns where upper(tab
le_name)='$tab_nm' and is_nullable='f'"
echo $var
echo $not_null_count
If you want to run this file directly then just do it through bash terminal
If you want to run it through vsql then , vsql has to be in the path of this script
check your current path in vsql like this

Code: Select all

nnani=> \set CUR_PATH `pwd`
nnani=>\!echo :CUR_PATH

Or you can try this

nnani=>\! pwd
Place your script in this path and use \I option with script name to run it.

Hope this helps.. :)
nnani........
Long way to go

You can check out my blogs at vertica-howto

Post Reply

Return to “Vertica SQL”