SQL Server:无法从表值函数中执行现有的标量函数

时间:2018-10-09 14:05:06

标签: sql sql-server function sql-server-2017

我创建了这个标量函数,没有任何问题。
但是我不能从另一个函数(内联表值)执行它。

USE [test]
GO

/****** Object:  UserDefinedFunction [dbo].[NameFromEnumerationID]    Script Date: 10/9/2018 6:46:22 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[NameFromEnumerationID]
(
    @EnumerationIDParam INT NOT NULL
)
RETURNS NVARCHAR ( 48 )
WITH    NATIVE_COMPILATION ,
        SCHEMABINDING
AS BEGIN ATOMIC WITH (  TRANSACTION ISOLATION LEVEL = SNAPSHOT      ,
                        LANGUAGE                    = N'English'    )
    RETURN CHOOSE ( @EnumerationIDParam                         ,
                    CAST ( N'ExampleName1' AS NVARCHAR ( 48 ) ) ,
                    CAST ( N'ExampleName2' AS NVARCHAR ( 48 ) ) ,
                    CAST ( N'ExampleName3' AS NVARCHAR ( 48 ) ) ,
                    CAST ( N'ExampleName4' AS NVARCHAR ( 48 ) ) ,
                    CAST ( N'ExampleName5' AS NVARCHAR ( 48 ) ) ,
                    CAST ( N'ExampleName6' AS NVARCHAR ( 48 ) ) ) ;
END
GO

当我尝试创建另一个执行上述功能的功能时,收到错误消息:
'NameFromEnumerationID' is not a recognized built-in function name.

CREATE FUNCTION [dbo].[NamesFromEnumerationIDs]
(
    @EnumerationIDListParam [IntList] NOT NULL READONLY
)
RETURNS TABLE AS RETURN
(
    SELECT  [Value]                                 AS [ID]                     ,
/* ERROR */ NameFromEnumerationID   ( [Value] )     AS [EnumerationName]        ,
    UPPER ( [NameFromEnumerationID] ( [Value] ) )   AS [EnumerationNameUpper]   ,
            [IsEnumerationIDValid]  ( [Value] )     AS [IsValid]
        FROM @EnumerationIDListParam
)
GO

对于第二个函数的参数,IntList类型非常简单:

USE [test]
GO

/****** Object:  UserDefinedTableType [dbo].[IntList]    Script Date: 10/9/2018 6:55:56 AM ******/
CREATE TYPE [dbo].[IntList] AS TABLE(
    [Value] [int] NOT NULL
)
GO

如何在第二个函数中执行第一个函数作为查询的一部分?

1 个答案:

答案 0 :(得分:2)

添加架构名称:dbo.NameFromEnumerationID( [Value] )

dba.stackexchange.com: Why is the schema prefix (dbo) mandatory when we call a function?