Compare a content of variable to a database column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare a content of variable to a database column
# 1  
Old 02-23-2010
Compare a content of variable to a database column

Hi

have an array like this

colarray[0]="a"
colarray[1]="b"
colarray[2]="c"
colarray[3]="d"
colarray[4]="e"
colarray[5]="f"

the arrayvariable is in unix sh file

i want to check the content of the array to oracle database table.
that is whether "a" is present in the table.
# 2  
Old 02-23-2010
unix variables are available inside the interactive program.
you just need to do the database processing to achieve whatever you want.

Code:
sqlplus -s $user/$pass@$db << END
-- ${colarray[0]} can be used for comparison here --
END

# 3  
Old 02-23-2010
Oracle? mysql?
Do you want to know if a field in a table has the letter or any field?
# 4  
Old 02-23-2010
oracle database

its oracle database.
yes i want to check the table field contains the cahracter or may be a value.

thanks.
# 5  
Old 02-23-2010
If you want to check whether the Oracle database column has any of the values of your shell array, then here's a method.

These are the contents of my Oracle table -

Code:
test@XE> 
test@XE> set lin 80
test@XE> 
test@XE> desc t
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 X                                                  VARCHAR2(1)
 Y                                                  VARCHAR2(30)

test@XE> 
test@XE> select * from t;

X Y
- ------------------------------
a this row has "a"
b this row has "b"
c this row has "c"
d this row has "d"
e this row has "e"
f this row has "f"

6 rows selected.

test@XE> 
test@XE>

Here's the Bash shell script:

Code:
$ 
$ cat -n f8.sh
     1  #!/bin/bash
     2  colarray[0]="a"
     3  colarray[1]="b"
     4  colarray[2]="c"
     5  # I want to fetch all rows from the Oracle table "T"
     6  # that has values in column "x" equal to any element of "colarray"
     7  # i.e. => select * from t where x in ('a','b','c');
     8  sqlplus -s /nolog << EOF
     9  set feed 1
    10  connect test/test
    11  select * from t;
    12  select * from t where x in (`echo ${colarray[@]} | sed -e "s/ /', '/g" -e "s/^/'/" -e "s/$/'/"`);
    13  exit
    14  EOF
    15
$ 
$

And here's the script execution:

Code:
$ 
$ . f8.sh

X Y
- ------------------------------
a this row has "a"
b this row has "b"
c this row has "c"
d this row has "d"
e this row has "e"
f this row has "f"

6 rows selected.


X Y
- ------------------------------
a this row has "a"
b this row has "b"
c this row has "c"

3 rows selected.

$ 
$

HTH,
tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to compare 3rd column value with first column and display

Hello Team, My source data (INput) is like below EPIC1 router EPIC2 Targetdefinition Exp1 Expres rtr1 Router SQL SrcQual Exp1 Expres rtr1 Router EPIC1 Targetdefinition My output like SQL SrcQual Exp1 Expres Exp1 Expres rtr1 Router rtr1 Router EPIC1 Targetdefinition... (5 Replies)
Discussion started by: sekhar.lsb
5 Replies

2. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

3. Shell Programming and Scripting

Help with compare 2 column content and corrected/replaced word

Input File CGGCGCCTCGCNNNCGAGCG CGGCGCGCCGAATCCGTGCG TCGCNGC GCGCCGC ACGGCNNNNN ACGGCCTCGCG CGGCNGCCCGCCC CGGCGCGCCGTCC Desired Output File CGGCGCCTCGCNNNCGAGCG CGGCGCGCCGAATCCGTGCG CGGCGCCTCGCATCCGAGCG TCGCNGC GCGCCGC TCGCCGC ACGGCNNNNN ACGGCCTCGCG ACGGCTCGCG... (6 Replies)
Discussion started by: perl_beginner
6 Replies

4. UNIX for Dummies Questions & Answers

Compare data - Match first column and compare second

Hi guys, looking for some help with a way to compare data in two files but with some conditions. example, File 1 consists of site1,10.1.1.1 site2,20.2.2.2 site3,30.3.3.3 File 2 contains site1,l0.1.1.1 site2,50.1.1.1 site3,30.3.3.3 site4,40.1.1.1 I want to be able to match the... (1 Reply)
Discussion started by: mutley2202
1 Replies

5. Shell Programming and Scripting

Pass column number as variable to awk and compare with a string.

Hi All, I have a file test.txt. Content of test.txt : 1 vinay se 2 kumar sse 4 kishore tl I am extracting the content of file with below command. awk '$2 ~ "vinay" {print $0}' test.txt Now instead of hardcoding $2 is there any way pass $2 as variable and compare with a... (7 Replies)
Discussion started by: Girish19
7 Replies

6. Shell Programming and Scripting

Problem in sending mail with database content

Hi All, I want to fetch records from Oracle DB table and send it in a mail to a set of users. i.e, I have a query which returns a set of records. I want to send mail with below content: Hi , PFB the details: <first database record> <Second database record> ……………. ………………..... (3 Replies)
Discussion started by: anil029
3 Replies

7. Shell Programming and Scripting

Take values from a column and put it in a variable and compare

Hi, I have a table in unix from which i want to read the contents line by line, then filter out the values from 6th column one by one and compare it a fixed value. How to do this? (7 Replies)
Discussion started by: arijitsaha
7 Replies

8. Shell Programming and Scripting

awk arrays - compare value in second column to variable

Hello, I am trying to redirect files to a directory by using a config file. The config files is as such: xxxxxx,ID,PathToDirectory xxxxxx,ID2,PathToDirectory2 and so on... I have a variable that should match one of these IDs. I want to load this config file into an awk array, and... (2 Replies)
Discussion started by: jrfiol
2 Replies

9. Shell Programming and Scripting

Change file content 4 column to one Column using script

Hi Gurus, I have file content sample: ,5113955056,,TAgent-Suspend ,5119418233,,TAgent-Suspend ,5102119078,,TAgent-Suspend filenames 120229H5_suspend, 120229H6_unsuspend I receive those files one of directory /home/temp/ I need following: 1. Backup first /home/temp/ file to... (5 Replies)
Discussion started by: thepurple
5 Replies

10. UNIX for Dummies Questions & Answers

Compare the content of a variable with a string

Hello all: I'm new in Unix and here and I'am spanish so my english isn't so good to explain my doubt. Here it is. Very urgent: I need to compare the value of a variable with a string. Example is this. Imagine that the variable x1 contains the path and a file text and I need to compare... (2 Replies)
Discussion started by: robdai
2 Replies
Login or Register to Ask a Question