2013-11-14 99 views
3

我不明白我在這裏做錯了。從更新部分,我收到此錯誤:錯誤必須聲明標量變量與表值參數

Must declare the scalar variable "@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 
+0

@OrderTestTVP TVP_OrderTest READONLY缺少類型聲明 - 對嗎? –

+0

我有這種類型的CREATE TYPE [dbo]。[TVP_OrderTest] AS TABLE()。事實上,當我只保留「插入」細分時,它不會引發任何錯誤 –

回答

10

我說你必須要使用的連接操作一個表的別名:

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

我現在沒有SQL,現在嘗試,但如果我記得正確,表變量需要別名,如果在表達式中使用。