Query goes in endless loop

Asked
Active3 hr before
Viewed126 times

5 Answers

endlessquery
90%

There is some way to implement a infinite loop in SQL?? I was thinking on some like a select inside another one, recursively... (Maybe im talking foolishness),You can't create an infinite loop with a declaritive language like SQL because the compiler's won't let you. You will get error messages and/or it just won't run.,For example, in SQL Server, you would write an infinite loop the way you would in any imperative language. Roughly:,I can't imagine why you would want to do this, but it's going to depend on your implementation's (SQL Server, Oracle, MySQL...) supported loop constructs.

You can do a recursive infinite loop with a CTE:

;
with rec as
   (
      select 1 as n union all select n + 1 from rec
   )
select n
from rec

By default, SQL Server would stop at 100; you can make it loop forever with:

option(maxrecursion 0)
load more v
88%

Trust me the script above can be very dangerous as there is no break condition and it will keep on going on forever. Currently, it is going to print 1 infinite time. If you have any expensive query instead of the SELECT 1, your server can run out of resources very quickly and stop responding eventually.,I hope this blog post helps you to write the necessary loop for your business. If you have any question, you can always reach out to on LinkedIn.,I use recursive cte. And it will only let you go 100 levels by default but you can overwrite it by an query hint of option( maxrecursion n ) if you put 0 then it could go for ever if your Recursive member part doesn’t have a where to not return anything.,Pinal is an experienced and dedicated professional with a deep commitment to flawless customer service. If you need help with any SQL Server Performance Tuning Issues, please feel free to reach out at pinal@sqlauthority.com.

Here is the simplest example

WHILE 1 = 1
SELECT 1
load more v
72%

I have the following stored process that doesn't work, every time I run it it stays in an infinite loop.,The objective of the SP is to go through the table of products for each row to evaluate the result and if the result when counting the rows is greater than 0, the code must be inserted in a table, if the result is 0 the record must be inserted according to the third query.,Place the above statement inside the WHILE loop.,So the @@FETCH_STATUS is always 0 and the loop is executed infinitely.

The objective of the SP is to go through the table of products for each row to evaluate the result and if the result when counting the rows is greater than 0, the code must be inserted in a table, if the result is 0 the record must be inserted according to the third query.

ALTER PROCEDURE[dbo].[SP_Fuentes]
AS
BEGIN
--SET NOCOUNT ON added to prevent extra result sets from
--interfering with SELECT statements.
SET NOCOUNT ON;

DECLARE @suministro AS VARCHAR(8000)
DECLARE @QUERY AS VARCHAR(800)
DECLARE @fecha as varchar(90)
DECLARE @FUENTE AS VARCHAR(800)
DECLARE @ID as varchar(90)
DECLARE @NOMBRE AS VARCHAR(800)

DECLARE #P CURSOR FOR
--Cursor para recorrer rescatar todos los Tags a procesar
SELECT NRO_SUMINISTRO
FROM PRUEBA
group by nro_suministro

OPEN #P
--Recupera primera fila
FETCH NEXT FROM #P INTO
@suministro

--check
for a new row
WHILE @ @FETCH_STATUS = 0
BEGIN

SELECT @QUERY = count( * ) from(
      SELECT TOP 1[ID], [NRO_SUMINISTRO], [FEC_MAX], [FUENTE], [NOMBRE] FROM PRUEBA WHERE(FUENTE = 1 OR FUENTE = 2 OR FUENTE = 3) and nro_suministro = @suministro order by FEC_MAX DESC
   ) a

   --SELECT @QUERY;

IF @QUERY > 0
BEGIN
INSERT INTO dbo.IZACION
SELECT @suministro, @fecha, @NOMBRE, @FUENTE, @ID;
END
if @QUERY < 0
INSERT INTO ZACION
SELECT top 1 @suministro, @fecha, @NOMBRE, @FUENTE, @ID
FROM ZACION
WHERE nro_suministro = @suministro
order by FUENTE ASC

END

FETCH NEXT FROM #P INTO @suministro
END
CLOSE #P
DEALLOCATE #P
load more v
65%

The following goes into an endless loop, meaning it just constantly shows the same record over and over and over again.,You are putting the mysql query in the while statement so every time it gets there it does the same query and shows the same first record, you are never advancing to the next record in the result set.,Simply because the query gets executed again every time the loop condition is checked. That means you'll always get the same result unless something changes in the database in the meantime.,Personally I would combine all queries into one, but to show how you can solve your problem (the same applies to all loops):

The following goes into an endless loop, meaning it just constantly shows the same record over and over and over again.

<?php
while ($rowr = mysql_fetch_assoc(mysql_query("SELECT * FROM table1")) {
    while($rowu = mysql_fetch_assoc(mysql_query("SELECT * FROM table2 WHERE id = '".$rowr['uid']."'"))){
        while($rowc = mysql_fetch_assoc(mysql_query("SELECT * FROM table3 WHERE id = '".$rowr['cid']."'"))){
    ?>
    <tr><td><?php echo $rowc['post']; ?></td><td><a href="other.php?id=<?php echo $rowu['id']; ?>"><?php echo $rowu['username']; ?></a></td><td><a href="this.php?p=d&i=<?php echo $rowr['id']; ?>"><font color="#FF0000">X</font></a></td></tr>
    <?php
        };
    };
};
?>
load more v
75%

Check your spelling. A single misspelled or incorrectly typed term can change your result.,Try substituting synonyms for your original terms. For example, instead of searching for "java classes", try "java training",Select the PTF appropriate for your component level. You will be required to sign in. Distribution on physical media is not available in all countries. ,Did you search for an IBM acquired or sold product ? If so, follow the appropriate link below to find the content you need.

While running the following test cases, DB2 will run into an
infinite loop when EXPLAIN is turned on.

SET CURRENT EXPLAIN MODE = YES;

SET CURRENT DEGREE = '1';

SELECT CHARC || 'ABC', NUMC * 2 + INTC, AVG(FLOATC),
   LEFT('ABC' || STRINGC, 5)
FROM NL073.intTBL
GROUP BY GROUPING SETS((FLOATC, NUMC * 2, INTC),
   (CHARC || 'ABC', 'ABC' || STRINGC))
HAVING(CHARC || 'ABC',
   NUMC * 2 + INTC,
   AVG(FLOATC),
   LEFT('ABC' || STRINGC, 5)) IN(SELECT CHARC || 'ABC',
   NUMC * 2 + INTC,
   AVG(FLOATC),
   LEFT('ABC' || STRINGC, 5) FROM NL073.intTBL WHERE NUMC BETWEEN - 10 AND 10 GROUP BY GROUPING SETS((FLOATC,
         NUMC * 2, INTC),
      (CHARC, CHARC || 'ABC', 'ABC' || STRINGC)) HAVING CHARC LIKE '%ABC');

It is in test
case N073DN03 TX01N003, member file N073DN03
SQL1_7.The first having gets into an infinite loop, outputting "NUMC*2"
over and over again.
load more v

Other "endless-query" queries related to "Query goes in endless loop"