2

我试图在 3 个不同的数据库中获取用户定义函数的列表和定义,以组合成一个数据库。

我有列出 3 个用户定义函数的 select 语句,但是当我运行它时,该ROUTINE_BODY列显示'EXTERNAL'butEXTERNAL_NAMEEXTERNAL_LANGUAGEis NULL(以及ROUTINE_DEFINITION它应该是EXTERNAL例程的哪个)。

SELECT * 
FROM INFORMATION_SCHEMA.ROUTINES 
WHERE ROUTINE_TYPE = 'FUNCTION'  

这给了我数据库、例程名称以及我需要的一切,除了函数的实际定义。任何人都可以帮助我了解 SQL Server 可能在哪里存储函数的 SQL 吗?

4

2 回答 2

2

您需要使用object_definition系统功能

select specific_name, object_definition(object_id(specific_name))
from INFORMATION_SCHEMA.ROUTINES 
where routine_type = 'FUNCTION' 

更新:好的,所以这不是你想要的。我刚刚错过了您需要定义 CLR 函数而不是标准的用户定义函数。实际上,我有一对可以帮助你的脚本。您可以使用它们获取 CLR 函数的参数,然后组合您需要的脚本

select 
    O.[object_id] as System_Object_ID,
    A.Name as Assembly_Name,
    ASM.assembly_class as Assembly_Class,
    ASM.assembly_method as Assembly_Method,
    case
        when ASM.execute_as_principal_id is null then 'CALLER'
        else PR.name
    end as Execute_As_Name
from sys.assembly_modules as ASM with (nolock)
    inner join sys.assemblies as A with (nolock) on A.[assembly_id] = ASM.[assembly_id]
    inner join sys.objects as O with (nolock) on O.[object_id] = ASM.[object_id]
    left outer join sys.schemas as S with (nolock) on S.[schema_id] = O.[schema_id]
    left outer join sys.database_principals as PR on PR.principal_id = ASM.execute_as_principal_id

select 
    O.[object_id] as System_Object_ID,
    P.name as Parameter_Name,
    T.name as [Type_Name],
    P.precision as [Precision],
    P.scale as [Scale],
    P.max_length as [Max_Length],
    case
        when P.has_default_value = 1 then isnull(cast(P.default_value as nvarchar(max)), 'null')
        else null
    end as Default_Value,
    P.is_output as Is_Output
from sys.assembly_modules as ASM with (nolock)
    inner join sys.parameters as P with (nolock) on P.[object_id] = ASM.[object_id]
    inner join sys.objects as O with (nolock) on O.[object_id] = ASM.[object_id]
    left outer join sys.schemas as S with (nolock) on S.[schema_id] = O.[schema_id]
    inner join sys.types as T with (nolock) on T.system_type_id = P.system_type_id and T.user_type_id = P.user_type_id      

或者,可能更容易,您可以只编写来自 SSMS 的对象的脚本 :)

于 2012-10-26T20:04:07.140 回答
1

这是一个用 .NET 编写的CLR 函数,然后在 SQL Server 中注册。

于 2012-10-26T20:05:42.917 回答