469,286 Members | 2,522 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

shell script to extract rows from table

Hi.

I am new to Unix. I need to write a Unix Shell Script to extract records from the table and write those extracted datas to a text file.

DB using is DB2.

Can anyone give me some sample shell script

thanks
Krishnakanth
Sep 30 '08 #1
5 12983
Is there any specific reason that you want a shell script. You can do a export of the table data using EXPORT command.
Oct 1 '08 #2
sakumar9
127 Expert 100+
You can call EXPORT command from a shell script, EXPORT will write the table data into files.
Oct 1 '08 #3
docdiesel
297 Expert 100+
Hi,

Expand|Select|Wrap|Line Numbers
  1. db2 "select a,b,c from schema.mytable" >file.txt
should do, if you don't need the data in CSV format. Use "db2 -x" if you don't want the column heads.

Regards

Doc Diesel
Oct 1 '08 #4
Thanks for your reply. Now I have got another issue.
_________________________
echo "Run SQL select current timestamp from SYSIBM.SYSDUMMY1 with ur" >>$monlog
db2 "select current timestamp from SYSIBM.SYSDUMMY1 with ur" >> currenttimestamp
cat currenttimestamp
_________________________

The output what I am getting is as follows:

1
---------------------------------------
2008-10-03-02-00.00.00.000000

1 row(s) selected
=================================

I want only the timestamp to be displayed. which I need to store the timestamp to a variable called currtimestamp.

and I need to pass this currtimestamp to another query as follows.
___________________
db2 "SELECT * FROM BMWDEV1.ZB_RCBL_ERROR WHERE TIMESTAMP_UPDATED >= '2008-10-01-00.00.00.000000' AND TIMESTAMP_UPDATED > currtimestamp WITH UR" >>$monlog
___________________

Can anyone help me out to fix this issue.

Thanks in advance.

Krishnakanth
Oct 3 '08 #5
docdiesel
297 Expert 100+
Hi,

either you use "db2 -x SELECT ..." to suppress the header & footer, or you grep for the leading digit "2" (of 2008...):
Expand|Select|Wrap|Line Numbers
  1. db2 "SELECT ... " | grep ^2 >file.txt
Instead of that you may store the date in a shell variable and use the latter in the next step:
Expand|Select|Wrap|Line Numbers
  1. DTSTAMP=` db2 -x "select ..." `
  2. db2 "select * ... where WHERE TIMESTAMP_UPDATED > '$DTSTAMP' "
  3.  
I guess you need the timestamp of the beginning of the script for more than one action. I'd suggest to create an auxiliary table {var varchar(20), value varchar(128)}, for script internal values:
Expand|Select|Wrap|Line Numbers
  1. insert into my.variables (var,value)
  2.   SELECT 'tstamp', Current_timestamp
  3.   from sysibm.sysdummy1 ;
  4.  
  5. SELECT * from ...
  6. WHERE TIMESTAMP_UPDATED>(
  7.   timestamp(select value from my.variables where var='tstamp')
  8. ) ;
  9.  
Regards

Doc Diesel
Oct 4 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Don Crossman | last post: by
4 posts views Thread by devi thapa | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.