How to use SQL Hard Code Conditions in UNIX Shell Script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use SQL Hard Code Conditions in UNIX Shell Script?
# 1  
Old 03-22-2016
How to use SQL Hard Code Conditions in UNIX Shell Script?

Hi All,

I am trying to place one SQL query in Shell Script with Where Condition as
Code:
Status='1'

But after running the the script it is returning error as

SQL0206N "1" is not valid in the context where it is used. SQLSTATE=42703

The query is working fine in Data Base.

Please suggest on this.

Thanks

Last edited by vbe; 03-22-2016 at 06:17 AM.. Reason: code/icode tags
# 2  
Old 03-29-2016
This looks like you use DB2, so I will use examples for this database.

When a shell interprets commands, it consumes quotes to keep words together as a single token. The quotes will not be passed along to called programs. So the following two lines are identical:
Code:
db2 select something from mytable where status='1'
db2 select something from mytable where status=1

If you want the single quotes preserved and passed to the db2 command, you have to protect them with double quotes or escape them with backslashes, like
Code:
db2 select something from mytable where "status='1'"
db2 "select something from mytable where status='1'"
db2 select something from mytable where status=\'1\'

Now, if you want the where clause to come from a variable, use this form:
Code:
WHERE="Status='1'"
db2 select something from mytable where "$WHERE"

Hope this helps.
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. Shell Programming and Scripting

run sql queries from UNIX shell script.

How can i run sql queries from UNIX shell script and retrieve data into text docs of UNIX? :confused: (1 Reply)
Discussion started by: 24ajay
1 Replies

3. Shell Programming and Scripting

How can i run sql queries from UNIX shell script and retrieve data into text docs of UNIX?

Please share the doc asap as very urgently required. (1 Reply)
Discussion started by: 24ajay
1 Replies

4. Shell Programming and Scripting

Issue with SQL UNIX shell script -

Hi all I am writing a shell script which will run a select query from a table . If the ouput is not 500 - then send a email to the team saying there is a error . But i am not sure how to redirect this output of the select query to the log file - #!/bin/ksh sqlplus /nolog conect... (1 Reply)
Discussion started by: honey26
1 Replies

5. Shell Programming and Scripting

Check SQL through UNIX shell script

I use below command to log in to oracle database sqlplus username/passwordWhen i enter into sql prompt i press quitIf there are any errors it will show just below the output of sqlplus username/password. The errors generally start with keyword ORA can we do this by using a unix shell script.?... (4 Replies)
Discussion started by: Nakul_sh
4 Replies

6. UNIX for Dummies Questions & Answers

How to convert R$Timestamp in Sql*Plus within a UNIX Shell Script?

I need to compare a R$Timestamp field sql within a Unix Shell Script. In straight SQL the following code works fine: Table Name: LL_UNIT_TRANSACTION UT Field: R$Timestamp Where TRUNC(UT.R$Timestamp) >= TRUNC(SYSDATE -7) the following returns no data within the Unix Shell Script... (2 Replies)
Discussion started by: Dapconsult
2 Replies

7. UNIX for Advanced & Expert Users

Use of Oracle pl/sql in UNIX shell script

Hi, I have basic knowledge on how to write pl/sql code inside shell script. I am looking for more advance thing. Is there any book for that which can just talk about how to write more advance plsql code inside shell script. Please help Thanks!!!!!! (1 Reply)
Discussion started by: diehard
1 Replies

8. UNIX for Dummies Questions & Answers

executing SQL query using unix shell script

I want to perform few post-session success tasks like update a status to 'true' in one of the sql database table, update date values to current system date in one of the configuration table in sql. How do i achieve this in a post session command?syntax with example will be helpful. (3 Replies)
Discussion started by: nathanvaithi
3 Replies

9. 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

10. UNIX for Dummies Questions & Answers

sql query results in unix shell script

Hi I want to get the a field from a SQL query into unix shell script variable. the whole situation is like this. 1. Opened a cursor to a table in DB2 databse. 2. Fetching individual rows with the help of cursor. 3. Each row has 4 fields. I want each of the field in individual shell... (1 Reply)
Discussion started by: skyineyes
1 Replies
Login or Register to Ask a Question