Skip to content

SQL Libary to export SQL query output to file

Notifications You must be signed in to change notification settings

aetperf/ExportSql

 
 

Repository files navigation

ExportSql

SQL CLR Libary to produce file output from SQL stored procedures.

  • Produce a CSV file output based on SQL Statement output. Example below produces as CSV file with the Sales table data
DECLARE @sqlcommand nvarchar(MAX);
DECLARE @filepath NVARCHAR(MAX);
DECLARE @delimiter NCHAR(1);
DECLARE @dateformat NVARCHAR(50);
DECLARE @decimalSeparator CHAR(1);

SET @sqlcommand = 'SELECT * from [SalesDB].[dbo].[Sales]';
SET @filepath = 'C:\temp';
SET @delimiter = '|';
SET @dateformat = 'yyyy-MM-dd HH:mm:ss';
SET @decimalSeparator = '.';

Exec [dbo].[RowbyRowSql2Csv]
	@sql = @sqlcommand,
	@filePath = @filepath,
	@fileName = 'SALES.CSV',
	@includeHeader = 1,
	@delimiter = @delimiter,
	@UseQuoteIdentifier = 0,
	@overWriteExisting = True,
	@Encoding = 'windows-1252',
	@dateformat = @dateformat,
	@decimalSeparator = @decimalSeparator,
	@maxdop = 4,
	@distributeKeyColumn='PRODUCT_KEY'; -- Must be an integer or bigint column (or a computed formula that return a int) ideally the rows should be evenlly balanced (the modulus operator on the maxdop is used to split data)

About

SQL Libary to export SQL query output to file

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 89.9%
  • TSQL 10.1%