Solved

SQL: Alternative to WHILE LOOP

Posted on 2013-11-14
1
405 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Auditing with Temporal Tables 4 29
backups - Strategies 1 21
all records from previous month 6 45
T SQL Update Table from another table 5 42
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

914 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

16 Experts available now in Live!

Get 1:1 Help Now