0

我创建了一个TVF和一个Scalar在我正在创建SQL Server 2008的内部使用。View该视图似乎无法查看或使用该功能,我不确定我的错误是基于权限还是完全不可能TVF从 a 中调用 a View

我在 SSMS 中遇到的错误是Invalid Object Name dbo.TwoYearColumnsWithNulls'

这是TVF我创建的:

Create FUNCTION [dbo].[TwoYearColumnsWithNulls]()
RETURNS @table TABLE( cols nvarchar(max))
AS
BEGIN
    DECLARE @cols AS NVARCHAR(MAX), @startdate as varchar(max),@enddate as varchar(max);
    Set @startdate = cast((DATEPART(yyyy, GetDate())-1) as varchar(4))+'-01-01';
    Set @enddate = cast((DATEPART(yyyy, GetDate())) as varchar(4))+'-12-24';    

    WITH DateRange(dt) AS
            (
                SELECT CONVERT(datetime, @startdate) dt
                UNION ALL
                SELECT DATEADD(ww,1,dt) dt FROM DateRange WHERE dt < CONVERT(datetime, @enddate)
            )
    INSERT INTO @table (cols) SELECT STUFF((SELECT ',' + 'isnull('+QUOTENAME((cast(DATEPART(yyyy, dt) as varchar(4)))+'-Week'+(cast(DATEPART(ww, dt) as varchar(2))))+', 0)' FROM DateRange
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')
        OPTION (maxrecursion 0);

    RETURN
END
GO

这是View我试图在其中使用它的一小部分。视图中也有一个PIVOT,不确定这是否重要?

CREATE VIEW [testdta].[TwoYearInventoryIssues]
AS
SELECT LIITM as ShortItemNumber, IMLITM as LongItemNumber, (SELECT cols FROM dbo.TwoYearColumnsWithNulls())
FROM
    (
        SELECT LIITM, IMLITM,.......
<SNIP>

编辑

根据下面的评论,我确实尝试使用Scalar函数和 aTVF来实现相同的结果。两者都没有提供相同的错误。如果有帮助,这是Scalar我尝试过的功能:

Create FUNCTION dbo.TwoYearColumnsWithNullsScalar()
RETURNS nvarchar(max)
AS
BEGIN
    DECLARE @isnullcols AS NVARCHAR(MAX), @startdate as varchar(max),@enddate as varchar(max);
    Set @startdate = cast((DATEPART(yyyy, GetDate())-1) as varchar(4))+'-01-01';
    Set @enddate = cast((DATEPART(yyyy, GetDate())) as varchar(4))+'-12-24';    

    WITH DateRange(dt) AS
            (
                SELECT CONVERT(datetime, @startdate) dt
                UNION ALL
                SELECT DATEADD(ww,1,dt) dt FROM DateRange WHERE dt < CONVERT(datetime, @enddate)
            )
    SELECT @isnullcols = STUFF((SELECT ',' + 'isnull('+QUOTENAME((cast(DATEPART(yyyy, dt) as varchar(4)))+'-Week'+(cast(DATEPART(ww, dt) as varchar(2))))+', 0)' FROM DateRange
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')
        OPTION (maxrecursion 0);

    RETURN  @isnullcols
END
4

0 回答 0