Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

insert rows into temp table

Posted on 2013-01-14
6
Medium Priority
?
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
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 …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

715 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