freeTDS bash:在Microsoft SQL Server中执行sql查询 [英] freeTDS bash: Executing sql queries in Microsoft SQL server

查看:75
本文介绍了freeTDS bash:在Microsoft SQL Server中执行sql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用freeTSD和命令行通过Mint Linux VM连接到Microsoft SQL Server 2008实例,以在其上执行sql语句.现在,我想在bash脚本中自动执行此操作.我能够成功登录bash脚本:

I am able to connect to a Microsoft SQL Server 2008 instance via a Mint Linux VM using freeTSD and command line to execute sql statements on it. Now I want automate this in a bash script. I am able to successfully login in my bash script:

TDSVER=8.0 tsql -H servername -p 1433 -D dbadmin -U domain\\Administrator -P password

然后我有我的SQL查询:

I then have my SQL query:

USE dbname GO delete from schema.tableA where ID > 5 GO delete from schema.tableB where ID > 5 GO delete from schema.tableC where ID > 5 GO exit

这在通过freeTSD命令行手动执行时有效,但是在我放入bash文件时无效.我关注了这篇文章: freeTSD&bash .

This works when doing manually via freeTSD command line, but not when I put in bash file. I followed this post: freeTSD & bash.

这是我的bash脚本示例:

Here is my bash script sample:

echo "USE dbname GO delete from schema.tableA where userid > 5 go delete from schema.tableB where userid > 5 go delete from schema.tableC where ID > 5 GO exit" > tempfile | TDSVER=8.0 tsql -H servername -p 1433 -D dbname -U domain\\Administrator -P password < tempfile

bash脚本的输出为:

the output of the bash script is:

locale is "en_US.UTF-8"
locale charset is "UTF-8"
Default database being set to sbdb
1> 2> 3> 4> 5> 6> 7> 8> 

然后执行我的脚本的其余部分.

and then the rest of my script is executed.

有人可以逐步解决我的问题吗?

Can someone give me a step by step answer to my problem ?

推荐答案

我不确定您的示例如何工作.

I'm not sure how your sample can work at all.

这是我的bash脚本示例:

Here is my bash script sample:

echo "USE dbname .... exit" > tempfile | TDSVER=8.0 tsql -H servername -p 1433 -D dbname -U domain\\Administrator -P password < tempfile
# ------------------------------------^^^^ ---- pipe char?

尝试使用';'字符.

echo "USE dbname .... exit" > tempfile ; TDSVER=8.0 tsql -H servername -p 1433 -D dbname -U domain\\Administrator -P password < tempfile
# ------------------------------------^^^^ ---- semi-colon

更好的是,使用shell的此处文档".

Better yet, use shell's "here documents".

TDSVER=8.0 tsql -H servername -p 1433 -D dbname -U domain\\Administrator -P password <<EOS
     USE dbname 
     GO 
     delete from schema.tableA where userid > 5 
     go 
     delete from schema.tableB where userid > 5 
     go 
     delete from schema.tableC where ID > 5 
     GO 
     exit
  EOS

IHTH.

当前命令行输入:

echo "delete from table where userid > 5
go
delete from table where userid > 5
go
delete from table where ID > 5
GO
exit" < /tmp/tempfile; TDSDUMP=/tmp/freetds.log TDSVER=8.0 tsql -H servername -p 1433 -D dbname -U Administrator -P password <<EOS

这篇关于freeTDS bash:在Microsoft SQL Server中执行sql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