简体   繁体   English

使用查询分析器代码从SQL Server 2000导出到Excel

[英]Export to excel from SQL Server 2000 using Query Analyzer code

What's the easiest way to export data to excel from SQL Server 2000. 从SQL Server 2000将数据导出到Excel的最简单方法是什么?

I want to do this from commands I can type into query analyzer. 我想从我可以输入查询分析器的命令中执行此操作。

I want the column names to appear in row 1. 我希望列名出现在第1行。

In Query Analyzer, go to the Tools -> Options menu. 在查询分析器中,转到“工具” - >“选项”菜单。 On the Results tab, choose to send your output to a CSV file and select the "Print column headers" option. 在“结果”选项卡上,选择将输出发送到CSV文件,然后选择“打印列标题”选项。 The CSV will open in Excel and you can then save it as a .XLS/.XLSX CSV将在Excel中打开,然后您可以将其另存为.XLS / .XLSX

替代文字

Manual copy and paste is the only way to do exactly what you're asking. 手动复制和粘贴是完全按照您的要求进行操作的唯一方法。 Query Analyzer can include the column names when you copy the results, but I think you may have to enable that somewhere in the options first (it's been a while since I used it). 查询分析器可以在复制结果时包含列名,但我认为您可能必须首先在选项中的某个位置启用它(自从我使用它以来已经有一段时间了)。

Other alternatives are: 其他替代方案是:

  1. Write your own script or program to convert a result set into a .CSV or .XLS file 编写自己的脚本或程序,将结果集转换为.CSV或.XLS文件
  2. Use a DTS package to export to Excel 使用DTS包导出到Excel
  3. Use bcp.exe (but it doesn't include column names, so you have to kludge it) 使用bcp.exe(但它不包括列名,所以你必须kludge它)
  4. Use a linked server to a blank Excel sheet and INSERT the data 将链接服务器用于空白Excel工作表并插入数据

Generally speaking, you cannot export data from MSSQL to a flat file using pure TSQL, because TSQL cannot manipulate anything outside the database (using a linked server is sort of cheating). 一般来说,您无法使用纯TSQL将数据从MSSQL导出到平面文件,因为TSQL无法操纵数据库之外的任何内容(使用链接服务器有点作弊)。 So you usually need to use some sort of client application anyway, whether it's bcp.exe, dtswiz.exe or your own program. 所以你通常需要使用某种客户端应用程序,无论是bcp.exe,dtswiz.exe还是你自己的程序。

And as a final comment, MSSQL 2000 is no longer supported (unless your company has an extended maintenance agreement) so you may want to look into upgrading at some point. 作为最终评论,不再支持MSSQL 2000(除非您的公司有扩展的维护协议),因此您可能希望在某些时候考虑升级。

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

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