Export a Table to a Date Stamped File Name in SQL Server

June 20, 2003

by MAK Muthusamy Anantha Kumar

Date stamping is a very common requirement in the IT industry for any data extract created. Through the DTS package with ActiveX script, it is very easy to date stamp a file. In this article, I am going to walk you through how to export a table to a date stamped file.

Create a DTS package with one SQL Server connection and a Destination Text connection.

Point the SQL Server connection to your local server and its database to the PUBS database.

Change the path of your Text file destination to c:\mytext.txt and name the connection as "DestinationFile".

Create a transformation task between the SQL Server connection and the DestinationFile.

Edit the transformation to transfer the table authors to the text file.

Now create an ActiveX script task and paste the following code into it.


'************************
'  Visual Basic ActiveX Script
'************************
Function Main()

mydate =now()
sFilename = "c:\MyFile_" &  Right(Year(mydate), 4)
If Month(mydate) < 10 Then sFilename = sFilename & "0" & _
Month(mydate) Else sFilename = sFilename & Month(mydate)
If Day(mydate) < 10 Then sFilename = sFilename & _
"0" & Day(Mydate) Else sFilename = sFilename & Day(mydate)
sFilename = DTSGlobalVariables("LogFilePath").Value & _
sFilename &  ".txt"
Set oConn = DTSGlobalVariables.Parent.Connections("DestinationFile")
oConn.DataSource = sFilename
Set oConn = Nothing
Main = DTSTaskExecResult_Success
End Function

This ActiveX script formulates the path and filename along with today's date stamp and changes the connection in the DestinationFile.

If today's date is March 3, 2003 then, when you execute the ActiveX script DestinationFile, the path in the connection DestinationFile will automatically be changed to the new path "c:\myfile_20030303.txt"

Now add a success task between the ActiveX script task and the SQL Server connection.

When you execute the package, first the ActiveX script formulates the destination text file location with today's date as suffix, and then does the transformation.

Conclusion:

Every time when you execute the package, the ActiveX script formulates the full path of the source files with today's date as suffix. This script can be used for all kinds of source files like XL, .csv, .txt, .mdb etc.








The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers