【问题标题】:How to pass udtt into a stored procedure in SQL Server Management Studio如何将 udtt 传递到 SQL Server Management Studio 中的存储过程
【发布时间】:2011-12-09 00:40:36
【问题描述】:

我有一个具有以下签名的 SP prc_Foo_Delete:

ALTER PROCEDURE [prc_Foo_Delete]
    @fooIds [int_udtt] READONLY,
    @deleteReason int,
    @comment nvarchar(512),
    @deletedBy nvarchar(128)

int_udtt 定义为:

CREATE TYPE [int_udtt] AS TABLE(
    [Id] [int] NOT NULL,
    PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)

我尝试使用以下脚本在 Management Studio 中调用此 SP:

DECLARE @return_value int
EXEC    @return_value = [prc_Foo_Delete]
        @fooIds = 3,
        @deleteReason = 2,
        @comment = N'asfdasdf',
        @deletedBy = N'asdfa'

SELECT  'Return Value' = @return_value
GO

我得到的错误是:操作数类型冲突:int 与 int_udtt 不兼容。如何传入 int 或 int 列表以在此工具中调用(我知道如何在代码中执行此操作,但在 Management Studio 中不知道)。

【问题讨论】:

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


    【解决方案1】:

    由于您已将用户定义类型定义为存储过程的参数,因此在调用存储过程时也需要使用该用户定义类型!你不能只发送一个INT 来代替......

    试试这样的:

    -- define an instance of your user-defined table type
    DECLARE @IDs [int_udtt]
    
    -- fill some values into that table
    INSERT INTO @IDs VALUES(3), (5), (17), (42)
    
    -- call your stored proc
    DECLARE @return_value int
    EXEC    @return_value = [prc_Foo_Delete]
            @fooIds = @IDs,   -- pass in that UDT table type here!
            @deleteReason = 2,
            @comment = N'asfdasdf',
            @deletedBy = N'asdfa'
    
    SELECT  'Return Value' = @return_value
    GO
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      相关资源
      最近更新 更多