3 Replies Latest reply on Mar 11, 2015 11:28 AM by WolfShade

    Run a query inside a loop

    meensi Level 2

      Hi All,

      I need to run a query inside a loop; and the query will be running for more than 100 times.

      Is there anyway to do other than this.


      Based on the value, the query will be running;  I cannot do queries of queries.


      Thanks in advance!

        • 1. Re: Run a query inside a loop
          WolfShade Level 4

          It is RARELY, if ever, a _good_ idea to put a query within a loop.  Every iteration will be a connect / query / disconnnect.  Bad mojo.  It should be done ONLY if there is no alternative.


          Without knowing the context of what you really need to do, we can't really help.  Can you provide details of what needs to be done?





          • 2. Re: Run a query inside a loop
            meensi Level 2

            Here we go , there is a Table A which has got all the values which I need.


            I need to compute a value based on 4 columns ( one is Table B, second is Table C and third is Table D and fourth is from Table A)..

            so while displaying the results i need to display some columns + Computed column.


            When running inside a cfloop, i m executing the query to compute the column which i needed.


            I cannot pre compute this and have it in my Database;

            • 3. Re: Run a query inside a loop
              WolfShade Level 4

              Have you attempted doing this with a LEFT OUTER JOIN (if you need data from Table A regardless of related data in any other table) or a standard JOIN (if you DON'T want data from Table A if there is no related data in another table)?


              If you are using PL-SQL or T-SQL, is there no way to calculate the necessary values on-the-fly within a JOIN query?


              May I see the query that you currently have in place inside the loop?