SQL 服务器存储过程输入错误

SQL Server stored procedure as input error

我有以下存储过程并创建自定义类型,然后将 table 作为参数提供给我的存储过程以更新我的目标 table:

-- Create Type
CREATE TYPE dbo.e2m_dt AS TABLE
(
    [ID] BIGINT,
    [text] VARCHAR(max)
)

-- Create Procedure:
CREATE PROCEDURE MERGE_REQUEST_RESULT  
    @temp_table dbo.e2m_dt READONLY
AS
    UPDATE Table1
    SET ID = @temp_table.ID,  
        text = @temp_table.text
    FROM Table1
    INNER JOIN @temp_table ON Table1.ID = @temp_table.ID
GO

但我收到以下错误:

Msg 137, Level 16, State 1, Procedure MERGE_REQUEST_RESULT, Line 9
Must declare the scalar variable "@temp_table".

Msg 137, Level 16, State 1, Procedure MERGE_REQUEST_RESULT, Line 10
Must declare the scalar variable "@temp_table".

Msg 137, Level 16, State 1, Procedure MERGE_REQUEST_RESULT, Line 16
Must declare the scalar variable "@temp_table".

我是 SQL 的新手,我们将不胜感激。

ID = @temp_table.ID 将引用标量变量 @temp_Table 而不是 table 类型变量 @temp_table。为您的对象起别名并使用它们:

CREATE PROCEDURE MERGE_REQUEST_RESULT  @temp_table dbo.e2m_dt READONLY
AS
        UPDATE T1
        SET ID = tt.ID,  
            [text] = tt.[text]
        FROM Table1 T1
        INNER JOIN @temp_table tt ON T1.ID = tt.ID;

GO

旁注,我建议不要调用您的 varchar(MAX) 专栏 texttext 是已弃用的数据类型。对象名称坚持使用非关键字。