How to execute a no of SELECT COUNT(*) statements using a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to execute a no of SELECT COUNT(*) statements using a loop
# 1  
Old 09-19-2010
How to execute a no of SELECT COUNT(*) statements using a loop

HI Unix Gurus,

I have a number of SELECT count(*) statements in an input file and I want to execute it using a shell script but one by one using loop in script....
How can I do this.....
# 2  
Old 09-19-2010
Code:
cat input.file |sh

# 3  
Old 09-19-2010
Hi Ustechie,
Can you please post how your file looks like.
We have very few information.
If it's written in SQL then I'd do:
Code:
while true; do
    mysql < input.file
done

# 4  
Old 09-19-2010
My requirement is....

I can have a number of Select count(*) statements in an input file....it may be only 1 more more than 1, number of statements is not fixed...


So I want to use a loop thru which i can check the count of 1 statement and if it is greater than 100,000 then i will stop processing....this is requirement

I can grep the number of SQLs present in input file but I want to run 1 sql at a time instead of running them all...
# 5  
Old 09-19-2010
You're really NOT making it easy by not copying an example of what your file could look like! What is your problem with that? If I understand well, the Select count(*) statements are buried in the middle of other statements. Right? Do they always occupy a full line? Can they be split between several lines? Do they have the trailing semicolon?

What if there are 150,000 statements? Do you want to process the first 100,000 statements? or nothing at all?

Why do you want to use a loop?
Code:
while read -r; do
    mysql -e "$REPLY"
done < input.file

is exactly the same thing as
Code:
mysql < input.file

If the idea is to process the first 100,000 statements, and considering one statement = one line, I would just do the following:
Code:
egrep '^Select count(*)' input.file | head -100000 | mysql

If you want more help, provide more information.
This User Gave Thanks to chebarbudo For This Post:
# 6  
Old 09-19-2010
I am sorry but cannot paste my code here due to some compliance issues...hope you understand.....


to make it simple, i'll give you an example.....

input file would contain...

Select count(*) from employee where dept=10;
Select count(*) from employee where dept=20;
Select count(*) from employee where dept=30;
Select count(*) from employee where dept=40;
Select count(*) from employee where dept=50;
Select count(*) from employee where dept=60;
Select count(*) from employee where dept=70;
Select count(*) from employee where dept=80;
Select count(*) from employee where dept=90;


Number of SQLs may vary from 1 to any number.....here i took it randomly 9 SQLs.....

Now I want to execute this input file thru a unix script but I want to execute first SQL first and if the result of first SQL is greater than 100,000 then stop processing...
# 7  
Old 09-19-2010
That's better. Of course I was not expecting you to paste the full or real code. But this template helps a lot!
In this case, I think that's what you want to do:
Code:
limit=100000
while read -r; do
    [[ $(mysql -Ne "$REPLY") -gt $limit ]] && break
done < input.file

Be careful, if one of the statement outputs more than one line, an error will occur. For example if you insert another field or a GROUP BY.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

One Line Command how to use pipe statements to execute and comment on multiple possible outcomes

Hello Forum, I'm looking to expand the following command: INACTIVE_KERNELS=$(python -mplatform | grep -qi red && rpm -qa | grep '^kernel-' |grep -vE `uname -r` | paste -sd \; || echo "Not Red Hat Server") Currently this command will check if my server is RedHat server using the grep -qi... (6 Replies)
Discussion started by: greavette
6 Replies

2. Shell Programming and Scripting

For loop statements order of operations

Say I have a for loop that parse through a file....Say it look for the colors red and blue in sections of the file. Say it find red before it find blue in the file. Say I have two if statements in the for loop Something like if blue is found print blue is my favorite color is the first if... (7 Replies)
Discussion started by: scj2012
7 Replies

3. Shell Programming and Scripting

Count lines with awk if statements

Hi Everybody, I wanna count lines in many files, but only if they meet a condition, I have something like this, cat /path1/usr/STAT/GPRS/ESTCOL_GPRS_2016* | awk 'BEGIN{FS=",";}{ if (substr($5,1,8)=='$DATE'){a++} END{for(i in a)print a}}' DATE=$(date +%Y%m%d -d "1 day ago") But it has... (6 Replies)
Discussion started by: Elly
6 Replies

4. Shell Programming and Scripting

2 statements in for loop

Bash shell, variables i and rem are working fine in 2 separate for loops, but I'd like to consolidate them like this: for && This gives syntax error on &&. Thanks in advance for direction. (5 Replies)
Discussion started by: p1ne
5 Replies

5. Shell Programming and Scripting

Combine 4 awk pattern count statements into 1

Hello fellow awkers, I am trying to combine the following awk statements into 1 so that the results are more accurate: awk '/\=\+/ { count++ } END { print count}' filename awk '/\=\?/ { count++ } END { print count}' filename awk '/\=\-/ { count++ } END { print count}' filename awk... (8 Replies)
Discussion started by: ux4me
8 Replies

6. Shell Programming and Scripting

execute multiple statements in if-else

how can we execute multiple statements in else condition i have if then statement else statements fi in else condition i have multiple statements but it executing only one statement is there any way to execute multiple statements (4 Replies)
Discussion started by: nani1984
4 Replies

7. Shell Programming and Scripting

for each value in an array, execute select statement

Hello All, I am new to shell scripting. I am working on Solaris O/S, bash script and sybase programming. I want to loop through multiple values in an array and for each value, I want to select a row from the database. following is the code written for it. output="loop.csv" ... (8 Replies)
Discussion started by: arundhati_s
8 Replies

8. Shell Programming and Scripting

How do I execute multiple statements within If then else

Please help me. I have been doing this for several hours. Here is the code if then echo a b c d >> file.txt echo 1111 >> file.txt fi The reason I want the two echo is because I want these statements printed on multiple lines. I keep getting error . First it tells me... (2 Replies)
Discussion started by: asemota
2 Replies

9. Shell Programming and Scripting

is that possible to keep statements in any loop??

Hi, Actually i stored all validdisks in one array and corresponding partitions required for all individual disks in other array.. Example: Validdisks=dsk2 dsk3 dsk5 ValidPartition=4 4 3 Now i have to create domain.. Domain creation can be done by below commands: fs_setup -d... (1 Reply)
Discussion started by: mansa
1 Replies

10. Shell Programming and Scripting

for i loop with conditional statements?

New to scripting in general, so patience plz. If I ask a stupid question or don't get it, I thank you for your kindness in advance. That said, did a for i loops checks to see if a PB* file is there but I need to know two things before I copy the file. I need to know if the file's create date... (2 Replies)
Discussion started by: xgringo
2 Replies
Login or Register to Ask a Question