Consider the following scenario:
- You have a SQL CLR function in SQL Server 2016 and 2017.
- The CLR function invokes Transact-SQL statements through impersonation calls, such as by calling the EXECUTE AS statement.
In this scenario, the execution fails, and you receive the following error message:
Msg 6522, Level 16, State 1, Procedure PrcedureName, Line 0 [Batch Start Line LineNumber]
A .NET Framework error occurred during execution of user-defined routine or aggregate "ProcedureName":
System.Data.SqlClient.SqlException: The server principal "LoginName" is not able to access the database "DatabaseName" under the current security context.
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.