title: How to select rows inside a CLR stored procedure in MS SQL Server 2008?
body: <p>Following along with an example I found on the internet, I wrote the following C# code that implements a CLR stored procedure in MS SQL Server 2008:</p>
  
  <pre><code>public class Class1
  {
      [Microsoft.SqlServer.Server.SqlProcedure]
      public static void CountStringLength(string inputString)
      {
          SqlContext.Pipe.Send(inputString.Length.ToString());            
      }
  }
  </code></pre>
  
  <p>This procedure takes a string as a parameter and outputs the number of characters in the string.</p>
  
  <p>I have been working on some code that retrieves data from SQL; there's nothing special about the way the data is retrieved: it makes a connection to the SQL server, selects the data, and then closes the connection.  I want to make this code run inside a stored procedure or trigger on my SQL server.</p>
  
  <p>I suppose that I could make the code run exactly the same as the existing SQL code: make a connection to the SQL server, select the data, and then close the connection.  However, this doesn't make sense once the code is running on the SQL server itself!  Why would I want code that runs on the SQL server make the server connect to itself?!?!</p>
  
  <p>Is there a best practice for what I'm trying to do?  Can I select rows in my code using the same connection that is used to execute the stored procedure?</p>