如何在 tsql 中运行 SQL 脚本 [英] How to run a SQL script in tsql

查看:34
本文介绍了如何在 tsql 中运行 SQL 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 tsql(与 FreeTDS 一起安装),我想知道是否有办法从命令行在 tsql 中运行 SQL 脚本并在文本文件中获取结果.

I'm using tsql (installed with FreeTDS) and I'm wondering if there's a way to run a SQL script in tsql from the command line and get the result in a text file.

例如,在 psql 中我可以这样做:

For example, in psql I can do:

psql -U username -C "COPY 'SELECT * FROM some_table' to 'out.csv' with csv header"

或者:

psql -U username -C "\i script.sql"

在 script.sql 中做:

And in script.sql do:

\o out.csv
SELECT * FROM  some_table;

有没有办法在 tsql 中做到这一点?我已经阅读了 linux 手册页并到处搜索,但我只是找不到方法.

Is there a way for doing this in tsql? I have read the linux man page and search everywhere but I just don't find a way.

推荐答案

我真的没有找到如何做到这一点,我开始认为它不能在 tsql 中完成.但是,我解决了重定向标准输入和标准输出的特定问题.我使用这样的 bash 脚本:

I really didn't find how to do this and I'm starting to thinks it just can't be done in tsql. However I solved for my specific problem redirecting the stdin and stdout. I use a bash script like this:

tsql connection parameters < tsql_input.sql > tsql_output.csv
lines=`cat tsql_output.csv | wc -l`

# The output includes the header info of tsql and the "(n number of rows)" message
# so I use a combination of head and tail to select the lines I need
headvar=$((lines-2))
tailvar=$((lines-8))

head -$headvar tsql_output.csv | tail -$tailvar tsql_output.csv > new_output_file.csv

这只会将查询结果保存在new_output_file.csv"上

And that saves just the query result on 'new_output_file.csv'

这篇关于如何在 tsql 中运行 SQL 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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