在具有变量表名称的存储过程中使用来自参数化查询的值

时间:2016-12-20 16:03:49

标签: sql stored-procedures parameter-passing dynamic-sql

我有一个存储过程,用于为特定客户帐户建立序列号。一个简单的表存储所有客户端的最后一个发布值,SP将只接受SupplyChainID的参数以及需要多少个数。

GetItemIDs

因此,SupplyChainID 12345可能有5个作为其最后发布的号码,因此正在运行

GetItemIDs 12345,200

将返回5,然后将值设置为205以供下次使用

所以当前的SP做了这样的事情,因为它必须检查它不超过99,999,999。 (如果它会结束,则回到0)

IF (SELECT EIBItemID FROM ItemIDGeneration WHERE SupplyChainID = @SCID) + @DocumentCount > 99999999
    BEGIN
        SELECT 0
        UPDATE dbo.ItemIDGeneration SET EIBItemID = @DocumentCount,
                                        LastIssuedDate = getdate() WHERE SupplyChainID = @SCID      
END
ELSE
    BEGIN
        SELECT EIBItemID FROM ItemIDGeneration WHERE SupplyChainID = @SCID  
        UPDATE dbo.ItemIDGeneration SET EIBItemID = EIBItemID + @DocumentCount,
                                        LastIssuedDate = getdate() WHERE SupplyChainID = @SCID
END

我需要添加的是检查另一个表以查看它是否具有更高的值,并且在这种情况下使用该表而不是ItemGeneration表中最后发布的数字

这样的事情:

 SELECT @HI_ID = MAX(EIBItemID) from 
 (SELECT EIBItemID FROM ItemIDGeneration WHERE SupplyChainID = @SCID 
 UNION ALL 
 SELECT ISNULL(MAX(EIBItemID),0) AS EIBItemID FROM dbo.SupplyChainID_'+ @tablename) as bigint 
    IF (@HI_ID) + @DocumentCount > 99999999
        BEGIN
            SELECT 0
            UPDATE dbo.ItemIDGeneration SET EIBItemID=@DocumentCount,LastIssuedDate = getdate() WHERE SupplyChainID = @SCID         END
    ELSE
            BEGIN
        SELECT @HI_ID--EIBItemID FROM ItemIDGeneration WHERE SupplyChainID = @SCID  
        UPDATE dbo.ItemIDGeneration SET EIBItemID = @HI_ID +@DocumentCount,LastIssuedDate = getdate() WHERE SupplyChainID = @SCID
    END

正如您所看到的,该表具有相同的SupplyChainID作为表名的一部分,因此我知道我需要将其作为查询的一部分传递并执行它但我无法使以下工作

 SET @tablename = 'dbo.SupplyChain_'+@SCID
 SET @tablequery = N'SELECT ISNULL(MAX(EIBItemID),0) AS EIBItemID FROM ' + @tablename

 execute  @tableID = sp_executesql @tablequery

 SELECT @HI_ID =    MAX(EIBItemID) from 
 (SELECT EIBItemID FROM ItemIDGeneration WHERE SupplyChainID = @SCID 
 union all 
 SELECT @tableID) as bigint 
IF (@HI_ID) + @DocumentCount > 99999999

该过程没有错误,但我认为我的语法有问题,就像我运行以下

时一样
  declare @return int
    exec @return = GetItemIDs 8001377,20
    select @return as result

我看到EIBItemID作为dbo.SupplyChain_xxxxx表中的正确结果返回,但sp结尾的返回值是基于ItemIDGeneration表的回复(返回200)。

我认为在调用exec但在@tableid中存储0时,它会明显返回正确的结果

2 个答案:

答案 0 :(得分:1)

您可以尝试此操作来获取值:

DECLARE @SCID INT = 1
DECLARE @TableName NVARCHAR(100) = N'dbo.SupplyChain_' + CAST(@SCID AS NVARCHAR(10))
DECLARE @TableID BIGINT
DECLARE @TableQuery NVARCHAR(1000) = N'SELECT @TableID = ISNULL(MAX(EIBItemID), 0) FROM ' + @TableName

EXEC sp_ExecuteSql  @TableQuery, N'@TableID BIGINT OUTPUT', @TableID OUTPUT

PRINT @TableID

答案 1 :(得分:0)

感谢所有回答,我使用了Alex的一个小调整建议

DECLARE @HI_ID as bigint
DECLARE @TableName NVARCHAR(100) = N'dbo.SupplyChain_' + CAST(@SCID AS NVARCHAR(10))
DECLARE @TableID BIGINT
DECLARE @TableQuery NVARCHAR(1000) = N'SELECT @TableID = ISNULL(MAX(EIBItemID),0) FROM ' + @TableName

EXEC sp_ExecuteSql  @TableQuery, N'@TableID BIGINT OUTPUT', @TableID OUTPUT

SELECT @TableID As EIBItemID