简体   繁体   English

SQL Server存储过程将选择结果导出为CSV

[英]SQL Server stored procedure to export Select Result to CSV

In my stored procedure, I want to export select result to a .CSV file. 在我的存储过程中,我想将选择结果导出到.CSV文件。 I need to write a stored procedure which selects some data from different tables and saves it to a .CSV file. 我需要编写一个存储过程,从不同的表中选择一些数据并将其保存到.CSV文件中。

Selecting part is ready 选择部件已准备就绪

 SELECT DISTINCT 
                  PER.NREGNUMBER_PERNUM AS [Registration Number], 
                  PER.CFAMNAME_PER AS [Family Name], 
                  PER.CGIVNAME_PER AS [Given Name], 
                  CONVERT(varchar(10), CONVERT(date, PER.DBIRTHDATE_PER, 106), 103) AS [Birth Date], 
                  PER.CGENDER_PERGEN as [Gender],
                  PHONE.MOBILE_NUMBER
        FROM      PERSON AS PER 
                  LEFT OUTER JOIN
                  PHONE ON PER.NREGNUMBER_PERNUM = PHONE.NREGNUMBER_PPHPER AND PHONE.CPRIMARY_PPH = 'Y'

The task was I had to export from database some data to .CSV at specified time. 任务是我必须在指定时间从数据库中将一些数据导出到.CSV In the begining we wanted to use windows scheduler for running stp. 在开始时我们想使用Windows调度程序来运行stp。 The STP had to be able to export data. STP必须能够导出数据。 But I couldn't find a way. 但我找不到办法。 Instead the thing what we did was creating simple STP which brings only data . 相反,我们所做的就是创建简单的STP,只带来数据。 And we created batch file which calls STP and export result to .CSV file. 我们创建了调用STP并将结果导出到.CSV文件的批处理文件。 The batch file is simple 批处理文件很简单

sqlcmd -S Etibar-PC\SQLEXPRESS -d MEV_WORK -E -Q "dbo.SelectPeople" -o "MyData1.csv" -h-1 -s"," -w 700

dbo.SelectPeople is STP dbo.SelectPeople是STP
Etibar-PC\\SQLEXPRESS is Schema Etibar-PC \\ SQLEXPRESS是Schema
MEV_WORK is Database name. MEV_WORK是数据库名称。

i have build a procedure to help you all 我已经建立了一个程序来帮助你们

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

-- example exec Sys_Database_exportToCsv 'select MyEmail from 
QPCRM.dbo.Myemails','D:\test\exported.csv'

create PROCEDURE Sys_Database_exportToCsv

(
@ViewName nvarchar(50),
@exportFile nvarchar(50)
)

AS

BEGIN

SET NOCOUNT ON;

EXEC sp_configure 'show advanced options', 1;  

RECONFIGURE; 

EXEC sp_configure 'xp_cmdshell', 1;  

RECONFIGURE; 

Declare @SQL nvarchar(4000)

Set @SQL = 'Select * from ' + 'QPCRM.dbo.Myemails'

Declare @cmd nvarchar(4000)

SET @cmd = 'bcp '+CHAR(34)+@ViewName+CHAR(34)+' queryout 
'+CHAR(34)+@exportFile+CHAR(34)+' -S '+@@servername+' -c -t'+CHAR(34)+','+CHAR(34)+' -r'+CHAR(34)+'\n'+CHAR(34)+' -T'

exec master..xp_cmdshell @cmd

EXEC sp_configure 'xp_cmdshell', 0;  

RECONFIGURE; 

EXEC sp_configure 'show advanced options', 0;  

RECONFIGURE; 

END

GO

To create a CSV string from you table result follow the instruction on 要从表格结果创建CSV字符串,请按照说明进行操作

http://blog.sqlauthority.com/2009/11/25/sql-server-comma-separated-values-csv-from-table-column/ http://blog.sqlauthority.com/2009/11/25/sql-server-comma-separated-values-csv-from-table-column/

As far as I know there is no way to save data into a file on disk directly using TSQL. 据我所知,没有办法直接使用TSQL将数据保存到磁盘上的文件中。 To save a file, you can create a SQL-CLR stored procedure instead of a TSQl one in which you can use regular C# code. 要保存文件,您可以创建一个SQL-CLR存储过程而不是TSQl存储过程,您可以在其中使用常规C#代码。

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

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