Sql return generation limit

Asked

Viewed 47 times

0

I have this code, which generates number of periods, but I take this return to excel, I can define the amount of returns? I could only bring five, returns, I can limit? inserir a descrição da imagem aqui

DECLARE @serie INT, @ordem INT, @Periodo INT DECLARE @tbResultado TABLE ( id INT IDENTITY(1,1) ,IdCliente INT, Serie INT, Ordem INT, idPerido INT)

SET @serie = 9059 SET @ordem = 05288 SET @Periodo = 0



WHILE (@Periodo <= 670) BEGIN   IF (@serie < (SELECT nmSerieIni FROM tbSysPeriodo where id = @Periodo)) 
            WHILE (@serie NOT BETWEEN (SELECT TOP 1 nmSerieIni FROM tbSysPeriodo where id = @Periodo) AND (SELECT TOP 1 nmSerieFim FROM tbSysPeriodo where id = @Periodo))      BEGIN
                SET @serie = @serie + (select ABS(nmSerieIni - nmSerieFim)+1 from tbSysPeriodo where id = @Periodo)

        END

    IF (@serie > (SELECT TOP 1 nmSerieFim FROM tbSysPeriodo where id = @Periodo)) 
            WHILE (@serie NOT BETWEEN (SELECT TOP 1 nmSerieIni FROM tbSysPeriodo where id = @Periodo) AND (SELECT TOP 1 nmSerieFim FROM tbSysPeriodo where id = @Periodo))      BEGIN
                SET @serie = @serie - (select ABS(nmSerieIni - nmSerieFim)+1 from tbSysPeriodo where id = @Periodo)

        END
            INSERT INTO @tbResultado    SELECT TOP 10       S.idCliente,        S.nmSerie,          S.nmOrdem,      P.idPeriodo     FROM tbSorte S      JOIN tbSortePeriodo P       ON S.id = P.idSorte     WHERE S.nmSerie = @serie AND S.nmOrdem NOT IN (SELECT ORDEM FROM @tbResultado) AND P.idPeriodo = @Periodo   ORDER BY nmSerie DESC, ABS(nmOrdem - @ordem)


WHILE (@serie NOT BETWEEN (SELECT TOP 1 nmSerieIni FROM tbSysPeriodo where id = @Periodo) AND (SELECT TOP 1 nmSerieFim FROM tbSysPeriodo where id = @Periodo)) BEGIN

    SET @serie = ABS(@serie-(SELECT TOP 1  nmSerie FROM tbSorte S JOIN tbSortePeriodo P ON S.id = P.idSorte WHERE P.idPeriodo = @Periodo AND S.nmSerie NOT IN (SELECT Serie FROM @tbResultado)))

END

INSERT INTO @tbResultado    SELECT TOP 10       S.idCliente,        S.nmSerie,          S.nmOrdem,      P.idPeriodo     FROM tbSorte S      JOIN tbSortePeriodo P       ON S.id = P.idSorte     WHERE S.nmOrdem NOT IN (SELECT ORDEM FROM @tbResultado) AND P.idPeriodo = @Periodo  ORDER BY ABS(nmSerie - @serie),nmSerie DESC,ABS(nmOrdem - @ordem)

SET @Periodo = @Periodo + 1


END          SELECT * FROM @tbResultado

3 answers

0

Good afternoon! Instead of using SELECT * FROM @tbResulted, because it does not use SELECT TOP 5 * FROM @tbResulted?

That way, instead of fetching all records from the @tbResulted table, it will only bring the determined amount in TOP.

Abs!

  • Speak there, Lucão. These answers are true indeed ?

  • because it doesn’t put everything in one answer?

0

Please try then the command below, I changed only to insert limit to 5 records, instead of 10 which is.

DECLARE @serie INT, 
        @ordem INT, 
        @Periodo INT 
DECLARE @tbResultado TABLE ( id INT IDENTITY(1,1) ,
                             IdCliente INT, 
                             Serie INT, 
                             Ordem INT, 
                             idPerido INT)

SET @serie = 9059 
SET @ordem = 05288 
SET @Periodo = 0

WHILE (@Periodo <= 670) BEGIN   IF (@serie < (SELECT nmSerieIni FROM tbSysPeriodo where id = @Periodo)) 
    WHILE (@serie NOT BETWEEN (SELECT TOP 1 nmSerieIni FROM tbSysPeriodo where id = @Periodo) AND (SELECT TOP 1 nmSerieFim FROM tbSysPeriodo where id = @Periodo))      
        BEGIN
            SET @serie = @serie + (select ABS(nmSerieIni - nmSerieFim)+1 from tbSysPeriodo where id = @Periodo)
        END

    IF (@serie > (SELECT TOP 1 nmSerieFim FROM tbSysPeriodo where id = @Periodo)) 
            WHILE (@serie NOT BETWEEN (SELECT TOP 1 nmSerieIni FROM tbSysPeriodo where id = @Periodo) AND (SELECT TOP 1 nmSerieFim FROM tbSysPeriodo where id = @Periodo))      
                BEGIN
                    SET @serie = @serie - (select ABS(nmSerieIni - nmSerieFim)+1 from tbSysPeriodo where id = @Periodo)
                END
            INSERT INTO @tbResultado    SELECT TOP 5 S.idCliente,
                                                     S.nmSerie,
                                                     S.nmOrdem,
                                                     P.idPeriodo     
                                        FROM tbSorte S
                                        JOIN tbSortePeriodo P       
                                        ON S.id = P.idSorte     
                                        WHERE S.nmSerie = @serie 
                                        AND S.nmOrdem NOT IN (SELECT ORDEM 
                                                              FROM @tbResultado) 
                                        AND P.idPeriodo = @Periodo   
                                        ORDER BY nmSerie DESC, 
                                        ABS(nmOrdem - @ordem)

WHILE (@serie NOT BETWEEN (SELECT TOP 1 nmSerieIni FROM tbSysPeriodo where id = @Periodo) AND (SELECT TOP 1 nmSerieFim FROM tbSysPeriodo where id = @Periodo)) 
    BEGIN
        SET @serie = ABS(@serie-(SELECT TOP 1  nmSerie FROM tbSorte S JOIN tbSortePeriodo P ON S.id = P.idSorte WHERE P.idPeriodo = @Periodo AND S.nmSerie NOT IN (SELECT Serie FROM @tbResultado)))
    END

    INSERT INTO @tbResultado    SELECT TOP 5 S.idCliente,
                                             S.nmSerie,
                                             S.nmOrdem,
                                             P.idPeriodo     
                                FROM tbSorte S      
                                JOIN tbSortePeriodo P       
                                ON S.id = P.idSorte     
                                WHERE S.nmOrdem NOT IN (SELECT ORDEM 
                                                        FROM @tbResultado) 
                                AND P.idPeriodo = @Periodo  
                                ORDER BY ABS(nmSerie - @serie),
                                         nmSerie DESC,
                                         ABS(nmOrdem - @ordem)

SET @Periodo = @Periodo + 1


END          

SELECT * FROM @tbResultado

-2

Good afternoon, but in case, it wouldn’t help, because if you see in WHILE, it goes from zero to 670, there are intervals. inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.