select count(*) into a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting select count(*) into a variable
# 1  
Old 09-24-2003
select count(*) into a variable

Hi,
Could anybody help me how can I assign the value of "select count(*) from table1" to a variable in an unix shell script. Thanks.
# 2  
Old 09-24-2003
Hi,

Please show us a little example scripture.
Don't know exact what you want

input= `shell statement `

for i in $input
do
....

done

Is a handout.


Regs David
# 3  
Old 09-24-2003
You can pass back the value from a PL/SQL block by setting a variable equal to the count from your table. You then exit your block with the variable. Search the boards for PL/SQL and you will find a few examples.

If you want a quick way to do this, after you log into your database and before you execute your Select statement, spool a file. After you close your session you can then have your shell script parse the spool file for the count. Also note that there are a number of options in Oracle (assuming your using it) to turn off headings and such so your spool file is a bit more clean.

Last edited by google; 09-24-2003 at 02:27 PM..
# 4  
Old 09-25-2003
#!/bin/sh
COUNT=`sqlplus -s system/manager <<ABC
set head off;
select count(*) from table1;
exit;
ABC`

Last edited by sssow; 09-25-2003 at 11:28 AM..
# 5  
Old 09-25-2003
Quote:
Originally posted by sssow
#!/bin/sh
COUNT=`sqlplus -s system/manager <<ABC
set head off;
select count(*) from table1;
exit;
ABC`
That script works, but it assumes that he is using Oracle. Anyway, this type of assignment should work for most databases. You just replace the "sqlplus" line with whatever database command line tool you use. For example, in mysql, it would be something like 'mysql -Ddbname -u username -ppassword'. The concept is the same, thought.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning value of a select count(*) from tablename ( run on db2 ) to a unix variable

Hi All, I have browsed through the forums for a similar topic, but there is no topic which explains about this problem, when the backend is DB2. I want to assign the output of a select count(*) from tablename to a unix variable in a shell script. I am using ksh. The database used to... (3 Replies)
Discussion started by: Siddarth
3 Replies

2. Shell Programming and Scripting

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..... (7 Replies)
Discussion started by: ustechie
7 Replies

3. UNIX for Dummies Questions & Answers

How to set a variable with a count variable i.e. VARIABLE$COUNT

Hi All I've very nearly finished this script I'm working on but have hit another idiots problem, after googling I can't see a solution for this one. I have a while count loop that checks checks two consecutive values then increments the count by two. What the script has to do is then check... (5 Replies)
Discussion started by: Bashingaway
5 Replies

4. Shell Programming and Scripting

Select variable within a if statement

i want to select a variable created and use it in a if statement, but not getting the desired results LINE='device for 0101a01: lpd://172.25.41.111:515' prt=`echo $LINE | awk '{print $3 }' | cut -c 1-7` echo $prt My if statement to select just what i want.. IFS=$":" while read prt... (11 Replies)
Discussion started by: ggoliath
11 Replies

5. UNIX for Dummies Questions & Answers

using unix variable in select column in awk

Hi, I have file on below pattern, and i want to write a generic awk to handle all cases. input_file: col1~col2~col3~col4~col5~col6 I need to generate 4 files something like this File1: col1~~col2~~col3 File2: col1~~col2~~col4 File3: col1~~col2~~col5 File4: col1~~col2~~col6 (1 Reply)
Discussion started by: luckybalaji
1 Replies

6. Shell Programming and Scripting

How can i assign an select statement into a variable?

I am trying to assign an select statement into a variable. Can someone hel me with this. example : a='select * from dual' echo $a should give me select * from dual But this is not working. I trying with \ before * and quotes too. (1 Reply)
Discussion started by: rdhanek
1 Replies

7. Shell Programming and Scripting

Redirect Postgresql Select Query to Variable

How to redirect postgresql select query to a variable using shell scripts? I tried to use psql -c "select ip from servers" db | egrep '+\.+\+\+' | sed 's/^ *\(.*\) *$/\1/' Output: 10.10.10.180 10.10.10.181 It display ip addresses from the databasem, but how could i redirect the... (3 Replies)
Discussion started by: uativan
3 Replies

8. Shell Programming and Scripting

Using a variable to select records with awk

As part of a bigger task, I had to read thru a file and separate records into various batches based on a field. Specifically, separate records based on the value in the batch field as defined below. The batch field left-justified numbers. The datafile is here > cat infile 12345 1 John Smith ... (5 Replies)
Discussion started by: joeyg
5 Replies

9. UNIX for Dummies Questions & Answers

select count(*) in sqlplus into variable unix shell

Need to select count(*) from table to check for zero result in unix script (2 Replies)
Discussion started by: struggle
2 Replies
Login or Register to Ask a Question