返回SQL Server存储过程中的最后一个选定结果集

时间:2017-01-12 08:08:52

标签: sql stored-procedures sql-server-2012

我只需要一个结果选择表作为存储过程的输出,以便在节点红流中将其传递给另一个函数。因为我有两个选择(第一个用于游标迭代,它还为存储过程产生了一个out,我真的从存储过程输出中删除了)。我已经坚持了3天。

CREATE PROCEDURE [dbo].[GetOrderStartingLevel] 
    @joborder nvarchar(100)
AS
BEGIN
    SET NOCOUNT ON;

    IF OBJECT_ID('tempdb.dbo.#TempTable', 'U') IS NOT NULL
        DROP TABLE #TempTable;

    IF OBJECT_ID('tempdb.dbo.#Degerler', 'U') IS NOT NULL
        DROP TABLE #Degerler;

    CREATE TABLE #TempTable 
    (
        siparis nvarchar(100),
        TotalCopies int,
        GoogCopies int,
        PoweredTime int,
        zaman datetime
    )

    CREATE TABLE #Values
    (
        siparis nvarchar(100),
        zaman datetime,
        totalcopies int, 
        googcopies int,
        PoweredTime int,
        GoodCopiesTime int, 
        ChangingTime int,
        Preparation int
    )

    INSERT INTO #TempTable
        SELECT
            [siparis], [TotalCopies], [GoogCopies],
            PoweredTime, zaman    
        FROM 
            [KurumsalZEKA].[dbo].[GunlukUretim]
        WHERE 
            siparis LIKE @joborder

    DECLARE @zaman datetime
    DECLARE @olddate datetime
    DECLARE @totalcopies int
    DECLARE @currentcopies int
    DECLARE @poweredtime int
    DECLARE @currentpoweredtime int

    SET @currentcopies = 0

    SELECT * 
    FROM #TempTable

    DECLARE cur CURSOR FOR 
        SELECT 
            zaman, totalcopies, PoweredTime 
        FROM 
            #TempTable 
        ORDER BY
            zaman ASC

    OPEN cur

    FETCH NEXT FROM cur INTO @zaman, @totalcopies, @poweredtime

    WHILE @@FETCH_STATUS = 0 
    BEGIN
        IF (@totalcopies - @currentcopies < 0) OR 
           (@currentpoweredtime - @poweredtime > 0)
            INSERT INTO #Values 
                SELECT
                    siparis, zaman, totalcopies, 
                    googcopies, PoweredTime, GoodCopiesTime, 
                    ChangingTime, Preparation 
                FROM 
                    dbo.GunlukUretim
                WHERE
                    zaman = @olddate;

        SET @currentcopies = @totalcopies
        SET @olddate = @zaman
        SET @currentpoweredtime = @poweredtime

        FETCH NEXT FROM cur INTO @zaman, @totalcopies, @poweredtime
    END

    CLOSE cur    
    DEALLOCATE cur

    INSERT INTO #Values  -- minimum deger
        SELECT TOP 1 
            siparis, zaman, totalcopies * (-1),
            googcopies * (-1),
            PoweredTime * (-1),
            GoodCopiesTime * (-1),
            ChangingTime * (-1),
            Preparation * (-1)  
        FROM
            dbo.GunlukUretim
        WHERE
            siparis LIKE @joborder 
        ORDER BY
            zaman

    INSERT INTO #Values   -- maximum deger
        SELECT TOP 1 
            siparis, zaman, totalcopies,
            googcopies, PoweredTime, GoodCopiesTime,
            ChangingTime, Preparation  
        FROM
            dbo.GunlukUretim
        WHERE
            siparis LIKE @joborder 
        ORDER BY 
            zaman DESC

    --select * from #Degerler
    IF OBJECT_ID('tempdb.dbo.#Degerler', 'U') IS NOT NULL
        SELECT
            MIN(zaman) AS start,
            MAX(zaman) AS finish,
            SUM(totalcopies) AS totalcopies,
            SUM(googcopies) AS goodcopies,
            SUM(PoweredTime) AS poweredtime,
            SUM(GoodCopiesTime) AS goodcopiestime,
            SUM(ChangingTime) AS changingtime, 
            SUM(Preparation) AS preparation 
        FROM 
            #Values 
        GROUP BY
            siparis;
END

0 个答案:

没有答案