Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 rsultset using while loop

Author  Topic 

archanag
Starting Member

1 Post

Posted - 2011-08-12 : 05:41:30
Hi,
I have data as follow:-
EmpNo Employeename Status/LeaveType LeaveDays LeaveFromDate LeaveDate
1 Super User PH 1 4/12/2011 4/12/2011
1 Super User EL 6 6/9/2011 6/16/2011
1 Super User PH 7 5/2/2011 5/10/2011
1003 Sandeep Karale PH 3 4/11/2011 4/13/2011
1003 Sandeep Karale PH 1 5/4/2011 5/4/2011
1003 Sandeep Karale PH 1 6/7/2011 6/7/2011
1003 Sandeep Karale PH 1 8/1/2011 8/1/2011
1003 Sandeep Karale PH 1 8/8/2011 8/8/2011
1003 Sandeep Karale PH 0.5 6/13/2011 6/13/2011
Now i want to create resultset in which we can see these records for each day e.g
1 superuser EL 6 6/9/2011 6/16/2011
.........I want this data as per date like
1 superuser EL 1 6/9/2011
1 superuser EL 1 6/10/2011
1 superuser EL 1 6/11/2011
1 superuser EL 1 6/12/2011
1 superuser EL 1 6/13/2011
1 superuser EL 1 6/14/2011
1 superuser EL 1 6/15/2011
1 superuser EL 1 6/16/2011

I trying this by using while loop...........but i am not able do it.....plz help me

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2011-08-12 : 06:14:28
You need a CROSS JOIN to a calendar table.


declare @tab table (EmpNo int, Employeename varchar(30), LeaveType char(2) , LeaveDays int , LeaveFromDate datetime, LeaveDate datetime)
declare @cal table (dates datetime)


;with mycte as
(
select cast('1990-01-01' as datetime) DateValue
union all
select DateValue + 1
from mycte
where DateValue + 1 < '2050-12-31'
)
insert into @cal
select DateValue
from mycte

OPTION (MAXRECURSION 0)

insert into @tab
select 1, 'Super User','EL',6,'2011-06-09', '2011-06-16'

select
EmpNo , Employeename , LeaveType, dates
from
@tab a
cross join
@cal b
where
b.dates between a.LeaveFromDate and a.LeaveDate
Go to Top of Page
   

- Advertisement -