freeTDS bash:在Microsoft SQL Server中执行sql查询

我可以使用freeTSD和命令行通过Mint Linux VM连接到Microsoft SQL Server 2008实例,以在其上执行sql语句。 现在我想在bash脚本中自动化这个。 我能够成功login我的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文件时使用。 我跟着这个post: 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> 

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

有人能给我一步一步的回答我的问题?

Solutions Collecting From Web of "freeTDS bash:在Microsoft SQL Server中执行sql查询"

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

这是我的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 

老线程,但这似乎工作..

 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) 

尝试

  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,我推荐第二次试用。 如果你正在用sql语句分隔符发送命令给sql,也许第一个更好。 也许第三个……谁知道呢……只有反复试验才能说明…