6

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

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

然后我有我的 SQL 查询:

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

这是我的 bash 脚本示例:

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 脚本的输出是:

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> 

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

有人可以逐步回答我的问题吗?

4

3 回答 3

5

我不确定你的样本是如何工作的。

这是我的 bash 脚本示例:

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 的“这里文档”。

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
于 2012-11-14T21:45:52.503 回答
3

旧线程,但这似乎有效..

printf  "use mydbname\ngo\nselect * from mytable\ngo\nexit\n"|tsql -I freetds.conf -S profileName -U user -P 'password'

1> 2> 1> 2> ID  stringtest  integertest
1   test    50
2   teststring2 60
3   test3   70
(3 rows affected)
于 2015-12-14T21:22:07.813 回答
0

尝试

 echo "USE dbname\n GO\n delete from schema.tableA where ID > 5\n GO\n delete from schema.tableB userid > 5\n go\n delete from schema.tableC where ID > 5\n GO\n exit\n" 

该字符串的其余部分可能有效

并尝试

 echo "USE dbname;\n delete from schema.tableA where ID > 5;\n delete from schema.tableB userid > 5;\n delete from schema.tableC where ID > 5;\n exit\n" 

并尝试

 echo "USE dbname; delete from schema.tableA where ID > 5; delete from schema.tableB userid > 5; delete from schema.tableC where ID > 5; exit" 

如果您使用的是 odbc,我建议您进行第二次试用。如果您使用“go”字作为 sql 句子分隔符向 sql 发送命令,也许第一个更好。也许是第三个……谁知道……只有反复试验才能告诉我们……

于 2012-10-29T15:50:06.500 回答