How can I get the definition of a SQL CLR function from the database

谁说我不能喝 提交于 2019-12-11 02:17:56

问题


The code I was given for an SQL CLR project contains one function where the .net implementation is invalid. However I have been able run a query to confirm that there is a function with the name of the invalid one on the server.

Schema  name            assembly_name  assembly_class              assembly_method  permission_set_desc  type_desc
dbo     ConvertFromUTC  database       AppName.Database.Functions  ConvertFromUTC   UNSAFE_ACCESS        CLR_SCALAR_FUNCTION

That's enough information to confirm the deployed version is a scalar function not a table as suspected.

Is there I way I can get the input/output parameters from the version loaded in the database to make sure that the correct version is what was suggested in my previous question and not something else? More generally I'd like to be able to get the same results for all the other CLR functions in the database to see if any of them are mismatches vs the code I was given as well.


回答1:


There are several different SQLCLR object types and so this would take a few queries. The quickest and easiest way to get the definitions, especially for a one-time operation, is to script out the objects via SQL Server Management Studio (SSMS). You should be able to select all of each particular type in "Object Explorer Details" and script them out in all together.

Or, to do all SQLCLR objects across all types in one shot, go to the general "Generate Scripts":

  1. Right-click on database in Object Explorer
  2. Go to "Tasks >" submenu
  3. Select "Generate Scripts..."
  4. Choose "Select specific database objects"
  5. Drill down to the various object types – Stored Procedures, User-Defined Functions, User-Defined Types, and User-Defined Aggregtes – and check the ones that you want to script out
  6. Click "Next >"
  7. Choose your preferred method of saving / viewing
  8. Click "Next >"
  9. Click "Next >" (again, after reviewing)
  10. Click "Finish"

For just User-Defined Aggregates (UDA) and scalar User-Defined Functions (UDF), you can use the following query (keep in mind that parameter_id of 0 is the return type):

SELECT OBJECT_NAME(am.[object_id]) AS [Name], am.*, p.*, t.[name] AS [DataType]
FROM   sys.assembly_modules am
INNER JOIN sys.parameters p
        ON p.[object_id] = am.[object_id]
INNER JOIN sys.types t
        ON t.[user_type_id] = p.[user_type_id]
WHERE  OBJECT_NAME(am.[object_id]) = N'{function_name}';


来源:https://stackoverflow.com/questions/45083492/how-can-i-get-the-definition-of-a-sql-clr-function-from-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!