SQL Server中的表值参数错误

时间:2013-05-09 17:24:26

标签: sql sql-server stored-procedures table-valued-parameters

我正在为公司项目制作报告模块。虽然我们在应用程序中使用了ORM,但我决定为报告编写存储过程以预期迁移到SSRS。

这些存储过程需要表值参数输入。因此,我创建了我的表类型:

USE MyDatabase
GO

/****** Object:  UserDefinedTableType [dbo].[IntList]    Script Date: 5/8/2013 5:20:59 PM ******/
CREATE TYPE [dbo].[IntList] AS TABLE(
    [Id] [int] NOT NULL,
    PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)
GO

我有以下SQL Server存储过程:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
USE MyDatabase
GO
-- =============================================
-- Author:      <lunchmeat317>
-- Create date: <05/06/2013>
-- Description: <File Type Report>
-- =============================================
ALTER PROCEDURE Report_FileType 
    @filetype varchar(20) = null,
    @User intList READONLY,
    @Group intList READONLY
AS
BEGIN
    SET NOCOUNT ON;

    /*
    lf = LibraryFile
    lfu = LibraryFileAssignedUser
    lfg = LibraryFileAssignedGroup
    */

    SELECT Extension AS FileType, COUNT(1) AS TotalFiles
    FROM LibraryFile lf
    LEFT JOIN LibraryFileAssignedUser lfu
        ON (SELECT COUNT(1) FROM @User) != 0
        AND lfu.LibraryFileId = lf.Id
    LEFT JOIN LibraryFileAssignedGroup lfg
        ON (SELECT COUNT(1) FROM @Group) != 0
        AND lfg.LibraryFileId = lf.Id
    WHERE ((@filetype IS NULL) OR (Extension = @filetype))
    AND (
        ((@User IS NULL) OR (lfu.UserId IN (SELECT * FROM @User)))
        OR ((@Group IS NULL) OR (lfg.HubGroupId IN (SELECT * FROM @Group)))
    )
    GROUP BY Extension
END
GO

当我尝试更改存储过程时,我不断收到错误消息

Msg 137, Level 16, State 1, Procedure Report_FileType
Must declare the scalar variable "@User".
Msg 137, Level 16, State 1, Procedure Report_FileType
Must declare the scalar variable "@Group".

我无法弄清楚为什么会这样。如果我使用标量类型(并更新我的代码以匹配)它可以工作。但是,当我尝试使用TVP时,我无法编译存储过程。

为了它的价值,我添加了类型,但我还没有设置它的权限。但是,我不认为这会导致编译错误;它只会在运行时导致错误(我之前已经处理过)。

有没有人对此问题有任何经验?谢谢!

1 个答案:

答案 0 :(得分:1)