简体   繁体   English

SQL Server-如何将单行插入临时表?

[英]Sql server - how to insert single row into temporary table?

I have two temporary table, when i do cycle through one table and i get some values from it, in this cycle I need insert new row into another temporary table. 我有两个临时表,当我循环浏览一个表并从中获得一些值时,在这个循环中,我需要将新行插入到另一个临时表中。 Is this possible. 这可能吗。 Here is my sql code and error information: 这是我的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

There is a problem with this lines: 这行有问题:

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

Error: 错误:

Incorrect syntax near the keyword 'EXEC'. 关键字“ EXEC”附近的语法不正确。 Incorrect syntax near ')'. ')'附近的语法不正确。

Some ideas? 有什么想法吗?

It was not possible for me to put these values into #temp2 when it was execute in another SP. 在另一个SP中执行时,我无法将这些值放入#temp2中。 I worked out with three temporary tables, like this: 我制定了三个临时表,如下所示:

BEGIN 开始

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

END GO 结束

It appears that you are trying to insert the result of the following into a #tmp2's ItemGroupNameRoot column: 似乎您正在尝试将以下结果插入#tmp2的ItemGroupNameRoot列中:

EXEC ItemGroup_GetRootWithRecurse @ItemGroupID

You really can't execute stored procedures and use results that way. 您确实无法执行存储过程并以这种方式使用结果。 Instead, I suggest replacing the ItemGroup_GetRootWithRecurse stored procedure with a user defined function. 相反,我建议用用户定义的函数替换ItemGroup_GetRootWithRecurse存储过程。 It might looking something like this: 它可能看起来像这样:

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

You would call the function nearly the same way as the stored procedure. 您将以与存储过程几乎相同的方式调用该函数。 The big difference is you drop the EXEC statement so the call looks like this: 最大的不同是您删除了EXEC语句,因此调用看起来像这样:

dbo.ItemGroup_GetRootWithRecurse_Function (@ItemGroupID)

I hope this helps. 我希望这有帮助。

short answer: 简短答案:
You can share a temp table created in a stored procedure with a stored procedure that it calls. 您可以将在存储过程中创建的临时表与其调用的存储过程共享。 You could try to use this technique to insert into #temp2 within ItemGroup_GetRootWithRecurse. 您可以尝试使用此技术插入ItemGroup_GetRootWithRecurse中的#temp2。

long answer: 长答案:
this may help: How to Share Data Between Stored Procedures . 这可能会有所帮助: 如何在存储过程之间共享数据 You can read the entire article, but the linked section on sharing temp tables between procedures my do the trick. 您可以阅读整篇文章,但是有关在过程之间共享临时表的链接部分却可以解决问题。

you could pass @Manager, @AllQuantity, @AllSumPrice, @AllSumPriceWithVAT, @Profit as well as @ItemGroupID into ItemGroup_GetRootWithRecurse, and within, insert into #tmp2 with something like: 您可以将@ 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 

this code may be a little off, since I don't have al the details, but should be close. 这段代码可能有点过时了,因为我没有其他细节,但是应该很接近。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server:如何插入到临时表? - SQL Server: how to insert to temporary table? 如何从两个临时表中获取数据并插入第三个表而不在 SQL 服务器中添加新行 - How to get data from two temporary tables and insert in third table without adding new row in SQL Server 在 SQL 我创建了一个临时表,我想将多个数据插入到单行中,用逗号分隔 - In SQL I have created a temporary table in that I want to insert multiple data into a single row with comma separated SQL Server-将来自动态查询和变量的结果插入到临时表的同一行中 - SQL Server- Insert results from dynamic query and variable into same row of a temporary table 如何在SQL Server中将数据插入到具有多行到另一行的另一个表中? - How to insert data to another table with multiples rows to a single row in SQL Server? 如何将SQL查询的输出插入临时表? - How to insert the output of a SQL Query into a temporary table? 如何将动态SQL结果插入到临时表中 - How to insert Dynamic SQL Result INTO Temporary Table SQL 服务器 - 从另一台服务器将数据插入临时表 - SQL Server - Insert data into temporary table from another server SQL Server批量插入临时表语法错误 - Sql Server Bulk Insert into temporary table syntax error 在SQL Server中拆分行和列并插入到临时表中? - Split rows and columns in SQL server and insert into temporary table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM