Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

insert rows into temp table

Posted on 2013-01-14
6
Medium Priority
?
420 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 70

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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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 70

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

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 Detach & Attach 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.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

916 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