Getting variable from a query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting variable from a query
# 1  
Old 03-21-2013
Getting variable from a query

Hi,

I need to get a variable HMAX_TBL_ID and then use it in second select query. The first select statement returns few leading white spaces for ex : 12345
so when I run the script HMAX_TBL_ID does not return the "12345" , it returns blank.
Please let me know what I am missing.


Code:
HMAX_TBL_ID=`psql -U ${PG_USER} -h ${PG_HOST} -d ${PG_DB} -p ${PG_PASS} -t -c "select stp_ctr_fetch_module_tbl_id('ATTRIB', 'GET')"` | sed -e 's/^[ \t]*//'

        echo -e "This is max table id in ${HMAX_TBL_ID} "

        if [ $? -ne 0 ]
        then
                echo -e " Error while getting the maximun table id processed"
        fi


  psql -U ${PG_USER} -h ${PG_HOST} -d ${PG_DB} -p ${PG_PASS} -t -c "select distinct tbl_id from tbl_ctr_module_tbl where tbl_id between ${LAST_PROCESSED_TABLEID} and ${HMAX_TBL_ID}" | sed '/^$/d' > ${TBL_ID_FILE}

# 2  
Old 03-21-2013
Stating what database and shell you are using would help, but since that info is not here...

Here's a couple of ways. First, try putting put a TRIM( ) function call (or your system's equivalent) around
Code:
stp_ctr_fetch_module_tbl_id('ATTRIB', 'GET')

Or in the script try one of these:

Code:
$ cat x
a="  1234"
print "Before: [$a]"
typeset -i a   # make it an integer.
print "After: [$a]"

b="  1234"
print "Before: [$b]"
b=$(print $b)
print "After: [$b]"


$ ./x
Before: [  1234]
After: [1234]
Before: [  1234]
After: [1234]
$


Last edited by gary_w; 03-21-2013 at 05:23 PM..
This User Gave Thanks to gary_w For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass variable to a query?

Hi All, How to pass date variable to a query? I have tried the below one , but it's not working. ost.ksh #!/bin/ksh v_date=$1 var=$(sqlplus -s $ORACON <<ENDOFSQL SELECT TO_DATE('$v_date','DD-MON-YYYY'),-1) FROM DUAL; exit; ENDOFSQL ) #End I have executed as below. (7 Replies)
Discussion started by: ROCK_PLSQL
7 Replies

2. Shell Programming and Scripting

Query on fetching a value of variable in script

I have below 3 files in a directory : oktest.txt okcode.txt okfun.txt I was writing a small shell script to just print the names of three files where I took a variable a and assigned it a value ok. a=ok echo "Three files are : $atest.txt $acode.txt $afun.txt" But when I run... (3 Replies)
Discussion started by: simpltyansh
3 Replies

3. Shell Programming and Scripting

Formating of query variable in perl

Hi , I am facing error in perl when I assign a below query in a varibale $query because of new line charchters $query= SELECT XYZ , ABC , c2 , c3 , c4 FROM t1 how can i get rid of new line charchters with out changing the... (2 Replies)
Discussion started by: gvk25
2 Replies

4. Shell Programming and Scripting

How to use a variable in insert query?

My script contains as follows, VALUE=`sqlplus un/pwd <<EOF > OB.txt set pagesize 0 feedback off verify off heading off echo off select max(1) from table1; exit; EOF` insert into table2 values(1, 'The max value is $value',...); i need the value of VALUE to be inserted after 'The max... (2 Replies)
Discussion started by: savithavijay
2 Replies

5. UNIX for Dummies Questions & Answers

how to - redirect query results to a variable

How can I send the results of a query to a unix variable. I basically want to run a query then do some logic on the results. Trying to redirect the result into a variable I define in the script. select count(*) as counter from table - nut to redirect the "count" returned from the query... (2 Replies)
Discussion started by: rstone
2 Replies

6. Shell Programming and Scripting

add the output of a query to a variable to be used in another query

I would like to use the result of a query in another query. How do I redirect/add the output to another variable? $result = odbc_exec($connect, $query); while ($row = odbc_fetch_array($result)) { echo $row,"\n"; } odbc_close($connect); ?> This will output hostnames: host1... (0 Replies)
Discussion started by: hazno
0 Replies

7. UNIX for Dummies Questions & Answers

Redirect Query o/p to variable

Hi, I wanted to o/p the number of rows in a table to a variable in linux. How can i achieve this. I wrote the query and its settings like feedback, pagesize line size in a file and using this file as a parameter to the sqlplus command. now can i redirect the o/p of that query to a variable.... (2 Replies)
Discussion started by: Swapna173
2 Replies

8. Shell Programming and Scripting

script variable within a sql query

I created a script to read a csv file with four columns. The script also saved values of each col in a arry. In the script, i connected to db try to run a query to pull out data baisc on the values from the csv file. select Num from tableName where Sec_Num in ('${isin}') /*isin is an arry... (1 Reply)
Discussion started by: Sherry_Run
1 Replies

9. Shell Programming and Scripting

storing database query in a variable

I am executing a SQL query in Unix Server: select status,location from user_information where request_id='DS-43720' In shell script I am writing this query in the below manner: echo "select status,location from user_information where request_id='DS-43720' ;" >> $directory/info.sql ... (3 Replies)
Discussion started by: debu
3 Replies

10. Shell Programming and Scripting

sql query variable not exactly unix

I know htis isnt exactly unix.... but hopefully someone can help me or direct me someplace to get help. I can run sql queries in scripts against my informix db using: dbaccess mydb myquery.sql >> sql.output I need to write my script to select based on todays date. Its very... (5 Replies)
Discussion started by: MizzGail
5 Replies
Login or Register to Ask a Question