title: TSQL in Memory VarBinary to Text File?
body: <p>With SQL Server 2008, I have a stored procedure that accepts a VarBinary parameter. While the parameter is in memory, I would like to write it to the %temp% directory of the SQL Server.</p>
  
  <p>How can I go from memory blob (it came as a byte[] from a C# app) to text file? The blob does not exist in any disk-resident table.</p>
  
  <p>I am leaning towards having the TSQL sproc call a CLR stored procedure, which so far looks (mostly) like the below.  </p>
  
  <p>Problem #1 is that I think the inputBytes[] is stuck at 8k input size.</p>
  
  <p>Problem #2 is that I can't seem to come up with some decent SQL that passes a varbinary value as a parameter so I can test this within Management Studio or VS DB unit test.</p>
  
  <pre><code>public static void FileWrite( string inputName , byte[] inputBytes )
  using (FileStream fileStream = new    
     FileStream(Environment.GetEnvironmentVariable("TEMP") + "\\" + inputName,   
     FileMode.OpenOrCreate, FileAccess.Write))
     {
          using ( BinaryWriter binaryWriter = new BinaryWriter(fileStream))
          {
             binaryWriter.Write( inputBytes );
      }
  }
  </code></pre>
  
  <p>Thanks.</p>