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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

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

Move files with CLR

From Wiki

Jump to: navigation, search

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

  1. USE [DBA]
  2. GO
  3. CREATE PROCEDURE [dbo].[MoveFile]
  4.     @sfilename [nvarchar](4000),
  5.     @dfilename [nvarchar](4000)
  6. WITH EXECUTE AS CALLER
  7. AS
  8. EXTERNAL NAME [CLRMOVEFILE].[StoredProcedures].[MoveFile]
  9. GO
  10. EXEC sys.sp_addextendedproperty @name=N'AutoDeployed', @value=N'yes' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'MoveFile'
  11. GO
  12. EXEC sys.sp_addextendedproperty @name=N'SqlAssemblyFile', @value=N'MoveFile.cs' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'MoveFile'
  13. GO
  14. 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)

673 Rating: 2.7/5 (43 votes cast)