sikender
sikender

Reputation: 5921

Can i use a Cursor within a User Defined Function In SQL Server 2005 managed code?

By using a cursor I want to make a virtual table. After that, using a function I want to use that virtual table and pass the values of the original table and then I show the virtual table into the output.

Upvotes: 0

Views: 1151

Answers (1)

Nestor
Nestor

Reputation: 13990

Yes. But you can also use SqlDataReader to accomplish the same. Note that you might have to create a new connection from the embedded c# (instead of using SQLContext).

string queryString = "SELECT OrderID, CustomerID FROM dbo.Orders;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    SqlCommand command = new SqlCommand(queryString, connection);
    using (SqlDataReader reader = command.ExecuteReader())
    {
       while (reader.Read())
       {
          var c1 = reader[0];
          var c2 = reader[1];
          ....
       }
       reader.Close();
    }
}

Check this for an example of how to wrap this code inside a Table-Valued-Function.

Upvotes: 1

Related Questions