Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
Move files with CLR
From Wiki
Use this deployed to a designated DBA database set to TRUSTWORHTY and EXTERNAL access to move files around
Create a C# database project and use the following code
public partial class StoredProcedures {
[Microsoft.SqlServer.Server.SqlProcedure]
public static void MoveFile(string sfilename, string dfilename)
{
try
{
File.Move(sfilename, dfilename);
}
catch (Exception ex)
{
SqlContext.Pipe.Send("Error writing to file : " + ex.Message);
}
}
};
Then run the following to deploy to your instance
- USE [DBA]
- GO
- CREATE PROCEDURE [dbo].[MoveFile]
- @sfilename [NVARCHAR](4000),
- @dfilename [NVARCHAR](4000)
- WITH EXECUTE AS CALLER
- AS
- EXTERNAL NAME [CLRMOVEFILE].[StoredProcedures].[MoveFile]
- GO
- EXEC sys.SP_ADDEXTENDEDPROPERTY @name=N'AutoDeployed', @VALUE=N'yes' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'MoveFile'
- GO
- EXEC sys.SP_ADDEXTENDEDPROPERTY @name=N'SqlAssemblyFile', @VALUE=N'MoveFile.cs' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'MoveFile'
- GO
- EXEC sys.SP_ADDEXTENDEDPROPERTY @name=N'SqlAssemblyFileLine', @VALUE=11 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'MoveFile'
--onpnt 13:23, 17 February 2010 (GMT)



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.