1

我有两个临时表,当我循环遍历一个表并从中获取一些值时,在这个循环中我需要将新行插入另一个临时表。这可能吗。这是我的sql代码和错误信息:

Alter PROCEDURE ProfitReportQ_Search_WithSub 
(@DateFrom datetime,
 @DateTo datetime,
 @DateActive bit,
 @UserID int,
 @ItemGroupIDValues nvarchar(max)
)
AS
BEGIN
CREATE TABLE #tmp(ItemGroupID int, ItemGroupName nvarchar(250), Manager nvarchar(250), AllQuantity int, AllSumPrice AllSumPrice, AllSumPriceWithVAT decimal(18,4), Profit decimal(18,4))
CREATE TABLE #tmp2(Manager nvarchar(250), AllQuantity int, AllSumPrice decimal(18,4), AllSumPriceWithVAT decimal(18,4), Profit decimal(18,4), ItemGroupNameRoot nvarchar(250))


INSERT INTO #tmp
    EXEC ProfitReportQ_Search @DateFrom, @DateTo, @DateActive, @UserID, @ItemGroupIDValues

DECLARE @ItemGroupID int
DECLARE @ItemGroupName nvarchar(250)
DECLARE @Manager nvarchar(250)
DECLARE @AllQuantity int
DECLARE @AllSumPrice decimal(18,4)
DECLARE @AllSumPriceWithVAT decimal(18,4)
DECLARE @Profit decimal(18,4)
DECLARE @ItemGroupNameRoot nvarchar(250)
DECLARE @count int

SET @count = (SELECT COUNT(*) FROM #tmp)
WHILE (@count <> 0)
BEGIN
    SELECT TOP (1) @ItemGroupID = ItemGroupID, @ItemGroupName = ItemGroupName, @Manager = Manager, @AllQuantity = AllQuantity, @AllSumPrice = AllSumPrice, @AllSumPriceWithVAT = AllSumPriceWithVAT, @Profit = Profit FROM #tmp
    DELETE #tmp WHERE ItemGroupID = ItemGroupID AND ItemGroupName = @ItemGroupName AND Manager = @Manager AND AllQuantity = @AllQuantity AND AllSumPrice = @AllSumPrice AND AllSumPriceWithVAT = @AllSumPriceWithVAT AND Profit = @Profit



    INSERT INTO #tmp2 (Manager, AllQuantity, AllSumPrice, AllSumPriceWithVAT, Profit, ItemGroupNameRoot )
        VALUES (@Manager, @AllQuantity, @AllSumPrice, @AllSumPriceWithVAT, @Profit, EXEC ItemGroup_GetRootWithRecurse @ItemGroupID)
END


SELECT ItemGroupNameRoot, Manager, SUM(AllQuantity) AS AllQuantity, SUM(AllSumPrice) AS AllSumPrice, 
        SUM(AllSumPriceWithVAT) AS AllSumPriceWithVAT, SUM(Profit) AS Profit
FROM #tmp2
GROUP BY ItemGroupNameRoot, Manager

DELETE #tmp
DELETE #tmp2

END
GO

这行有问题:

    INSERT INTO #tmp2 (Manager, AllQuantity, AllSumPrice, AllSumPriceWithVAT, Profit, ItemGroupNameRoot )
        VALUES (@Manager, @AllQuantity, @AllSumPrice, @AllSumPriceWithVAT, @Profit, EXEC ItemGroup_GetRootWithRecurse @ItemGroupID)

错误:

关键字“EXEC”附近的语法不正确。')' 附近的语法不正确。

一些想法?

4

3 回答 3

1

在另一个 SP 中执行时,我无法将这些值放入 #temp2。我使用了三个临时表,如下所示:

开始

CREATE TABLE #tmp(ItemGroupID int, ItemGroupName nvarchar(250), 
                    Manager nvarchar(250), AllQuantity int, 
                    AllSumPrice decimal(18,4), AllSumPriceWithVAT decimal(18,4), 
                    Profit decimal(18,4), ID int
                    )
CREATE TABLE #tmp2(Manager nvarchar(250), AllQuantity int, 
                    AllSumPrice decimal(18,4), AllSumPriceWithVAT decimal(18,4), 
                    Profit decimal(18,4), ItemGroupNameRoot nvarchar(250)
                    )

CREATE TABLE #tmp3(ItemGroupNameRoot nvarchar(250))


INSERT INTO #tmp
    EXEC ProfitReportQ_Search_v2 @DateFrom, @DateTo, @DateActive, @UserID, @ItemGroupIDValues

