LIKE in SQL shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting LIKE in SQL shell script
# 1  
Old 06-11-2010
LIKE in SQL shell script

Code:
sqlplus -s <<__END__
${USER}/${PASS}@${DB}
WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK
WHENEVER OSERROR EXIT FAILURE ROLLBACK
SET ECHO ON
SET TIMING ON
SET SERVEROUTPUT ON SIZE 1000000
select count(*) from table where start LIKE '10-JUN-10';
EXIT
__END__
result=$?
echo $result

This gives me always 0. When I run from the GUI tool for count 13.
When I change the query to select count(*) from table I will get the correct result
please tell me how to change the SQL code so that it gives me correct result.

Last edited by pludi; 06-11-2010 at 06:01 AM..
# 2  
Old 06-11-2010
How is the column start defined (desc <table_name>)?
# 3  
Old 06-11-2010
select count(*) from table order by start desc
It gives out the count.

Some problem I am facing when usng LIKE in SQL statement used in Shell script
# 4  
Old 06-11-2010
Could you please post the table structure? You can obtain the details with the describe command:

Code:
desc <table_name>

It's important, because, if it's of date type, you don't need the like operator in this case:

Code:
select count(*) from table where trunc(start) = to_date('10-JUN-10', 'dd-mon-yy');

Consider that you will need a FBI on trunc(start) in this case, otherwise probably a full table scan will be performed).

It should be better to use something like this to avoid the above mentioned problem:

Code:
select count(*) from table where start 
between to_date('09-JUN-10', 'dd-mon-yy')
and to_date('11-JUN-10', 'dd-mon-yy');

# 5  
Old 06-11-2010
Quote:
Originally Posted by sandy1028
select count(*) from table order by start desc
It gives out the count.
Are you sure that the SELECT statement works ?
Apparently, start is reserved word and cannot be used as a column name in an Oracle table.

Code:
test@ORA11G>
test@ORA11G> create table t ( start varchar2(10) );
create table t ( start varchar2(10) )
                 *
ERROR at line 1:
ORA-00904: : invalid identifier

test@ORA11G>

The only way to make it work (that I can think of) is to put start within double quotes in the CREATE TABLE statement.
But then, you'll have to use double quotes in your SELECT statement as well.

tyler_durden
# 6  
Old 06-11-2010
Well,
I suppose that the words table and start are used only in the example and are not the real table/column names.
# 7  
Old 06-11-2010
In that case, I guess the OP incorrectly believes that the shell variable "result" holds the record count of the table. It does not.

The value of "result" will be 0 if -
(i) the "sqlplus" command was run successfully, which means that the ORACLE_HOME was set, and
(ii) all commands in the sqlplus session ran successfully

That the record count of the table in the sqlplus session is <n>, is a different matter altogether. The value <n> will not be assigned to "result".

The OP's SELECT statement works, probably because the column (start, or whatever) is a VARCHAR2 and also, the LIKE operator works with a literal string as well.

Code:
test@ORA11G>
test@ORA11G> desc t
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 X                                                  VARCHAR2(10)

test@ORA11G>
test@ORA11G>
test@ORA11G>
test@ORA11G> select * from t;
X
----------
10-JUN-10
11-JUN-10
10-JUN-10

3 rows selected.

test@ORA11G>
test@ORA11G> select count(*) from t where x like '10-JUN-10';

  COUNT(*)
----------
         2

1 row selected.

test@ORA11G>
test@ORA11G>

Since the query works and sqlplus works in my environment, the shell script would run successfully as well, but the value of "result" will not be 2.

Code:
$
$ cat f3.sh
sqlplus -s test/test <<__END__
WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK
WHENEVER OSERROR EXIT FAILURE ROLLBACK
SET ECHO ON
SET TIMING ON
SET SERVEROUTPUT ON SIZE 1000000
select count(*) from t where x LIKE '10-JUN-10';
EXIT
__END__
result=$?
echo "Result = $result"
$
$
$ . f3.sh
  COUNT(*)
----------
         2
1 row selected.
Elapsed: 00:00:00.00
Result = 0
$
$

But if there's something wrong with the SELECT statement, then the error code will be non-zero:

