如何使用存储过程将数据存储在临时表上?

Posted

技术标签:

【中文标题】如何使用存储过程将数据存储在临时表上?【英文标题】:How to store data on a temp table using Stored procedure? 【发布时间】:2014-01-07 07:34:24 【问题描述】:

我正在尝试基于下面的标量函数编写存储过程,以显示每年和每种产品有多少客户?如果产品没有客户 那么它应该在结果中为零的客户数量,所以它应该显示所有产品。然后我想将它插入临时表。这是我迄今为止尝试过的,但对于 SP 中的某些点,我不知道我必须做什么才能得到我的结果。我猜它卡在一个循环中:( 有什么想法吗??

标量:

CREATE FUNCTION NumOfCustomers
(
    @year INT,
    @productId INT,
    @typeOfCustomer NVARCHAR
)
RETURNS TABLE AS 
RETURN
(
SELECT COUNT(DISTINCT Sales.Customer.CustomerID) AS number 

FROM    Sales.SalesOrderDetail SOD 
        INNER JOIN Sales.SalesOrderHeader SOH ON SOD.SalesOrderID = SOH.SalesOrderID
        INNER JOIN Sales.Customer ON Customer.CustomerID = SOH.CustomerID

WHERE
    SOD.ProductID = @productID 
    AND YEAR(SOH.OrderDate) = @year
    AND Sales.Customer.CustomerType = @typeOfCustomer
)
GO   

存储过程:

CREATE PROCEDURE MYsp AS 
BEGIN
    DECLARE @year INT, @count INT
    DECLARE yearCursor CURSOR FOR
    SELECT DISTINCT YEAR(SOH.OrderDate) FROM Sales.SalesOrderHeader SOH
    --ORDER BY YEAR DESC
    OPEN yearCursor
    FETCH NEXT FROM  yearCursor INTO @year
    --** Starting the loop in curser and filling the data
    SET @count = 1
    WHILE(@@FETCH_STATUS = 0)
    BEGIN

            DECLARE @prodID INT
            DECLARE @NumOfCustomers INT
            DECLARE prodCursor CURSOR FOR
            SELECT P.ProductID FROM Production.Product P
            OPEN prodCursor
            FETCH NEXT FROM  prodCursor INTO @prodID
            SET @count = 1
            WHILE(@@FETCH_STATUS = 0)
            BEGIN   
                SELECT @NumOfCustomers = NUM.number FROM NumOfCustomers(@year, @prodID, 's') NUM
                INSERT #temp (Year,ProductID,NumOfCustomers) VALUES (@year,@prodID,@NumOfCustomers)

                SET @count = @count+1
                FETCH NEXT FROM prodCursor INTO @prodID
            END
            CLOSE prodCursor
            DEALLOCATE prodCursor

        FETCH NEXT FROM yearCursor INTO @year
    END
    CLOSE yearCursor
    DEALLOCATE yearCursor
END
GO

然后我想执行它并从该临时表中获取数据:

EXEC MYsp
SELECT * FROM #temp

【问题讨论】:

【参考方案1】:

在过程中使用 select 语句:

   CREATE PROCEDURE MYsp AS 
    BEGIN
        DECLARE @year INT, @count INT
        DECLARE yearCursor CURSOR FOR
        SELECT DISTINCT YEAR(SOH.OrderDate) FROM Sales.SalesOrderHeader SOH
        --ORDER BY YEAR DESC
        OPEN yearCursor
        FETCH NEXT FROM  yearCursor INTO @year
        --** Starting the loop in curser and filling the data
        SET @count = 1
        WHILE(@@FETCH_STATUS = 0)
        BEGIN

                DECLARE @prodID INT
                DECLARE @NumOfCustomers INT
                DECLARE prodCursor CURSOR FOR
                SELECT P.ProductID FROM Production.Product P
                OPEN prodCursor
                FETCH NEXT FROM  prodCursor INTO @prodID
                SET @count = 1
                WHILE(@@FETCH_STATUS = 0)
                BEGIN   
                    SELECT @NumOfCustomers = NUM.number FROM NumOfCustomers(@year, @prodID, 's') NUM
                    INSERT #temp (Year,ProductID,NumOfCustomers) VALUES (@year,@prodID,@NumOfCustomers)

                    SET @count = @count+1
                    FETCH NEXT FROM prodCursor INTO @prodID
                END
                CLOSE prodCursor
                DEALLOCATE prodCursor

            FETCH NEXT FROM yearCursor INTO @year
        END
        CLOSE yearCursor
        DEALLOCATE yearCursor
    Select *
     from #temp
    END

GO

现在执行程序:

EXEC MYsp

【讨论】:

Aziz 它不会在临时表中插入任何内容...!错误是:过程 MYsp 没有提供参数和参数。

以上是关于如何使用存储过程将数据存储在临时表上?的主要内容,如果未能解决你的问题,请参考以下文章

从存储过程结果集中在表上插入/更新数据

如何从存储过程返回的游标将数据插入临时表

如何在Oracle中使用Java存储过程

如何在带有 LINQ to SQL 的存储过程中使用临时表

如何在没有临时表的情况下从另一个存储过程调用存储过程(带参数)

临时表作为存储过程中的输出参数