简体   繁体   English

使用SQL Server存储过程导出csv文件中的表数据

[英]Exporting table data in csv file using SQL Server stored procedure

I have these requirements: 我有这些要求:

  1. Export table data into a .csv file using a stored procedure 使用存储过程将表数据导出到.csv文件中
  2. First row in the .csv file will be a custom header .csv文件中的第一行将是自定义标头

Note: data in this row will not come from table. 注意:此行中的数据不会来自表。 It will be a fixed header for all the .csv being generated. 它将是生成的所有.csv的固定标头。

Similar to something like this: 类似于这样的事情:

Latest price data:
product1;150;20150727
product2;180;20150727
product3;180;20150727

Assuming that date is a proper datetime column the following procedure will at least do the job for this table named prodtbl : 假设date是一个正确的datetime列,以下过程至少会为这个名为prodtbl表执行以下任务:

CREATE proc csvexp (@header as nvarchar(256)) AS
BEGIN
 SELECT csv FROM (
  SELECT prod,date,prod+';'+CAST(price AS varchar(8))+';'
        +replace(CONVERT(varchar,getdate(),102),'.','') csv
  FROM prodtbl
  UNION ALL
  SELECT null,'1/1/1900',@header
 ) csvexp ORDER BY prod,date
END

The command 命令

EXEC csvexp 'my very own csv export'

will then generate the following output: 然后将生成以下输出:

my very own csv export
product1;150;20150727
product2;180;20150727
product3;180;20150727

The part of actually getting this output into a .csv file still remains to be done ... 实际上将此输出转换为.csv文件的部分仍有待完成......

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

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