表值必须声明标量变量

时间:2018-02-14 05:12:03

标签: sql sql-server stored-procedures table-valued-parameters

我有一个接受表值参数的存储过程。

以下是代码:

ALTER PROCEDURE [dbo].[ConsolidateInspection]
    @TblConsolidation DTProductCodePO READONLY
AS
BEGIN
    BEGIN TRANSACTION
        DECLARE @CurrentFormNo AS BIGINT = 0

        INSERT INTO [InspectionDB].[dbo].[FormHeader] 
                      (PONumber, POSuffix, [Productcode], [Stratum], [SpecNumber], [Engineer],
                       [ProductManager], [CountryCode], [SupplierCode], [Supplier],
                       [QTY], [EAGTIN], [INGTIN], [OUGTIN], [InspectionType], [InspectorName])
            SELECT 
                @CurrentFormNo = P.FormNo   
            FROM 
                FormHeader P 
            INNER JOIN 
               @TblConsolidation T ON P.PONumber = T.PO 
                                   AND P.POSuffix = T.POSuffix 
                                   AND P.Productcode = T.ProductCode
                                   AND P.CountryCode = T.CountryCode
           ORDER BY 
               PO, P.POSuffix, P.Productcode


        COMMIT TRANSACTION
    END

我正在尝试执行上面的存储过程:

exec ConsolidateInspection @TblConsolidation dbo.Temp

dbo.Temp是具有相同属性的表的名称

我试过

exec ConsolidateInspection @TblConsolidation Select * from Temp

这是一个错误说

  

必须声明标量变量" @ TblConsolidation"

任何帮助?

1 个答案:

答案 0 :(得分:2)

dbo.Temp与DTProductCodePO不同! DTProductCodePO是一个表类型,您应该定义它的变量然后填充它,然后将它作为这样的参数:

Declare @Tbl DTProductCodePO

Insert into @Tbl(your fields here ...) 
SELECT ...SAME FIELD HERE... from dbo.Temp

exec ConsolidateInspection @Tbl