简体   繁体   English

将查询输出到CSV文件

[英]Output Query into CSV file

Can someone help point me in the write direction. 有人可以帮我指出写作方向吗? I want to schedule this query to run and out put to a csv. 我想安排此查询运行并输出到csv中。 How can I adapt the query to output to a csv? 如何调整查询以输出到csv?

SELECT d.result1
FROM
  (SELECT COUNT(*) AS result1
   FROM
     ( SELECT test.dbo.OrderedDocuments.UserDocumentID,
              test.dbo.OrderedDocuments.OrderGroupID,
              test.dbo.OrderGroups.TimePlaced
      FROM test.dbo.OrderedDocuments
      INNER JOIN test.dbo.OrderGroups ON test.dbo.OrderedDocuments.OrderGroupID = test.dbo.OrderGroups.OrderGroupID
      WHERE test.dbo.OrderGroups.TimePlaced >'2018-05-17') mySubQuery) d

You can do this by creating a SSIS package and schedule it. 您可以通过创建SSIS包并计划它来完成此操作。

  1. Right click database you are using > Tasks > Export Data 右键单击您正在使用的数据库>任务>导出数据
  2. Check server, authentication, and database details are correct > Next 检查服务器,身份验证和数据库详细信息是否正确>下一步
  3. Select Flat File Destination as the destination, and enter the file name of the csv file you wish to output to > Next 选择“平面文件目标”作为目标,然后输入要输出到的csv文件的文件名>“下一步”。
  4. Choose the second option, to write a query > Next 选择第二个选项,编写查询>下一步
  5. Enter the query > Parse to ensure the query is valid > Next 输入查询>解析以确保查询有效>下一步
  6. Click Preview to ensure the data is what you expect > Next 单击预览以确保数据符合您的期望>下一步
  7. Click the 'Save SSIS Package' checkbox > Select where you want to save it 单击“保存SSIS包”复选框>选择要保存的位置
  8. Give the file a name> Next 为文件命名>下一步
  9. Double check the package > Finish 仔细检查包装>完成

Use this link for reference on how to schedule a job using SQL Server Management Studio 使用此链接可参考有关如何使用SQL Server Management Studio安排作业的参考

Then Just Schedule the Job, SQL Select SIS Package you just created. 然后,只需计划作业,即可使用刚刚创建的SQL Select SIS程序包。

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

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