Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

MS SQL

Posted Jun 20, 2003

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

By DatabaseJournal.com Staff

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.



MS SQL Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM


















Thanks for your registration, follow us on our social networks to keep up-to-date