Solved

insert rows into temp table

Posted on 2013-01-14
6
413 Views
Last Modified: 2013-01-15
im trying to insert rows into a temp table but am not having any luck.


declare @StartDate as date
declare @EndDate as date
declare @today as date
set @StartDate = '1/14/2013 2:18:28 PM'
set @EndDate = '1/14/2013 2:18:28 PM'
set @today = getdate()
declare @myCounter as int
set @myCounter = 0

--drop table #tempDates

CREATE TABLE #tempDates(myDate date NULL)

if(@today != @StartDate)
begin
      --when today doesn't equal to startdate
      --ex: today 1/14 and start date 1/20
      -- should be 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/27
      while (@myCounter <= 7)
      begin
            set @StartDate = dateadd(dd, 1, @StartDate)
            insert into #tempDates values (@StartDate)
      end
end
else
begin
      --when today does equal startdate
      --ex: today 1/14 and start date 1/14
      -- should be 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20
      while (@myCounter <= 7)
      begin
            set @StartDate = dateadd(dd, 1, @StartDate)
            insert into #tempDates values (@StartDate)
      end
end

select * from #tempDates
0
Comment
Question by:fwstealer
  • 3
  • 3
6 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 38776346
Works fine for me, atlhough of course the dates start at 1/21 since the day is added before it goes to the temp table.
0
 

Author Comment

by:fwstealer
ID: 38776397
i can't get it to work at all; need the dates to begin at the startdate so how is that corrected.

when i run it - it just spins and never returns anything
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 38776596
Oh, I corrected the no-return part: sorry, based on your comments about the results returned, I assumed you had to.  Here it is:


declare @StartDate as date
declare @EndDate as date
declare @today as date
set @StartDate = '1/14/2013 2:18:28 PM'
set @EndDate = '1/14/2013 2:18:28 PM'
set @today = getdate()
declare @myCounter as int
set @myCounter = 0

--drop table #tempDates

CREATE TABLE #tempDates(myDate date NULL)

if(@today != @StartDate)
begin
      --when today doesn't equal to startdate
      --ex: today 1/14 and start date 1/20
      -- should be 1/20, 1/21, 1/22, 1/23, 1/24, 1/25, 1/27
      while (@myCounter <= 7)
      begin
            insert into #tempDates values (@StartDate)
            set @StartDate = dateadd(dd, 1, @StartDate)
            set @myCounter = @myCounter + 1  
      end
end
else
begin
      --when today does equal startdate
      --ex: today 1/14 and start date 1/14
      -- should be 1/14, 1/15, 1/16, 1/17, 1/18, 1/19, 1/20
      while (@myCounter <= 7)
      begin
            insert into #tempDates values (@StartDate)
            set @StartDate = dateadd(dd, 1, @StartDate)
            set @myCounter = @myCounter + 1
      end
end

select * from #tempDates
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:fwstealer
ID: 38778229
just noticed that the solution provided is producing 8 rows when i only need 7; what needs to change to get 7 rows?
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 38778774
Change:

while (@myCounter <= 7)

to

while (@myCounter < 7)

in both places.
0
 

Author Comment

by:fwstealer
ID: 38778787
thanks scott
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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