Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

Community Wiki

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.

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.

Navigation

Google Ads

Write to file in SQL Server 2005+

From Wiki

Jump to: navigation, search

Use this deployed to a designated DBA database set to TRUSTWORHTY and EXTERNAL write contents to a file

Create a C# database project and use the following code

  1. public class SQLCLRIO
  2. {
  3.  
  4.     public static void WriteToFile(String content, String filename)
  5.     {
  6.         try
  7.         {
  8.             File.WriteAllText(filename, content);
  9.         }
  10.         catch (Exception ex)
  11.         {
  12.             SqlContext.Pipe.Send("Error writing to file : " + ex.Message);
  13.         }
  14.     }
  15. }

Then run the following to create your proc in SQL Server

  1. CREATE PROCEDURE [dbo].[writetofile]
  2.     @content [nvarchar](max),
  3.     @filename [nvarchar](255)
  4. WITH EXECUTE AS CALLER
  5. AS
  6. EXTERNAL NAME [CLRIO].[SQLCLRIO].[WriteToFile]

--onpnt 13:28, 17 February 2010 (GMT)

674 Rating: 2.5/5 (30 votes cast)