Examples

Current Articles | Categories | Search

By Wayne Hamilton @ Thursday, June 05, 2008 8:35 AM :: 1797 Views :: 0 Comments

For a project I was working on, users wanted to be able Trigger exports from SQL to a Mainframe application that used a Fixed Column Width format for the data transfer.  Since the Mainframe expects the same file name each time, the users wanted a way to maintain a history of the files that were exported across.  I tried to set up the parameters so that it could be reused without having to rewrite it each time.

Originally this started out as an SSIS package, but the users weren't happy with the delay in the export, and I didn't want to schedule the job to run every minute, or set up a "WAIT" as a first step and just keep the job running in memory, so I looked into using the SQL CLR.

This solution uses a CLR Stored Procedure that accepts a Query, FileName and ArchivePath to create the file(s).  The Query and FileName are required since that's the point of the procedure, but if your don't need to archive the files, you can pass in an Empty String for the Archive Path.  To give the Stored Procedure the most flexibility, I do all of the data formatting in the Query that's passed in.  In my current case, I have a View that puts in any padding requirements for the columns.  Since the Stored Procedure works with the File System obviously the assembly must be created with External Access or the Trustworthy option of the database must be set.

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server
Imports System.IO
Partial Public Class StoredProcedures
<Microsoft.SqlServer.Server.SqlProcedure()> _
Public Shared Sub ExportFixedWidthFile(ByVal Query As SqlString, _
ByVal FileName As SqlString, ByVal ArchiveFile As SqlString)
'Open the Stream for writing the File
Dim tw As New StreamWriter(FileName.ToString(), False)
'Use the existing Database Connection
Using c As New SqlConnection("context connection=true")
c.Open()
'Create the Command using the Query and Execute as a Data Reader
Dim cmd As New SqlCommand(Query, c)
Dim dr As SqlDataReader = cmd.ExecuteReader()
'Loop Through the Rows and Columns sending the Data to the Stream
While dr.Read
For i As Integer = 0 To dr.FieldCount - 1
tw.Write(dr.GetString(i))
Next
'Write a New Line following Each Record
tw.WriteLine()
tw.Flush()
End While
dr.Close()
c.Close()
End Using
tw.Close()
tw = Nothing
'If ArchiveFile is passed in, then copy the file to the Archive
If ArchiveFile.ToString <> "" Then
System.IO.File.Copy(FileName.ToString(), ArchiveFile.ToString, True)
End If
End Sub
End Class

Once the assembly is loaded into your database and the Stored Procedure you can call it as easially as this:

DECLARE
@Query VARCHAR(1000),
@FileName VARCHAR(255),
@ArchiveFile VARCHAR(255)
--Export File without an Archive File
SELECT
@Query = 'SELECT * FROM myExportView',
@FileName = 'C:\Temp\TestFile.txt',
@ArchiveFile = ''
EXEC dbo.ExportFixedWidthFile @Query, @FileName, @ArchiveFile
--Export File with an Archive
--(Normally, I add a Date Time to the Archive File Name.)

SELECT
@Query = 'SELECT * FROM myExportView',
@FileName = 'C:\Temp\TestArchive.txt',
@ArchiveFile = 'C:\Temp\Archive\Tested.txt'
EXEC dbo.ExportFixedWidthFile @Query, @FileName, @ArchiveFile

 

 

Posted @ Thursday, June 05, 2008 8:35 AM by Wayne Hamilton
Previous Page | Next Page
Comments
Currently, there are no comments. Be the first to post one!
You must be logged in to post a comment. You can login here

Survey

Which of the following CLR objects are you currently using in SQL Server?




Submit Survey  View Results

Links

  Search

What's New

 Subscribe in a reader

Q&A with Adam Machanic - Who's Afraid of SQLCLR by Jonathan Kehayias

You could almost hear the gasps of horror when Microsoft integrated the .NET Common Language Runtime (CLR) into SQL Server 2005. Run C# or VB.NET code inside SQL Server? For many database developers and administrators, it was like an alien had just burst through SQL Server’s midsection.

But SQL Server MVP Adam Machanic says that more and more developers and DBAs are discovering that far from destroying SQL Server performance and security, SQLCLR is actually a powerful ally in solving complex business and technical problems.

Read More on the PASS website..

SQLCLR String Splitting Part 2: Even Faster, Even More Scalable by Site Administrator

SQL Server MVP Adam Machanic shows a new way of parsing strings in SQL Server using SQLCLR that outperforms all conventional TSQL methods, as well as the most common SQLCLR implemenations, using a custom split function that allows streaming of the results.

Trading in xp_cmdshell for SQLCLR (Part 1) - List Directory Contents by Jonathan Kehayias

Learn how to use SQLCLR to get file system information instead of using xp_cmdshell on your SQL Servers.

Recent Examples


Copyright 2007 by SQLCLR.net Terms Of Use Privacy Statement
Website graphics provided by Matt Green Designs
Page generated in 0.1404045 seconds.

All information and example code on this site is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from its use.

This site is in no way affiliated with Microsoft. Unless specifically stated otherwise, nothing should be construed to represent the official positions or opinions of Microsoft and/or its Employees.