I have a production database with numerous scalar functions. My goal is to create set based alternatives for reporting. Unfortunately, I don't have a good way to view the T-SQL actually being executed because the functions reference a CLR assembly. Is there a good way to view the T-SQL inside the assembly for each function?
Sql-server – View T-SQL in CLR Assembly
sql serversql-clr
Related Question
- Sql-server – .NET SQLCLR Assembly not working in SQL Server 2016 (Error msg 10314)
- SQL Server – Understanding SQLCLR_QUANTUM_PUNISHMENT in CLR Procedures
- SQL Server – Assembly Not Authorized for PERMISSION_SET=UNSAFE When Creating a CLR Assembly
- SQL Server CLR Method Failing Due to TLS 1.0 Shutdown
- SQL Function to Get Value for CLR in SQL Server
- Adding .Net Assembly to SQL CLR Without TRUSTWORTHY ON
- Sql-server – Apparently, the CLR assembly function is causing deadlocks
Best Answer
T-SQL being executed is different than T-SQL that will be executed if the function is run.
To see what is currently executing, you would use either SQL Server Profiler, or Extended Events. Then you can execute a function and see what T-SQL it runs.
To see what the function might execute, that is a bit trickier. You do have the code in the
sys.assembly_files
system catalog view. Thecontent
field contains the binary representation of the assembly. But, to see the code you need to find a way to extract it to a binary file (no built-in method, and no, not a text file containing that "binary" string) and then decompile it.String literals should be contained as-is in the assembly, so theoretically you should be able to convert the
VARBINARY
value insys.assembly_files.content
toNVARCHAR
, but there are two problems there:0x00
bytes, and those will stop the displaying of what comes after it because it is the "null" terminator for strings, so you need to find a way to convert those to spaces (while also not converting a trailing0
of one byte followed by a leading0
of the next byte).So, perhaps just use SQL Server Profiler or Extended Events to see what T-SQL is submitted when the functions are executed.