存储过程中的表类型参数导致操作数类型冲突错误

时间:2013-09-24 08:50:23

标签: sql-server-2008 stored-procedures user-defined-types

我想提供一个标识符数组作为存储过程的参数。

存储过程如下所示:

ALTER PROCEDURE [dbo].[SearchPerson]
    @personType INT = NULL,
    @city NVARCHAR(64) = NULL,
    @siteIds IntegerList READONLY,
    -- some other params...
AS
    SELECT
        -- some fields...
    FROM dbo.PersonView AS pv
    WHERE
    (
        (@personType IS NULL OR pv.PersonType = @personType) AND
        (@city IS NULL OR pv.City LIKE '%' + @city + '%') AND
        (pv.SiteId in (SELECT si.Value FROM @siteIds AS si)) AND
        -- some other params filter...
    )

用户表类型如下:

CREATE TYPE [dbo].[IntegerList] AS TABLE(
    [Value] [int] NULL
)

当我从SSMS中的脚本调用存储过程时(我最初从.NET代码调用它时遇到同样的问题):

DECLARE @siteIds AS IntegerList,
@personType AS INT = 1
INSERT INTO @siteIds VALUES (1)
EXEC [dbo].[SearchPerson] @personType, @siteIds

我收到了错误:

  

操作数类型碰撞:int与IntegerList

不兼容

2 个答案:

答案 0 :(得分:11)

我找到了答案:这是导致错误的表类型参数的顺序!

表类型参数必须是存储过程参数中的第一个参数,并且在传递给存储过程调用的参数中也是如此!

存储过程:

ALTER PROCEDURE [dbo].[SearchPerson]
    @siteIds IntegerList READONLY, -- THIS PARAMETER HAS TO BE THE FIRST !
    @personType INT = NULL,
    @city NVARCHAR(64) = NULL,
    -- some other params...
AS
    SELECT
        -- some fields...
    FROM dbo.PersonView AS pv
    WHERE
    (
        (@personType IS NULL OR pv.PersonType = @personType) AND
        (@city IS NULL OR pv.City LIKE '%' + @city + '%') AND
        (pv.SiteId in (SELECT si.Value FROM @siteIds AS si)) AND
        -- some other params filter...
    )

电话:

DECLARE @siteIds AS IntegerList,
@personType AS INT = 1
INSERT INTO @siteIds VALUES (1)
EXEC [dbo].[SearchPerson] @siteIds, @personType -- PUT @siteIds FIRST !

sql server bug还是我错过了什么?

答案 1 :(得分:2)

const uint16_t LAYERS_RIGHT[TOTAL_LAYERS][NBROW][NBCOL] = {
  {{c00, c10, c20, c30, c40, c50, c60},
   {c01, c11, c21, c31, c41, c51, c61},
   {c02, c12, c22, c32, c42, c52, c62},
   {c03, c13, c23, c33, c43, c53, c63},
   {c04, c14, c24, c34, c44, c54, c64},
   {c05, c15, c25, c35, c45, c55, c65}}
   /* blah blah */
};
const uint16_t LAYERS_LEFT[TOTAL_LAYERS][NBROW][NBCOL] = {
   /* blah blah */
   /* blah blah */
};

uint16_t *(LAYERS[TOTAL_LAYERS][NBROW][NBCOL]);

SQL SERVER 2012 - 表类型参数的位置无关紧要,你必须在传递数据时确保顺序,你可以检查上面的表格类型参数最后工作正常的代码。