DECLARE @ID int
DECLARE @ItemGroupID int
DECLARE @ItemGroupName nvarchar(250)
DECLARE @Manager nvarchar(250)
DECLARE @AllQuantity int
DECLARE @AllSumPrice decimal(18,4)
DECLARE @AllSumPriceWithVAT decimal(18,4)
DECLARE @Profit decimal(18,4)
DECLARE @ItemGroupNameRoot nvarchar(250)
DECLARE @count int

SET @count = (SELECT COUNT(*) FROM #tmp)
WHILE (@count <> 0)
BEGIN
    SELECT TOP (1) @ItemGroupID = ItemGroupID, @ItemGroupName = ItemGroupName, @Manager = Manager, @AllQuantity = AllQuantity, @AllSumPrice = AllSumPrice, @AllSumPriceWithVAT = AllSumPriceWithVAT, @Profit = Profit, @ID = ID 
    FROM #tmp
    DELETE #tmp WHERE ID = @ID

    INSERT INTO #tmp3 EXEC ItemGroup_GetRootWithRecurse_ForProfitReport @ItemGroupID
    SELECT TOP(1)@ItemGroupNameRoot = ItemGroupNameRoot 
    FROM #tmp3


    INSERT INTO #tmp2 SELECT @Manager, @AllQuantity, 
                    @AllSumPrice, @AllSumPriceWithVAT, 
                    @Profit, @ItemGroupNameRoot
    DELETE #tmp3
    SET @count = (SELECT COUNT(*) FROM #tmp)
END
DELETE #tmp
SELECT ItemGroupNameRoot, Manager, SUM(AllQuantity) AS AllQuantity, SUM(AllSumPrice) AS AllSumPrice, 
        SUM(AllSumPriceWithVAT) AS AllSumPriceWithVAT, SUM(Profit) AS Profit
FROM #tmp2
GROUP BY ItemGroupNameRoot, Manager
DELETE #tmp2

结束

于 2009-08-06T10:04:43.310 回答
0

您似乎正在尝试将以下结果插入 #tmp2 的 ItemGroupNameRoot 列:

EXEC ItemGroup_GetRootWithRecurse @ItemGroupID

您真的无法执行存储过程并以这种方式使用结果。相反,我建议用用户定义的函数替换 ItemGroup_GetRootWithRecurse 存储过程。它可能看起来像这样:

CREATE FUNCTION ItemGroup_GetRootWithRecurse_Function (@ItemGroupID int)
RETURNS varchar(30)
AS
BEGIN
declare @Return varchar(30)
select @return = case @ItemGroupID
when 1 then 'One'
when 2 then 'Two'
else 'Three'
end

return @return
end

您调用该函数的方式几乎与存储过程相同。最大的不同是你删除了 EXEC 语句,所以调用看起来像这样:

dbo.ItemGroup_GetRootWithRecurse_Function (@ItemGroupID)

我希望这有帮助。

于 2009-08-05T06:41:37.937 回答
0

简短回答:
您可以将在存储过程中创建的临时表与其调用的存储过程共享。您可以尝试使用此技术插入 ItemGroup_GetRootWithRecurse 中的#temp2。

长答案:
这可能会有所帮助:如何在存储过程之间共享数据。您可以阅读整篇文章,但有关在过程之间共享临时表的链接部分可以解决问题。

您可以将@Manager、@AllQuantity、@AllSumPrice、@AllSumPriceWithVAT、@Profit 以及 @ItemGroupID 传递到 ItemGroup_GetRootWithRecurse 中,并在其中插入 #tmp2,如下所示:

WITH recurseUp AS 
(
SELECT ItemGroupID, ParentID, ItemGroupName 
FROM dbo.ItemGroup 
WHERE ItemGroupID = @ItemGroupID 

UNION ALL 
SELECT b.ItemGroupID, b.ParentID, b.ItemGroupName 
FROM recurseUp AS a 
INNER JOIN dbo.ItemGroup AS b ON a.ParentID = b.ItemGroupID
) 
INSERT INTO #temp2 (Manager, AllQuantity, AllSumPrice, AllSumPriceWithVAT, Profit, ItemGroupNameRoot )
SELECT @Manager, @AllQuantity, @AllSumPrice, @AllSumPriceWithVAT, @Profit, ItemGroupName 
FROM recurseUp AS recurseUp_1 WHERE (ParentID IS NULL) END 

这段代码可能有点偏离,因为我没有所有细节,但应该很接近。

于 2009-08-05T13:53:25.850 回答