如何将存储在变量中的字符串从bash脚本传递给sybase SQL IN子句

时间:2015-03-08 04:55:00

标签: bash

我有一个说明empId的数组,它们是typr String,现在我想在bash的SQL子句中传递它。

我曾在下面尝试过

#make an array of empIds

empIdsarray=(e123 e456 e675 e897)
for j in "${empIdsarray[@]}"
do
   inclause=\"$j\",$inclause
done
#remove the trailing comma below
inclause=`echo $inclause|sed 's/,$//'
`isql -U$user -P$pwd -D$db -S$server <<< QRY > tempRS.txt
    select * from emp where empId IN ($inclause)
go
quit
go
QRY`

我曾尝试过IN('$ inclause'),但没有任何工作,输出是空白的,虽然我直接在DB中运行它给出了结果。 任何帮助表示赞赏。

#it should execute like
select * from emp where empId IN ("e123", "e456", "e675", "e897")

提前感谢。

1 个答案:

答案 0 :(得分:0)

在BASH,你可以这样做:

#!/bin/bash
#make an array of empIds
empIdsarray=(e123 e456 e675 e897)
printf -v inclause '"%s",' "${empIdsarray[@]}"

isql -U$user -P$pwd -D$db -S$server <<QRY > tempRS.txt
select * from emp where empId IN (${inclause%,})
go
quit
go
QRY