简体   繁体   English

以CSV格式导出数据表SQL Server 2008

[英]Export a data table in CSV format sql server 2008

I need to create a stored procedure which queries a table and creates a CSV file for that data in a specified directory location. 我需要创建一个存储过程,该存储过程查询表并在指定目录位置为该数据创建一个CSV文件。

how do i do it? 我该怎么做?

You can't do it with a stored procedure directly. 您不能直接使用存储过程来做到这一点。 I suggest right-clicking your database in SQL Server Management Studio, and selecting "Export Data". 建议您在SQL Server Management Studio中右键单击数据库,然后选择“导出数据”。

Supply it with a query or a stored procedure that returns rows. 向其提供查询或返回行的存储过程。 Tell it you want the file to be delimited by commas and text-qualified with quotes. 告诉它您希望文件用逗号分隔并用引号引起来的文本限定。

When the wizard asks you if you want to just execute it or save it, save it to disk. 当向导询问您是否要执行或保存它时,将其保存到磁盘。

This makes something called an SSIS package. 这产生了一个称为SSIS包的东西。 You can then use Windows Explorer to run it, or use a command line program called dtexec.exe to run it with parameters, or give it to your DBA to have them run it inside SQL Server. 然后,您可以使用Windows资源管理器来运行它,或使用名为dtexec.exe的命令行程序来运行带有参数的文件,或者将其交给DBA以使其在SQL Server中运行。

You can call BCP via exec xp_cmdshell 'bcp dbname..table out filename.bcp .....' 您可以通过exec xp_cmdshell'bcp dbname..table out filename.bcp .....'调用BCP。

this is a very similar question except it for importing. 除了要导入外,这是一个非常相似的问题。 Use bcp to import csv file to sql 2005 or 2008 使用bcp将CSV文件导入到SQL 2005或2008

See here for more information on BCP. 有关BCP的更多信息,请参见此处。

It is possible to do but you need to have enough rights to run xp_cmdshell and ad hoc distributed queries. 可以这样做,但是您需要具有足够的权限才能运行xp_cmdshell和临时分布式查询。

sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO
sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO
DECLARE @OutputPath VARCHAR(1000)
SET @OutputPath = 'd:\temp\'
DECLARE @OutputFilename VARCHAR(1000)
DECLARE @ServerName VARCHAR(1000)
SET @ServerName='servername'
DECLARE @Username VARCHAR(50)
SET @Username = 'username'
DECLARE @Password VARCHAR(50)
SET @Password = 'password'
DECLARE @DatabaseName VARCHAR(50)
SET @DatabaseName = 'databasename'
DECLARE @Dsn VARCHAR(1000)
SET @Dsn = 'Server='+@ServerName+';Database='+@DatabaseName+';Uid='+@Username+';Pwd='+@Password+';'

DECLARE @StartProcedureTime DATETIME
DECLARE @EndProcedureTime DATETIME
DECLARE @TimeTaken INTEGER 
DECLARE @QueryToRun VARCHAR(1000)
DECLARE @Query VARCHAR(1000)
DECLARE @result INT

SET @Query = 'mkdir "' + @OutputPath + @DatabaseName +'\"'
EXEC @result = [master]..xp_cmdshell @Query

DECLARE db_cursor CURSOR FOR
SELECT
 T.[FileName] + '.csv' AS [FileName],
 REPLACE(T.SqlToRun, '''', '''''') AS [SqlToRun]
FROM
(
 SELECT 'sp_DataGenerator_AverageRatingAverage' AS [FileName], 'exec sp_DataGenerator_AverageRatingAverage 54' AS [SqlToRun]
 UNION ALL
 SELECT 'sp_DataGenerator_AverageRatingGood', 'exec sp_DataGenerator_AverageRatingGood ''01-01-2009'', ''01-01-2010'''
 UNION ALL
 SELECT 'sp_DataGenerator_AverageRatingBad', 'exec sp_DataGenerator_AverageRatingBad ''01-01-2009'', ''01-01-2010'''
) AS T

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @OutputFilename, @QueryToRun
WHILE @@FETCH_STATUS = 0
BEGIN
 SET @Query = 'bcp "SELECT * FROM OPENROWSET (''SQLOLEDB'','''+@Dsn+''','''+@QueryToRun+''') AS T" queryout '+@OutputPath + @DatabaseName + '\' + @OutputFilename +' -c -t, -r\n -U'+@Username+' -P'+@Password+'  -S'+@ServerName + ' > ' +@OutputPath + @DatabaseName + '\' + @OutputFilename +'.txt'

 SET @StartProcedureTime = getdate() --take start time 
 EXEC @result = [master]..xp_cmdshell @Query
 SET @EndProcedureTime = getdate() --take end time 
 SET @TimeTaken = DATEDIFF(millisecond, @StartProcedureTime, @EndProcedureTime) --take difference in milliseconds.

 PRINT '"'+@QueryToRun + '" took ' + str(@TimeTaken) + ' Milliseconds.'

 FETCH NEXT FROM db_cursor INTO @OutputFilename, @QueryToRun   
END
CLOSE db_cursor  
DEALLOCATE db_cursor

go

sp_configure 'xp_cmdshell', 0
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 0
GO
RECONFIGURE
GO
sp_configure 'Show Advanced Options', 0
GO
RECONFIGURE
GO

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

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