如何声明标量表类型?

时间:2015-03-12 16:05:52

标签: sql-server stored-procedures

我有这样的存储过程;

ALTER PROCEDURE [dbo].[crudValues]
    @tblValue as dbo.tblValue READONLY
AS
BEGIN
    SET NOCOUNT ON;
    UPDATE Value SET Value.Value = @tblValue.Value, Value.LastUpdate = @tblValue.LastUpdate FROM Value INNER JOIN @tblValue ON Value.ID = @tblValue.ID
END

当我执行此存储过程时,它给出了错误

Msg 137, Level 16, State 1, Procedure crudValues, Line 21
Must declare the scalar variable "@tblValue".
Msg 137, Level 16, State 1, Procedure crudValues, Line 21
Must declare the scalar variable "@tblValue".
Msg 137, Level 16, State 1, Procedure crudValues, Line 21
Must declare the scalar variable "@tblValue".

如何声明标量表类型。或者我该如何解决这个错误?

2 个答案:

答案 0 :(得分:1)

将表变量括在双引号或括号中(但不在连接中)或在查询中使用表变量的别名:

ALTER PROCEDURE [dbo].[crudValues]
    @tblValue dbo.tblValue READONLY
AS
BEGIN
    SET NOCOUNT ON;
    UPDATE Value 
    SET Value.Value = t.Value, Value.LastUpdate = t.LastUpdate 
    FROM Value INNER JOIN @tblValue t ON Value.ID = t.ID
END

带引号标识符的备选方案如下:[@tblValue].ID"@tblValue".ID

答案 1 :(得分:0)

您需要为表格添加别名。此代码产生相同的错误:

DECLARE @Table TABLE
(
        Field INT
)

SELECT 
    @Table.Field
FROM @Table