【问题标题】:how do i pass in a command to sqsh and get the output to a file in one go?如何将命令传递给 sqsh 并一次性将输出输出到文件?
【发布时间】:2014-02-21 17:54:06
【问题描述】:

我正在尝试设置一个简单的循环来定期查询 bash 中的数据库表。通常我似乎必须这样做:

sqsh -s SERV -U user -P passwd -D db -L bcp_colsep=','

然后在 sqsh 中我必须输入:

select * from some_table where foo=bar
\go -m bcp > /path/to/output.out

我试图使用 sqsh 的-C 选项来传递这样的命令:

sqsh -s SERV -U user -P passwd -D db -L bcp_colsep=',' -C 'select * from some_table where foo=bar \go -m bcp > /path/to/output.out'

但我不断得到:

Incorrect syntax near '\'.

怎样才能得到想要的效果?

【问题讨论】:

    标签: sql-server database bcp sqsh


    【解决方案1】:

    当您使用 -C 选项将 SQL 语句传递给 sqsh 时,将隐式执行 \go 命令。要获得 'bcp' 结果样式的输出,您需要使用 -L 参数设置变量 'style=bcp' 或使用 -mbcp 作为命令行参数并将输出重定向到文件,或使用 sqsh -o 参数指定输出的文件名。所以基本上你的命令看起来像:

    sqsh -S SERV -U user -P passwd -D db -L bcp_colsep=',' -m bcp \
    -C 'select * from some_table where foo=bar' > /path/to/output.out
    

    HTH,马丁

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 2020-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多