Solved

SQL: Alternative to WHILE LOOP

Posted on 2013-11-14
1
421 Views
Last Modified: 2014-01-31
Hello all,

I have inherited a proc that is causing the DBAs headaches.  Specifically, there is a portion of the code that declares variables and then loops through a staging table while setting a counter.

I would like to rewrite this section to be more efficient, but don't have much experience with these sorts of things; could a CROSS APPLY work in this scenario?  What is the "best practice" for this sort of thing?

Here's the code:

DECLARE @MAXMONTHS INT
      SELECT 
            @MAXMONTHS = MAX(DATEDIFF(MM,STARTDATE,ENDDATE)) + 1 
      FROM 
            [Stage].[RESULTS] -- ADD 1 IN ORDER TO ACCOUNT FOR ROUNDING DOWN IN YEAR CALCULATION

DECLARE @THISMONTH INT
SET @THISMONTH = 1
WHILE (@THISMONTH <= @MAXMONTHS)
BEGIN
      INSERT INTO 
            [Stage].[TPHASES]
      SELECT
            R.SF_ID
            , R.AP_ID
            , @THISMONTH AS Phase
            , DATEADD(MM,@THISMONTH-1,R.STARTDATE) AS STARTDATE
            , DATEADD(D,-1,DATEADD(MM,@THISMONTH,R.STARTDATE)) AS ENDDATE
      FROM 
            [Stage].[RESULTS] AS R
            INNER JOIN 
            [Stage].[RESULTSWO] AS RWO 
            ON RWO.SF_ID = R.SF_ID AND RWO.[ROW] = 1
      WHERE 
            R.[ROW] = 1
            AND RWO.ENDDATE >= DATEADD(MM,@THISMONTH-1,R.STARTDATE)
      ORDER BY
            R.SF_ID
      SET 
            @THISMONTH = @THISMONTH + 1
END
;

Open in new window


Please let me know what other information I can provide.  Thanks.

P.S. We're on 2008 R2.  (For now.)
0
Comment
Question by:Donovan Moore
1 Comment
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39649586
Please note that there must be a reason for this. You see the procedure is trying to insert the data month by month, apparently minimising the impact on your database transaction log.

If it done it all at once it would start a huge transaction (depending on the number of rows in your tables) and would block everyone who is accessing these tables.

BTW, to remove the WHILE loop all you have to do is to adjust the script a little bit
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question