2

我在 SQL SP 中有这段代码。

当我测试@OUTSTANDINGSDATA 是否不为空时,我收到“必须声明标量变量“@OUTSTANDINGSDATA””错误。其他变量不会发生这种情况。有任何想法吗?提前谢谢

USE [PantaRei]
GO
/****** Object:  StoredProcedure [dbo].[InsertDdrsData]    Script Date: 08/22/2012 14:29:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[InsertDdrsData] 
@RECEIVABLESDATA RECEIVABLESTABLE READONLY,
@DILUTIONSDATA DILUTIONSTABLE READONLY,
@ACCOUNTABLESDATA ACCOUNTABLESTABLE READONLY,
@OUTSTANDINGSDATA OUTSTANDINGSTABLE READONLY,
@TABLE1 VARCHAR(MAX),
@TABLE2 VARCHAR(MAX),
@TABLE3 VARCHAR(MAX),
@TABLE4 VARCHAR(MAX)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    DECLARE @CMD nvarchar(345)

    SET NOCOUNT ON

    SET @TABLE1 = RTRIM(@TABLE1)
    SET @TABLE2 = RTRIM(@TABLE2)
    SET @TABLE3 = RTRIM(@TABLE3)

    SELECT * INTO #t1 FROM @RECEIVABLESDATA

    SET @CMD =

        'INSERT INTO ' + QUOTENAME(@TABLE1) +
        'SELECT * FROM #t1'

    EXECUTE sp_executesql @CMD

    SELECT * INTO #t2 FROM @DILUTIONSDATA

    SET @CMD =

        'INSERT INTO ' + QUOTENAME(@TABLE2) +
        'SELECT * FROM #t2'

    EXECUTE sp_executesql @CMD

    SELECT * INTO #t3 FROM @ACCOUNTABLESDATA

    SET @CMD =

        'INSERT INTO ' + QUOTENAME(@TABLE3) +
        'SELECT * FROM #t3'

    EXECUTE sp_executesql @CMD

    IF @OUTSTANDINGSDATA IS NOT NULL
        BEGIN
            SELECT * INTO #t4 FROM @OUTSTANDINGSDATA

            SET @CMD =

                'INSERT INTO ' + QUOTENAME(@TABLE4) +
                'SELECT * FROM #t4'

            EXECUTE sp_executesql @CMD

        END 
END
4

2 回答 2

4

@OUTSTANDINGSDATA是一个表值参数。

NULL传入也不行。改为测试空表。

IF NOT EXISTS (SELECT * FROM @OUTSTANDINGSDATA)
于 2012-08-22T13:03:23.953 回答
0

NULL作为表值参数的值传递是无效的。

也许您需要检查它是否为空?


例如:

CREATE TYPE LocationTableType AS TABLE 
( LocationName VARCHAR(50)
, CostRate INT );
GO
CREATE PROCEDURE dbo.usp_InsertProductionLocation
    @TVP LocationTableType READONLY
    AS 
    SET NOCOUNT ON
    SELECT *
        FROM  @TVP;
GO
EXEC dbo.usp_InsertProductionLocation NULL

产生错误:

Msg 206, Level 16, State 2, Procedure usp_InsertProductionLocation, Line 0
Operand type clash: NULL is incompatible with LocationTableType

糟糕 - 根据 Martin 的评论,您不必提供值作为参数 - 但它仍然不会NULL在存储过程中。将自动提供表的空实例。

于 2012-08-22T13:07:32.547 回答