Code:
$
$ # Note that column xy does not exist in table t
$ cat f3.sh
sqlplus -s test/test <<__END__
WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK
WHENEVER OSERROR EXIT FAILURE ROLLBACK
SET ECHO ON
SET TIMING ON
SET SERVEROUTPUT ON SIZE 1000000
select count(*) from t where xy LIKE '10-JUN-10';
EXIT
__END__
result=$?
echo "Result = $result"
$
$ . f3.sh
select count(*) from t where xy LIKE '10-JUN-10'
                             *
ERROR at line 1:
ORA-00904: "XY": invalid identifier

Result = 136
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass Oracle sql script as argument to UNIX shell script?

Hi all, $ echo $SHELL /bin/bash Requirement - How to pass oracle sql script as argument to unix shell script? $ ./output.sh users.sql Below are the shell scripts and the oracle sql file in the same folder. Shell Script $ cat output.sh #!/bin/bash .... (7 Replies)
Discussion started by: a1_win
7 Replies

2. UNIX for Dummies Questions & Answers

Help with SQL Shell script

Hi, I need to excute two .sql scripts sequentially within file.sh sqlplus user/pass@db1 @/opt/Infor/Outbound_Marketing/7.2.2/custom/iadeploy1.sql THEN sqlplus user/pass@db2 @/opt/Infor/Outbound_Marketing/7.2.2/custom/iadeploy3.sql Thanks, Soph (4 Replies)
Discussion started by: soph1997
4 Replies

3. Shell Programming and Scripting

Run SQL thru shell script: how to get a new line when run sql query?

Hi, this's Pom. I'm quite a new one for shell script but I have to do sql on shell script to query some information from database. I found a concern to get a new line...When I run my script, it retrieves all data as wondering but it's shown in one line :( What should I do? I'm not sure that... (2 Replies)
Discussion started by: Kapom
2 Replies

4. Shell Programming and Scripting

pass null value to sql script from korn shell script

There are 4 parameters that I have to pass from korn shell to sql script. 1) I have to check if $1 , $2 , $3 and $4 are null values or not . How can I do that ? 2) Once its determined that these values are null (in the sense they are empty) how can I pass null values to sql script... (11 Replies)
Discussion started by: megha2525
11 Replies

5. Shell Programming and Scripting

How to grep sql error in shell script and exit the script?

I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts. Here is my script #!/bin/csh -f source .orapass set user = $USER set pass = $PASS cd /opt/data/scripts echo... (2 Replies)
Discussion started by: allinshell99
2 Replies

6. Shell Programming and Scripting

(Urgent):Creating flat file using sql script and sqlplus from UNIX Shell Script

Hi, I need help urgently for following issue. Pls help me to resolve this issue. I am calling sql script file(file1.sql) from UNIX Shell Script(script1.ksh) using sql plus and trying to create flat file that contains all records returned from SQL query in SQL script(file1.sql) I given... (6 Replies)
Discussion started by: praka
6 Replies

7. Shell Programming and Scripting

Calling SQL LDR and SQL plus scripts in a shell script

Hi- I am trying to achieve the following in a script so I can schedule it on a cron job. I am fairly new to the unix environment... I have written a shell script that reads a flat file and loads the data into an Oracle table (Table1) via SQLLDR. This Works fine. Then, I run a nested insert... (5 Replies)
Discussion started by: rajagavini
5 Replies

8. Shell Programming and Scripting

Shell Script - SQL

Guys,Please look at a simple shell which validates presence of a customer table in OASIS schema. SQL Query for validation is wrong(Table name in quotes should not have schema name OASIS.). And shell is working correctly (with exit 1) as it will never get count=1 with this query. But it is... (2 Replies)
Discussion started by: bhush782003
2 Replies

9. Shell Programming and Scripting

SQL in shell script

Hi, I am able to insert the values into a database table from a flat file using SQL loader. Now can I do the same using only shell script.Can this be done without using sql loader and only with shell scripting(ie use of awk). Please put some light on this. Thanks in advance (3 Replies)
Discussion started by: sendhil
3 Replies

10. Shell Programming and Scripting

Shell script and sql

Hi, I have an issue. There is a file that has got some values seperated by commas. I have to write a shell script that would extrct these values and insert them into a table. How can this be done Thanx in advance (3 Replies)
Discussion started by: sendhil
3 Replies
Login or Register to Ask a Question