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

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

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:

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

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:

WHERE="Status='1'"
db2 select something from mytable where "$WHERE"

Hope this helps.