Quantcast
Channel: SQL Server Integration Services forum
Viewing all articles
Browse latest Browse all 24688

SSIS Detach Database copy file to remote folder

$
0
0

OK, I have a application that creates a new database every so often. I want to use SSIS 2008 to detach any copy older than say 3 months and copy the files to a "archive storage" location.

I have a Data Flow that identifies the databases to be archived which builds a recordset.  From here it does a foreach loop and another Data Flow that gets the file paths for all said databases and some other information about each databases which builds another recordset.  Now I technially have all the data I need, So I can detach and then copy the files.  This is where I am stuck, how can I detach 1 database at a time and copy the files?

My thought was a EXECUTE SQL detach and a Script task to create folders and copy the files.

I can not get a Parameterized Detach Script to work.
It is not the prettiest script but it works in SSMS?

DECLARE @strDBNamer as varchar(255)
SET @strDBNamer ='@strDBName'
DECLARE @SQL AS VARCHAR(4000)
IF EXISTS (SELECT 1 
FROM sys.databases
WHERE name = @strDBNamer)
BEGIN
SET @SQL = '
	USE [master];
	ALTER DATABASE ['+@strDBNamer+'] SET  SINGLE_USER WITH ROLLBACK IMMEDIATE;
	EXEC master.dbo.sp_detach_db @dbname = N'''+@strDBNamer+'''
	'
	
	EXEC(@SQL)
END

and then for the Script Task (VB.NET), something like this.

       Dim strDBName As String
        Dim strFilePath As String
        Dim strFileName As String
        Dim strMaxDate As String
        Dim strMinDate As String
        Dim strDest As String
        strDBName = Dts.Variables("db_name").Value.ToString
        strFilePath = Dts.Variables("file_path").Value.ToString
        strDest = "\\xxxx\xxxx\"
        Dim FileObject As System.IO.FileInfo = New System.IO.FileInfo(strFilePath)
        strFileName = FileObject.Name
        strMaxDate = Format(Dts.Variables("max_date").Value, "MM_dd_yyyy")
        strMinDate = Format(Dts.Variables("min_date").Value, "MM_dd_yyyy")
        If Not System.IO.Directory.Exists(strDest & strMinDate & "-" & strMaxDate) Then
            System.IO.Directory.CreateDirectory(strDest & strMinDate & "-" & strMaxDate)
        End If
        If File.Exists(strDest & strMinDate & "-" & strMaxDate & "\" & strFileName) Then
            File.Copy(strFilePath, strDest & strMinDate & "-" & strMaxDate & "\" & strFileName, False)
        End If
How could I get it to fail if the copy has error?

Thanks, Matt


Viewing all articles
Browse latest Browse all 24688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>