Advertisement

06.05.2006 at 07:26AM PDT, ID: 21874851
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.6

A loop to insert / update a table with dates

Asked by Bill_Harrison in MS SQL Server

Tags: ,

Hi

I am trying to use a loop to insert a date and a value into a table. The idea being that I loop through the date range and update the table if the date already exists (which also includes the day of the week) but insert if it doesn't exists.

For some reason it will only allow be to insert/update 57 records.

The end product will also update some other tables that relate to the same info, but I have only supplied the basic stuff here.

below is the code with an example:

SET NOCOUNT OFF


DECLARE @from_date datetime, @to_date datetime, @room_type_id Int, @rate money

DECLARE @days Int, @counter Int, @id Int

SET @from_date = '2006/01/01'

SET @to_date = '2006/12/30'

SET @room_type_id = 280

SET @rate = $100

SET @days = datediff(day,@from_date, @to_date)

SET @counter = 0

WHILE (@counter < @days)

        BEGIN
        
                    If EXISTS (SELECT ROOM_RATE_ID FROM ar_room_rate WHERE (ref_date = dateadd(d, @counter, @from_date) AND room_type_id = @room_type_id AND DATEPART(dw,ref_date) IN (1,2,3,4,5,6,7)))
                    
                     BEGIN
                    
                                  UPDATE ar_room_rate SET rate = @rate WHERE (ROOM_RATE_ID = (SELECT ROOM_RATE_ID FROM ar_room_rate WHERE (ref_date = dateadd(d, @counter, @from_date) AND room_type_id = @room_type_id AND DATEPART(dw,ref_date) IN (1,2,3,4,5,6,7))))
                              --SELECT 'UPDATE ', dateadd(d, @counter, @from_date)
                     END
                    
                  ELSE
                  
                     BEGIN
                                  
                               IF DATEPART(dw, DATEADD(d, @counter, @from_date)) IN (1,2,3,4,5,6,7)
                              
                                     BEGIN
                                    
                                            INSERT INTO ar_room_rate (room_type_id, ref_date, rate)
                                                                                      VALUES
                                                                                 (@room_type_id, DATEADD(d, @counter, @from_date), @rate)
                                                                                
                                            SELECT @id = SCOPE_IDENTITY()
                                            
                                            
                                            INSERT INTO ar_release_link (release_plan_id, room_rate_id, allotment, adjustment)
                                                                                                 SELECT RELEASE_PLAN_ID, @id, default_allotment, default_adjustment FROM ar_release_plan  WHERE (room_type_id = @room_type_id)
                                                      
                                          --SELECT 'INSERT ', dateadd(d, @counter, @from_date)      
                                                                                          
                                    END
                                    
                         END
                         
                         
            SET @counter = (@counter + 1)

      END
      
SET NOCOUNT ONStart Free Trial
[+][-]06.05.2006 at 10:26AM PDT, ID: 16834964

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MS SQL Server
Tags: update, loop
Sign Up Now!
Solution Provided By: Lowfatspread
Participating Experts: 1
Solution Grade: A
 
 
[+][-]06.06.2006 at 12:59AM PDT, ID: 16840448

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-42