简体   繁体   English

SQL Server:将查询导出为.txt文件

[英]SQL Server : export query as a .txt file

I am trying to export my SQL Server query results into a folder in .txt format (this is for an automated job) 我试图将我的SQL Server查询结果导出为.txt格式的文件夹(这是一个自动化作业)

I know the equivalent in MySQL works with INTO OUTFILE . 我知道MySQL中的等价物与INTO OUTFILE Does anyone know the best way to do this in SQL Server 2008 Management Studio? 有谁知道在SQL Server 2008 Management Studio中执行此操作的最佳方法?

SELECT DISTINCT RTRIM (s1.SGMNTID) AS 'AccCode',RTRIM (s1.DSCRIPTN) AS 'CodeDesc', CASE
    WHEN  s1.SGMTNUMB = '1' THEN '1' 
    WHEN s1.SGMTNUMB = '2' THEN '2'
    WHEN s1.SGMTNUMB = '3' THEN '110'
    WHEN s1.SGMTNUMB = '4' THEN '4'
    WHEN s1.SGMTNUMB = '5' THEN '120'
    END AS 'AccountType_id',
CASE WHEN s1.SGMTNUMB = '2' 
THEN LEFT(s1.SGMNTID, 2)
ELSE 'DEFAULT'
END AS 'AccGroupName'

 FROM GL40200 s1

UNION 

SELECT  REPLACE ([ACTNUMBR_1]+'-'+ [ACTNUMBR_2]+'-'+ [ACTNUMBR_3]+'-'+[ACTNUMBR_4]+'-'+    [ACTNUMBR_5],' ', '') AS 'AccCode',
 '' AS 'CodeDesc',
 '0' AS 'AccountType_id',
 'Default' AS 'AccGroupName'
FROM GL00100 a

INTO OUTFILE 'C:\Users\srahmani\verian/myfilename.txt'

you do this in the SSMS app, not the SQL. 你在SSMS应用程序中执行此操作,而不是SQL。 In the toolbar select 在工具栏中选择

Query --> Results To --> Results To File 查询 - >结果到 - >结果到文件

Another way is from command line, using the osql: 另一种方法是从命令行,使用osql:

OSQL -S SERVERNAME -E -i thequeryfile.sql -o youroutputfile.txt

This can be used from a BAT file and shceduled by a windows user to authenticated. 这可以从BAT文件中使用,并由Windows用户进行验证。

You can use bcp utility . 您可以使用bcp实用程序

To copy the result set from a Transact-SQL statement to a data file, use the queryout option. 要将结果集从Transact-SQL语句复制到数据文件,请使用queryout选项。 The following example copies the result of a query into the Contacts.txt data file. 以下示例将查询结果复制到Contacts.txt数据文件中。 The example assumes that you are using Windows Authentication and have a trusted connection to the server instance on which you are running the bcp command. 该示例假定您使用的是Windows身份验证,并且与运行bcp命令的服务器实例具有可信连接。 At the Windows command prompt, enter: 在Windows命令提示符下,输入:

bcp "<your query here>" queryout Contacts.txt -c -T

You can use BCP by directly calling as operating sytstem command in SQL Agent job. 您可以通过在SQL代理作业中直接调用as operating sytstem命令来使用BCP。

You can use windows Powershell to execute a query and output it to a text file 您可以使用Windows Powershell执行查询并将其输出到文本文件

Invoke-Sqlcmd -Query "Select * from database" -ServerInstance "Servername\\SQL2008" -Database "DbName" > c:\\Users\\outputFileName.txt Invoke-Sqlcmd -Query“Select * from database”-ServerInstance“Servername \\ SQL2008”-Database“DbName”> c:\\ Users \\ outputFileName.txt

The BCP Utility can also be used in the form of a .bat file, but be cautious of escape sequences (ie quotes "" must be used in conjunction with ) and the appropriate tags. BCP实用程序也可以以.bat文件的形式使用,但要小心转义序列(即引号“”必须与之结合使用)和相应的标记。

.bat Example: .bat示例:

C:
bcp "\"YOUR_SERVER\".dbo.Proc" queryout C:\FilePath.txt -T -c -q
-- Add PAUSE here if you'd like to see the completed batch

-q MUST be used in the presence of quotations within the query itself. -q必须在查询本身存在引号时使用。

BCP can also run Stored Procedures if necessary. 如有必要,BCP还可以运行存储过程。 Again, be cautious: Temporary Tables must be created prior to execution or else you should consider using Table Variables. 同样,要小心:必须在执行之前创建临时表,否则您应该考虑使用表变量。

This is quite simple to do and the answer is available in other queries. 这很简单,答案可以在其他查​​询中找到。 For those of you who are viewing this: 对于那些正在查看此内容的人:

select entries from my_entries where id='42' INTO OUTFILE 'bishwas.txt';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM