سؤال

I'm working on a reporting module for a company project. Although we use an ORM for our application, I've decided to write stored procedures for the reports in anticipation of migrating to SSRS.

These stored procedures require table-valued parameter input. As such, I've created my table type:

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

I have the following SQL Server stored proc:

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

When I attempt to alter the stored procedure, I continually get the error message

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".

I can't figure out why this is happening. If I do use a scalar type (and update my code to match) it works. However, when I try to use a TVP, I can't compile the stored procedure.

For what it's worth, I've added the type, but I haven't set the permission on it yet. However, I don't expect that would cause a compilation error; it would only cause an error at runtime (which I've dealt with before).

Does anyone have any experience with this issue? Thanks!

هل كانت مفيدة؟

المحلول

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top