db2 query in shell script doesn't run with empty results in shell

扶醉桌前 提交于 2019-12-11 20:47:37

问题


I have below script in shell. test.sh

#! /bin/bash

connect_stat=$(db2 -x "connect to $DB_NAME USER $DB_USER using $DB_PASSWORD" )
db2 "SET SCHEMA=SCHEMA1"

while read line; 
do
a=$(db2 -x "SELECT C.id FROM table C WHERE C.col1  IN ('$line)') with ur")
echo $a
done<inputs.txt 

I get empty results when I run "sh test.sh"

Where as when I run the same above query in db2 through putty, I get results.

I have below dbcon file. Will trigger the dbcon file in putty and results are available

/admin/.profile

db2 connect to DB_NAME USER DB_USER using DB_PASSWORD

db2 set SCHEMA=SCHEMA1

db2


db2 => SELECT C.id FROM table C WHERE C.col1  IN ('xyz-asd-asd') with ur

ID
----------------------------------------------------------------
123

  1 record(s) selected.

Please help me what I am doing wrong in shell script.

Thanks in advance.


回答1:


$(db2 -x "connect to $DB_NAME...) executes in a subshell, which terminates the connection when the subshell exits, so by the time you get to db2 "SELECT C.PARTNUMBER..." the connection does not exist.



来源:https://stackoverflow.com/questions/30787459/db2-query-in-shell-script-doesnt-run-with-empty-results-in-shell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!