Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

query results parsed in variables

Status
Not open for further replies.

cybercop23

IS-IT--Management
Aug 12, 2001
103
US
Hi all. How can I get the result of an Informix select query into a set of variables that I can use within my script? Here's what I have:
....
UPC1=00"$UPC"
upc=`echo "$UPC1" | cut -c 1-13`

dbaccess itemmaster << stp
select upmdesc, upmsize
from upm
where upmdesc = "$upc";
stp
....
Since this only returns one row, I want the two fields from the query to be parsed into variables that I can later use in my script. Somethig like:
item_description should be upmdesc
item_size should be upmsize

Any ideas?

Thanks
Alex
 
Assuming you're using a bourne shell derivative.
Code:
dbaccess itemmaster << stp | read item_description item_size
   select upmdesc, upmsize
   from upm
   where upmdesc = "$upc";
stp
 
Tried that and the variables are null. I've added the following print statements:
echo query done
echo $item_description
echo $item_size
And the result is:
1 row(s) retrieved.

query done


Note that there's nothing printed for $item_description and $item_size

Any ideas why not?
AB
 
what is the output of this?

Code:
dbaccess itemmaster <<stp
   select upmdesc, upmsize
   from upm
   where upmdesc = "$upc";
stp

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
blank like
blank like
upmdesc upmsize
testitem 1 lb
blank like
1 row(s) retrieved.
blank like
blank like

can it be because of the blank lines? how do i get rid of them?
 
Code:
dbaccess itemmaster <<stp | sed -n '3p' | read item_description item_size
   select upmdesc, upmsize
   from upm
   where upmdesc = "$upc";
stp

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Almost there. Now I get the column headings put into the variables.
query done
upmdesc
upmsize
 
got it.
dbaccess itemmaster <<stp | sed -n '3p' | read item_description item_size
output to pipe cat without headings
select upmdesc, upmsize
from upm
where upmdesc = "$upc";
stp

Thanks for the help.
 
Why not using the UNLOAD instruction instead of a SELECT ?
unl=/tmp/upm.$$
echo "UNLOAD TO '$unl' DELIMITER '|'
SELECT upmdesc,upmsize FROM upm WHERE upmdesc='$upc'
" | dbaccess itemmaster - 2>&1
IFS='|' read item_description item_size junk < $unl

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top