My preference for this type of thing is to use a tally table. I keep a view on every system that looks like this.
create View [dbo].[cteTally] as
WITH
E1(N) AS (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)),
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
cteTally(N) AS
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
)
select N from cteTally
GO
This way I have a tally table at my fingertips at all times. You can read more about tally tables and how they replace loops here. http://www.sqlservercentral.com/articles/T-SQL/62867/
To solve your current problem this becomes fairly simple. First we need a table and some sample data.
create table #Guest
(
GuestID int identity
, ArrivalDate date
, DepartureDate date
)
insert #Guest
select '2016-06-01', '2016-06-15' union all
select '2016-07-01', '2016-07-12'
Here is where the power of the tally table really shows. To produce the output for your challenge is as simple as this code.
SELECT GuestID
, ArrivalDate
, DepartureDate
, DATEADD(Day, N - 1, ArrivalDate) as EachDate
from #Guest g
join cteTally t on t.N <= DATEDIFF(day, ArrivalDate, DepartureDate) + 1
order by GuestID
, EachDate
The other big advantage of this type of approach over a recursive cte is that you are using what is known as a triangular join and can cause some performance challenges. Here is some more info on triangular joins. http://www.sqlservercentral.com/articles/T-SQL/61539/