运行多个sqlcmd时sqlcmd不会关闭bash脚本中的连接

时间:2019-06-03 10:06:14

标签: sql-server batch-file sqlcmd

在运行以下查询时,我能够获得第一个查询结果,但不会继续进行第二个查询。似乎连接一旦完成就不会停止。

我能够对.sql文件中的查询运行类似的查询,并以1对1的方式运行它们。我在运行所有7合1 bash脚本时遇到问题。

QUERY1 = SELECT * FROM SOMETABLE
QUERY2 = SELECT * FROM SOMETABLE
QUERY3 = SELECT * FROM SOMETABLE
QUERY4 = SELECT * FROM SOMETABLE
QUERY5 = SELECT * FROM SOMETABLE
QUERY6 = SELECT * FROM SOMETABLE
QUERY7 = SELECT * FROM SOMETABLE

sqlcmd -S $HOST -U $USER -P $PASSWORD -d $DATABASENAME -s $QUERY1 > 
QueryRun1.csv
sqlcmd -S $HOST -U $USER -P $PASSWORD -d $DATABASENAME -s $QUERY2 > QueryRun2.csv
sqlcmd -S $HOST -U $USER -P $PASSWORD -d $DATABASENAME -s $QUERY3 > QueryRun3.csv
sqlcmd -S $HOST -U $USER -P $PASSWORD -d $DATABASENAME -s $QUERY4 > QueryRun4.csv
sqlcmd -S $HOST -U $USER -P $PASSWORD -d $DATABASENAME -s $QUERY5 > QueryRun5.csv
sqlcmd -S $HOST -U $USER -P $PASSWORD -d $DATABASENAME -s $QUERY6 > QueryRun6.csv
sqlcmd -S $HOST -U $USER -P $PASSWORD -d $DATABASENAME -s $QUERY7 > QueryRun7.csv

此bash脚本无法获取第二查询结果,因此。

1 个答案:

答案 0 :(得分:1)

我不记得为什么,我是在几天前写的,但是我记得>sqlcmd遇到问题。我的下面语法比较好运:

output=$(/opt/mssql-tools/bin/sqlcmd -U $Login -P $Password -i "$file")
echo "$output" > $log

在您的情况下,这将意味着:

QUERY1="SELECT * FROM SOMETABLE"
QUERY2="SELECT * FROM SOMETABLE"
QUERY3="SELECT * FROM SOMETABLE"
QUERY4="SELECT * FROM SOMETABLE"
QUERY5="SELECT * FROM SOMETABLE"
QUERY6="SELECT * FROM SOMETABLE"
QUERY7="SELECT * FROM SOMETABLE"

output=$(sqlcmd -S $HOST -U $USER -P $PASSWORD -d $DATABASENAME -Q "$QUERY1")
echo "$output" > QueryRun1.csv
output=$(sqlcmd -S $HOST -U $USER -P $PASSWORD -d $DATABASENAME -Q "$QUERY2")
echo "$output" > QueryRun2.csv
output=$(sqlcmd -S $HOST -U $USER -P $PASSWORD -d $DATABASENAME -Q "$QUERY3")
echo "$output" > QueryRun3.csv
output=$(sqlcmd -S $HOST -U $USER -P $PASSWORD -d $DATABASENAME -Q "$QUERY4")
echo "$output" > QueryRun4.csv
output=$(sqlcmd -S $HOST -U $USER -P $PASSWORD -d $DATABASENAME -Q "$QUERY5")
echo "$output" > QueryRun5.csv
output=$(sqlcmd -S $HOST -U $USER -P $PASSWORD -d $DATABASENAME -Q "$QUERY6")
echo "$output" > QueryRun6.csv
output=$(sqlcmd -S $HOST -U $USER -P $PASSWORD -d $DATABASENAME -Q "$QUERY7")
echo "$output" > QueryRun7.csv