The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to get a file name & record count of csv file sirik UNIX for Dummies Questions & Answers 2 03-06-2008 02:55 PM
How count number of fields in a record sureshg_sampat Shell Programming and Scripting 5 01-07-2008 06:30 AM
record count dr46014 Shell Programming and Scripting 4 12-11-2007 04:39 PM
splitting a record and adding a record to a file rsolap Shell Programming and Scripting 1 08-13-2007 01:58 PM
How to count the record count in an EBCDIC file. oracle8 UNIX for Dummies Questions & Answers 1 07-26-2006 07:22 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-09-2006
johnu122 johnu122 is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 2
Smile Need help with Isql record count

What I am trying to do is check if the database query returned any records.
If no records returned then output a message else output results to a file.
Right now if I take out the if and else statements the code runs fine and sends the email. If no records returned the email sends the column names. I would like to send a message instead of the column heading.
Here is my code can anyone help?

#!/bin/ksh
#cd /home/john/

. sybase
. ss_pto.id

echo "****** Please do not reply to this message ******" > ss_pto.status
echo " " >> ss_pto.status
echo New PTO and Special Requests ADDED as of: `date` >> ss_pto.status
echo " " >> ss_pto.status
echo Please add to Outlook calendars: >> ss_pto.status
#export today=`date`


date '+%m %d %Y %H %M %S' |
{
read MONTH DAY YEAR HOUR MINUTE SECOND
DAY=`expr "$DAY" - 1`
case "$DAY" in
0)
MONTH=`expr "$MONTH" - 1`
case "$MONTH" in
0)
MONTH=12
YEAR=`expr "$YEAR" - 1`
;;
esac
DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -1`
esac

}
tempdate="$YEAR"-"$MONTH"-"$DAY"" ""$HOUR":"$MINUTE":"$SECOND"


isql -U $U -P $P -S $S -w1000 -o tmp_isql.status << label1
use ptodb_rch01_prod
go
select RTRIM(usr.fname) "First name", convert(char(20), usr.lname) "Last name", convert(char(15), ind.pto_date, 107) "PTO Date", ind.comments "Comments"
from ss_pto_user usr, ss_pto_index ind
where
ind.user_id = usr.user_id and ind.request_date >= '$tempdate' and (ind.class_id = 7 and ind.section_id = 5 or ind.class_id = 2 and ind.section_id = 4 )
order by usr.lname asc

go
label1


reccnt=`wc -l tmp_isql.status`
if [reccnt = 0]
message="no records returned"
echo $message>>ss_pto.status
else
cat tmp_isql.status>>ss_pto.status

mailx -s"SS PTO" -senders email reciepents email < ss_pto.status
  #2 (permalink)  
Old 11-10-2006
shashikandi shashikandi is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 9
The reason is sql returns like this if there are no matching records.

column1 column2 column3 ...
------------------------------------------------------
(0 rows affected)

The reccnt=`wc -l tmp_isql.status` always returns more than 0.

May be you can try this.
recount='cat tmp_isql.status |awk 'NR >3' |grep -v - | grep -v return'

if [$recount != "(0 rows affected)"]
message="no records returned"
echo $message>>ss_pto.status
else
cat tmp_isql.status>>ss_pto.status

mailx -s"SS PTO" -senders email reciepents email < ss_pto.status

Here change ' NR>3' according to the number of lines for columns headings

Hope this piece of code is useful to you.
  #3 (permalink)  
Old 11-18-2006
johnu122 johnu122 is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 2
Quote:
Originally Posted by shashikandi
The reason is sql returns like this if there are no matching records.

column1 column2 column3 ...
------------------------------------------------------
(0 rows affected)

The reccnt=`wc -l tmp_isql.status` always returns more than 0.

May be you can try this.
recount='cat tmp_isql.status |awk 'NR >3' |grep -v - | grep -v return'

if [$recount != "(0 rows affected)"]
message="no records returned"
echo $message>>ss_pto.status
else
cat tmp_isql.status>>ss_pto.status

mailx -s"SS PTO" -senders email reciepents email < ss_pto.status

Here change ' NR>3' according to the number of lines for columns headings

Hope this piece of code is useful to you.
Thanks I got it to work just had one trouble and that was the variable $recount ended up having a carriage return in it so the compare kept failing. I ended up writing it to a file then using the file results. I am not sure if there is a better or easier way but here is what I did.

recount='cat tmp_isql.status |awk 'NR >1' |grep -v - | grep -v return'

echo $recount>temprecount
recount2=`cat temprecount`

if ["$recount2" = "(0 rows affected)"]
message="no records returned"
echo $message>>ss_pto.status
else
cat tmp_isql.status>>ss_pto.status
  #4 (permalink)  
Old 11-10-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
Code:
if grep -q '(0 rows affected)' tmp_isql.status
then
    echo "no records returned"" >>ss_pto.status
else
    cat tmp_isql.status>>ss_pto.status
fi

Jean-Pierre.
  #5 (permalink)  
Old 11-10-2006
justsam
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
isql has the -b option which strips off the header...

isql -U $U -P $P -S $S -w1000 -b -o tmp_isql.status
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0