10

I've got a SqlServer project with a very simple test for a Table-Valued-Function:-

[SqlFunction(TableDefinition = "forename nvarchar(50)", FillRowMethodName = "TestFillRow", DataAccess = DataAccessKind.Read)]
public static IEnumerable TestConn(int ID)
{
    using (SqlConnection con = new SqlConnection("context connection=true"))
    {
        //con.Open();
        yield return "Anthony";
    }
}

public static void TestFillRow(object obj, out string forename)
{
    forename = (string)obj;
}

Note the Open on the connection is currently commented out. Once deployed I can execute like this in SQL:-

SELECT * FROM [dbo].[TestConn](1)

All works fine.

Now I uncomment the con.open() and it fails with:-

Data access is not allowed in this context. Either the context is a function or method not marked with DataAccessKind.Read or SystemDataAccessKind.Read, is a callback to obtain data from FillRow method of a Table Valued Function, or is a UDT validation method.

I don't see what the problem is, the TestConn function has got DataAccessKind.Read.

Anyone know of any other reasons for getting this error?

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306

2 Answers2

16

The problem is the following:

  1. SQLCLR does not allow any data access inside TestFillRow

  2. Even though it "looks" like your TestFillRow doesnt access data, the way the compiler translates code with "yield" statements is by actually deferring it's execution until the first .MoveNext() call to the iterator. Therefore the following statement:

    using (SqlConnection con = new SqlConnection("context connection=true"))        
    

    gets executed inside TestFillRow... which is illegal.

Do not use yield return; instead load the whole result to a List<> and return the list at the end of the UD Function.

Braiam
  • 1
  • 11
  • 47
  • 78
Nestor
  • 13,706
  • 11
  • 78
  • 119
  • A very belated acceptance and upvote, (prompted I'm ashamed to say by a down vote on my question). You're absolutely right the nothing runs until Movenext. I've even blogged about this behaviour myself here:- http://geekswithblogs.net/codingbloke/archive/2010/09/12/simple-asynchronous-operation-runner-ndash-part-1.aspx but I clearly didn't see the connection between them until now. – AnthonyWJones Jan 29 '11 at 14:13
  • See Connect bug regarding FillRow limitations: http://connect.microsoft.com/SQLServer/feedback/details/442200/sql-server-2008-clr-tvf-data-access-limitations-break-existing-code – piers7 Jan 17 '14 at 06:43
  • 2
    It is not the problem that the connection *opens* inside `TestFillRow`. Even if you provide your own "eager" implementation of an enumerator that opens the connection and fetches the first row inside the main method, that enumerator will not work from `TestFillRow` because the connection will have been forcefully closed. – GSerg Feb 05 '15 at 17:08
1

"SQLCLR does not allow any data access inside TestFillRow" is a mistake.

If you don't use context connection = true connetion string you can access data inside FillRow method.

Jeroen
  • 60,696
  • 40
  • 206
  • 339
mshakurov
  • 64
  • 6