Solved

SQL: Alternative to WHILE LOOP

Posted on 2013-11-14
1
400 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
Comment Utility
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now