【问题标题】:Error Must declare the scalar variable with Table Valued Parameter错误必须使用表值参数声明标量变量
【发布时间】:2013-11-14 22:15:13
【问题描述】:

我没有明白我在这里做错了什么。从更新部分,我收到此错误:

必须声明标量变量“@OrderTestTVP”。

这是我的表值参数类型

CREATE TYPE [dbo].[TVP_OrderTest] AS TABLE(
    [OrderTestId] [int] NULL,
    [OrderId] [int] NOT NULL,
    [TestCode] [varchar](10) NOT NULL
    )
GO

ALTER PROCEDURE [dbo].[TVP_Save_OrderTest]
    @OrderTestTVP TVP_OrderTest READONLY
AS
BEGIN
    BEGIN TRY
        DECLARE @TableOfIdentities TABLE (IdentValue BIGINT, TestCode varchar(10) )
        INSERT INTO OrderTest
                (
                    OrderId
                    ,TestCode
                ) 
                OUTPUT Inserted.OrderTestId,inserted.TestCode 
                INTO @TableOfIdentities(IdentValue,TestCode)
                   SELECT  OrderId
                    ,TestCode
            FROM @OrderTestTVP
            WHERE OrdertestID = 0

        UPDATE 
                OrderTest
                SET   
                    OrderId = @OrderTestTVP.OrderId,
                    TestCode = @OrderTestTVP.TestCode
                    FROM OrderTest INNER JOIN @OrderTestTVP
                  ON OrderTest.OrderTestId = @OrderTestTVP.OrderTestId

        SELECT * FROM @TableOfIdentities
    END TRY
    BEGIN CATCH
        exec error_catch
    END CATCH
END

【问题讨论】:

  • @OrderTestTVP TVP_OrderTest READONLY 缺少类型声明 - 对吧?
  • 我有那个类型 CREATE TYPE [dbo].[TVP_OrderTest] AS TABLE()。事实上,当我只保留“插入”段时它不会抛出任何错误

标签: c# sql-server


【解决方案1】:

我会说你必须为连接操作使用表别名:

UPDATE 
 OrderTest
 SET   
     OrderId = o.OrderId,
     TestCode = o.TestCode
 FROM OrderTest INNER JOIN @OrderTestTVP o
     ON OrderTest.OrderTestId = o.OrderTestId

我现在手头没有可尝试的 SQL,但如果我没记错的话,表变量在表达式中使用时需要别名。

【讨论】:

    猜你喜欢
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    相关资源
    最近更新 更多